Variables come back higher than expected

Hi everone, I’m new here, and to Twine and game development in general. I discovered Twine a few days before Christmas and decided to start with something simple but, before I knew it, my project kind of took on a life of its own and slowly kept escalating in complexity. I don’t really mind, in fact, I welcome the challenge and the steep learning curve but now I have run into a problem that I can’t figure out…

It’s an original fantasy story in a roleplaying-like layout. I have a multi-choice character creation system, where each choice is recorded as a part of the character’s backstory, and also determines your starting attributes, and so on. I have run about two dozen tests on it so far, and every now and then (2-3 times) one or two attributes turns out about twice their intended value. I have poured over the code several times and it looks (with my newbie eyes) to be exactly as it should be. It also doesn’t happen every time, so I have tried to recreate the circumstances in additional tests but, then, the variables came back as intended instead. Could it be a glitch in Twine or is it because I need to optimize my code? I have used << set $var >> for all attributes in the StoryInit passage, and << set $var += >> in “current” passages for the individual choices. I’m not using any arrays, although I know I probably should. I have uploaded a screenshot with an example of the code below:

Twine Version: 2.3.9
Story Format: Sugarcube 2.31.1

Generally its better if you copy-n-paste your code examples into your comment, making sure to wrap it within Preformatted Text mark-up, which allows anyone who answers your question to copy-n-paste that code example into their own test project to debug it.

At first glance their doesn’t appear to be anything wrong with the screen captured code. A couple of questions though:

  1. Can the Reader revisit that Passage multiple times while $aBackground has the same value?
  2. Can the Reader visit that Passage when $aBackground has a value other than what is catered for by the shown conditional expresions?

I will definitely keep that in mind, pasting in my code next time. To answer your question, no, none of the background passages are revisitable. I have, however, pinpointed a separate location during the early game, where you’re supposed to get +1 Charisma (meaning, increase the variable by +1). See the code below. Most of time, this passage works exactly as it is supposed to but, then, one out of eight or nine times, it suddenly adds +5 to the variable instead of +1. Again, I have been going over the code myself but I can’t find anything wrong with it. It would be really nice to solve this…

Code for increasing my variable:

//''Your Charisma has increased by +1''//<<if visited("Intro3N") eq 1>><<set $charisma+=1>><<set $apcombat to $agility + $fitness>><<set $apmagic to $cunning + $willpower>><<set $apstealth to $agility + $cunning>><<set $apsocial to $willpower + $charisma>><</if>>

Note that the $ap- variables only recalculates skills, based on the new primary variables, and the passage can only be “visited” again by entering any of your “character sheet”, “inventory”, or “journal” passages. Still, I had to cap the increase so you only get it once :blush: I really appreciate any tips or advice at this point.

Are you saying that the stat increases are run every time you visit your character info page? That’d be why they keep changing on you then.

No, then I’m saying it wrong. The stats only increase during various story passages and, as I showcased in my previous comment, I have found ways to ensuring you only get each increase once:

<<if visited("storypassage") eq 1>><<set $charisma+=1>>

The character info page merely show the values. However, every time you go to a menu passage, should as character info, you essentially “revisit” the story passage when you exit that menu again. Hence the above code. I hope that clarified my previous comment.

== Update ==
What you said, @tayruh, actually made me think a little. Then it dawned on me that because of the way I’ve made the character creation, the character sheet becomes available in the same passage as you received the final background-related modifiers, which may account for the messed-up variables during that sequence. I’ve implemented the same cap here as I use in the story passages now, to prevent the bonuses from repeating and I haven’t seen any further discrepancies. So, thank you, I really appreciate it :blush:

However, that still leaves me with the +1 bonus that occasionally comes back as +5 instead. I’ve run a few dozen more tests, and I can’t help but feel like the answer is staring me right in the face. I just can’t figure out why it happens only sometimes… I’ve added the entire passage below, hoping that it might show one of you guys what I have done wrong:

<<set _rollResult to random(1,6) + $apsocial>><<if _rollResult gte 12>>You look at the goblins and they look back at you. You get up and advance a single step. Then you say in a confident voice:
//"You understand the common tongue?"//
The goblins look at each other in confusion but, then, one of them responds:
//"Aye, we does!"//
//"If you're smart"// you say with narrow eyes //"you'll run back the way you came, and never bother the good folk of these mountains again!"//
The goblins seem to be deliberating between themselves for a brief moment before giving you a determined nod.
//"We understands, you spares us and we goes home..."//
Before you can say anything more, the two goblins turn around and begins to sprint away...

//''Your Charisma has increased by +1''//<<if visited("Intro3N") eq 1>><<set $charisma+=1>><<set $apcombat to $agility + $fitness>><<set $apmagic to $cunning + $willpower>><<set $apstealth to $agility + $cunning>><<set $apsocial to $willpower + $charisma>><</if>>

[[Loot the spoils before you continue|Intro3K][$coin+=5]]
<<if $spHeal1 eq true and $energy gte 25 and $vitality lt $maxvitality>>[[Cast Minor Healing before you loot the bodies (-25 Energy)|Intro3K][$energy-=25; $vitality to $maxvitality; $coin+=5]]<</if>><<elseif _rollResult gte 8>>You look at the goblins and they look back at you. You get up and advance a single step before you say in a confident voice:
//"You understand the common tongue?"//
The goblins look at each other in confusion but, then, one of them responds:
//"Aye, we does!"//
//"If you're smart"// you say with a wicked grin //"you'll run back the way you came, and never bother the good folk of these mountains again!"//
The goblins seem to be deliberating between themselves for a brief moment before giving you a determined nod.
//"We understands, but we wants Tushak's coins"// one of them says and gestures to their dead friend //"you agrees, yes?"//
You consider it for a moment but eventually decide to let them have the spoils. How much coin can a few goblins really have anyway? You nod and the goblins proceed to rummage through their dead friend's belongings. As soon as they find the handful of coins they were looking for, they give you a nod and get up to run away...

[[Continue on your way->Intro3K]]
<<if $spHeal1 eq true and $energy gte 25 and $vitality lt $maxvitality>>[[Cast Minor Healing before you continue (-25 Energy)|Intro3K][$energy-=25; $vitality to $maxvitality]]<</if>><<elseif _rollResult lt 8>>You look at the goblins and they look back at you. You advance a single step and say in a confident voice:
//"You understand the common tongue?"//
The goblins look at each other in confusion but, then, one of them responds:
//"Aye, we does!"//
//"If you're smart"// you say with a serious mien //"you'll run back the way you came, and never bother the good folk of these mountains again!"//
The goblins seem to be deliberating between themselves for a brief moment before raising their spears and charging. Surprised by their bravery, you barely get your sword up in time to parry the first attack, and the next plants a sharp stick firmly in your hip...<<set $vitality-=10>>

[[Fight back and hope you can still regain the upper hand->Intro3L]]<</if>>