Repeating Variable

Twine Version: Sugarcube 2.0
[also choose a Story Format tag above]

Help needed -

I have been trying to create a system where repeating action will change multiple variable. For example if the player went to the gym and worked out then his strength goes +1 and energy - 10.
And if he does it again the effect increases the stat.

I have already set the variables of the player with strength set to 0 and energy one to 100. What kind of code should I use.

Thanks in advance.

You can use a link with a setter component which will change multiple values when the player clicks on it. It’s described here in the Sugarcube docs: SugarCube v2 Documentation

To set multiple variables with a single link, just separate them with a comma inside the bracket.

You should also wrap the link in an if-statement to make sure it can only be selected when the player has 10 or more energy.

::PassageName
current Energy: $energy
current Strength:  $strength 
\<<if $energy >= 10>>
   <<link "Workout" PassageName>> <<set $energy to $energy - 10, $strength =$strength + 1>> <</link>>
\<</if>>

This is how I would do it if you want to implement it simply, but if you don’t like that the passage is reloaded every time you click workout, I would recommend then moving it into its own Widget

in a extra widget I would do something like this:

::widgets (Tagged with "widget" important! ) (I also add "nobr" tag to my widget so it wont put in <br> into my text)

<<widget "workout">>
<<if $energy >= 10>>
<<set $energy to $energy - 10, $strength =$strength + 1>>
That was a nice workout!
<<else>>
I am too tired to Workout Right now...
<</if>>
<</widget>>

then in your Passage (example: Gym)

::Gym

<<link "Workout" >> <<replace "#workout">> <<workout>> <</replace>> <</link>>
<span id="workout"></span>

Hope this helps a little.
Have a nice Day!

This worked, thank you.

If you don’t mind, how do I show changes at the side bar.
Currently my StoryCaption passage shows_

Energy: $energy
Strength: $strength
Intelligence: $int
Charm: $chr

but if for example the character works out the stats dont seem to change although using the browser console to bring up variables shows changes, such as strength = 2 changed to 3.

1 Like

That’s because the story caption element is only updated when you navigate to a new passage. It doesn’t monitor changes to the variables and update itself automatically. To update it without navigating to a new passage, you need to use the <<replace>> macro. Here’s a widget that does that:

<<widget updatestats>>
	<<replace "#story-caption">>
		<<include "StoryCaption">>
	<</replace>>
<</widget>>

You call the widget inside your workout link:

<<link "Workout" >>
	<<replace "#workout">>
		<<workout>>
	<</replace>>
	<<updatestats>>
 <</link>>
1 Like

As svlin already mentioned
The Story caption updates itself on a render of a new passage, but if you incase your $stats in your Story caption with a span each like this:

<span id="energy"> $energy </span>
<span id="strength"> $strength </span>

you can put this code into your <<workout>> widget between the <<if>> and <<else>> part :

<<script>> 
document.getElementById("energy").innerText = variables().energy; 
document.getElementById("strength").innerText = variables().strength 
<</script>>

So you won’t need to go to a new passage and you will only replace the stats you are currently changing instead of the whole StoryCaption, (but this is just my personal preference as I do put a lot of new elements into my StoryCaptions)