Coding help for new twine game [Inventory/Item Map]

Thanks, the definitely helped. The hours and days seem to be updating properly now. But I think I’m still missing something about the Mana regen code. Here’s my updated code:

<!--  Check if hour changed -->\
<<if $time != $lastRegenTime>>\
<!-- Check if day changed -->\
<<if $day != $lastRegenDay>>\
\
  <!--  Calculate hours passed -->\
  <<set $hoursPassed = parseInt($time.split(':')[0]) - parseInt($lastRegenTime.split(':')[0])>>\
  <!-- Calculate days passed -->\
  <<set $daysPassed = parseInt($day[0]) - parseInt($lastRegenDay[0])>>\
  \
    <!--  Calculate regen amount -->\
  <<set $regenAmount = $hoursPassed * $manaRegenRate>>\
\
  <!--  Update last regen time -->\
  <<set $lastRegenTime = $time>>\
\
<!-- Update last regen day -->\
<<set $lastRegenDay = $day>>\
  <!--  Add regen amount -->\
  <<set $mana += $regenAmount>>\
\
<</if>>\
<</if>>\

Bizarrely, (at least to my novice eyes) it worked once at a seemingly random point and regenerated the mana but I couldn’t replicate it to see why it regened once.

If it helps I initialised the new variables for the days in StoryInit as

 <<set $lastRegenDay = 1>> <<!-- Track last regen day-->
    <<set $daysPassed = 0>> <<!-- Track days passed -->

If you’re using $day and $lastRegenDay etc. as normal number variables, then you shouldn’t use parseInt and the array index notation [0] like you do here: <<set $daysPassed = parseInt($day[0]) - parseInt($lastRegenDay[0])>>, you can just use the variables ($day - $lastRegenDay).

About the mana regeneration: the calculation “(hours of current time minus hours of lastRegenTime)”, as mentioned above, is only accurate in the case when they’re on the same day, meaning where $day is equal to $lastRegenDay. But currently you’re doing it when the days are not the same: <<if $day != $lastRegenDay>>.

Essentially, you need to implement this logic from my post above:

Ah right, I’d assumed because <<if $time != $lastRegenTime>>\ was using not I’d want to use the same logic for <<if $day == $lastRegenDay

This is where my inexperience of coding is causing a problem - I thought I had implemented the logic from your last post. I did discover why the regen randomly worked once though - turns out I navigated to a passage during my test that had my old code that was regenerating mana based on passage navigation from before I decided to put in a time system and do it based on time passing.

Can’t find an edit button for my previous post to update it. I’ve made the following changes to the Mana Regen code:

<!-- Check if hour changed -->
<<if $time != $lastRegenTime>>
  <!-- Check if day changed -->
  <<if $day == $lastRegenDay>>
    <!-- Calculate hours passed -->
    <<set $hoursPassed = parseInt($time.split(':')[0]) - parseInt($lastRegenTime.split(':')[0])>>
    <!-- Calculate regen amount -->
    <<set $regenAmount = $hoursPassed * $manaRegenRate>>
    <!-- Update last regen time -->
    <<set $lastRegenTime = $time>>
    <!-- Add regen amount -->
    <<set $mana += $regenAmount>>
  <<else>>
    <!-- Calculate hours from last regen until midnight on previous day -->
    <<set $hoursToMidnightPrevDay = 24 - parseInt($lastRegenTime.split(':')[0])>>
    <!-- Calculate hours from midnight until now -->
    <<set $hoursFromMidnightToNow = parseInt($time.split(':')[0])>>
    <!-- Calculate total hours passed -->
    <<set $hoursPassed = $hoursToMidnightPrevDay + $hoursFromMidnightToNow>>
    <!-- Calculate regen amount -->
    <<set $regenAmount = $hoursPassed * $manaRegenRate>>
    <!-- Update last regen time -->
    <<set $lastRegenTime = $time>>
    <!-- Update last regen day -->
    <<set $lastRegenDay = $day>>
    <!-- Add regen amount -->
    <<set $mana += $regenAmount>>
  <</if>>
<</if>>

And it seems to have introduced a strange bug that I can’t figure out so can’t properly test if its working as intended. So I have a repeatable passage where the player can train in magic and every time they visit it, it should reduce the value of $mana by 20. This was working before I made the changes to Regen. Now it works once and not on subsequent visits - unless I use the Sleep passage (and in theory I suppose any passage that would trigger a regen by advancing time enough) to regen the mana where it, again, only works once until another regen.

OK I’ve worked out what the bug was and I think I know how to fix it. Basically, mana regen rate was 5 and 4 hours was elapsing in the passage. So 4x5 is 20 and 20 was the amount of mana being reduced in the passage so it had a net difference of 0 and didn’t appear to be updating mana properly once I had the Regen over time code working properly.