Saving a Variable (Capture Help?)

Twine Version: 2.2.1
Story Format: 2.35.0

Hello! Relative newbie here, working on a turn based combat system. Whenever the player takes their turn, I’d like to be able to tell them what has happened since their last turn, like what the enemies have done. I have an array where these messages go that clears after the player sees them, but I’m having trouble saving correct values in these messages.

Here’s a simplified version of what I’m working with:

<<set $messages to []>>

<<set $dmg to random(7,9)>>
<<set $hp -= $dmg>>
<<run $messages.push('You took $dmg damage from the Python!')>>

<<set $dmg to random(10,12)>>
<<set $hp -= $dmg>>
<<run $messages.push('You took $dmg damage from the Wolf!')>>

<<= $messages.join('<br>')>>

This process is happening over the course of several looping passages. So the problem as you can probably see is that both messages will say 10-12 damage, when in fact the Python only dealt 7-9 damage.

Of course I could use different variables for each source of damage, but this gets complicated because there can be more than one of each type of enemy (so I can’t just use $wolfdmg), and each enemy can take multiple turns before the player sees the messages (so I can’t just use $enemy1dmg).

So I’m wondering if there’s a way I can “save” a variable as a string for the purposes of these messages, so that when the variable changes, the message remembers what the value was, and not what the value currently is. This sounds like what the capture macro is good for, but I can’t figure out how to use it.

EDIT: Oversimplified my example code a bit and failed to specify that the amount of damage is somewhat random. Fixed.

You could compare the player’s hp in the last turn to the current hp,

Before the turn

<<set $prevhp to $hp>>

During or after the turn

You took <<print ($hp-$prevhp)>> damage this turn

You may try something along this:

<<set $dmg to [8]>>
<<set $hitter to ["Python"]>>
<<set $dmg.push(12)>>
<<set $hitter.push ("Wolf")>>
<<for _i to 0;_i lt $dmg.length; _i++>>
You took $dmg[_i] damages from the $hitter[_i].
<</for>>

Instead of a one array with the full messages, there’s two arrays, one for the damages and one for the source of the damages.
A loop ensure you write each line.

Rather than attempting to reference the variable later, just resolve its value during string creation.

For example:

<<run $messages.push('You took '
+ $dmg + ' damage from the Python!')>>
1 Like