Carrying varibles over from one passage to another

Using Twine 2.6.2, Sugarcube 2.36.1

So I’m attempting to tackle a game with stats that are randomly generated that help either show the player good, bad, or middle of the road options. I decided to add a little state chart at the bottom of every passage so the player can see what numbers they’re starting out with. However, I discovered that the game might not be carrying the set numbers through out the game. It’d go something like this:

<<set _wis to random(8, 15)+1>>

You look around, the forest all around you. On a immediate glance there's no indication of which would be better.

You decide...

<<if _wis >= 13>>
That surely [[walking forward|Path 1]] is the best path you can take. The sheltering branches make almost a corridor for you to follow and the lighting there looks greatest.
<<elseif _wis >= 10>>
What you had always heard is that if you're stuck in a maze, sticking to the left was best. So [[walking left|Path 2]] in a maze as massive and confusing as the woods must apply here too.
<<elseif _wis < 10>>
You can't know for certain what path goes where but you know the sun is setting to the right of you. So [[walking right|Path 3]] must be as good a path as any.
<</if>>

Stats
Strength: <<print _str>>
Dexterity: <<print _dex>>
Constitution: <<print _con>>
Intelligence: <<print _int>>
Wisdom: <<print _wis>>
Charisma: <<print _chr>>

Wisdom prints, all good thus far, as the other stats are undefined which is fine, it should fill out as it goes along. But then on the next passage, wisdom returns to being undefined and the next stat is printed, and it continues on like this. Maybe print is the wrong thing to use in this case, maybe I need to add some javascript code, but I couldn’t figure out what I should do based on what I could find. Anybody else have an idea of what to do?

You are using temporary variables (which start with an underscore). Temporary variables only exist until you change passage, and are then wiped — as you’ve noticed.

You need to use story variables (which start with a $ sign). Story variables carry on from passage to passage (and are saved in save files.

So just change _wis to $wis

2 Likes

I had seen that but only in discussions using the Harlowe format so I assumed it was just in Harlowe and not in Sugarcube. Thank you for clearing that up!

Reading the SugarCube documentation, in this specific case the TwineScript - Variables section, can help with learning what features that story format has. :slight_smile:

2 Likes