0 votes
by (130 points)
I apologize in advance if this was answered somewhere else and I missed it.

In my story I have a few numerical values, stats, that are consistently tracked and can be increased or decreased based upon choices made by the player.

When a choice is made that causes one of these stats to be changed, I would like text to be displayed that describes the change. For example, when within the story, I place the following command:

<<set $healthpoints to $healthpoints + 1>>

I would like it to display within the story with something like:

"You feel your vigor increase!"

I could add such a line into every passage where such a change occurs, but that seems inelegant. I've tried updating the StoryInit passage with variations of this command:

<<if $healthpoints to $healthpoints + 1>>\
You feel your vigor increase!

But it's not doing anything but throwing up errors. You're all probably laughing at me right now, and that's okay - I'm learning this as I go.

Any help or advice you can provide will be greatly appreciated. Thanks in advance.

2 Answers

0 votes
by (23.6k points)

Depending on your version of Sugarcube/Twine you can use a widget. Put this into a passage you have tagged widget:

<<widget statchange>><<nobr>>

<<print '<<set ' + $args[0] + ' += 1>>'>>

<<if $args[0] is "$strength">>
	Your vigor increased.
<<elseif $args[0] is "$intelligence">>
	Your smartass brain grew.


When you now want to change a stat by one, you can do this:

<<statchange "$intelligence">>
<<statchange "$strength">>

Instead of

<<set $strength to $strength + 1>>

Works for Sugarcube 2.22.0 and Twine 2.2.1



by (130 points)
Thank you, this is very useful! I extrapolated from your example to create a decrease stat widget as well; both work, and I'm quite pleased with the result.

I appreciate you taking the time to respond.
+1 vote
by (159k points)

note: You don't state which version of SugarCube you are using so I will assume that it's v1.0.35

You could create one or more widgets that you use to both update the relevant store variable and output the related text message.

1. Initialise the variables within your StoryInit special passage.

<<set $healthpoints to 10>>

2. Create a passage to store your widgets in and assign this passage a widget tag.
The passage name isn't important so you can name it whatever you like, I named mine Widgets for simplicity sake. The following example defines two widget: one to increase the value of the $healthpoints variable and the other to decrease it, to could create two similar widgets for each of the relevant story variables. 

<<widget "increasehealth">>
	\<<if $args[0]>>
		<<set $healthpoints += $args[0]>>
		<<replace "#messages">>You feel your vigor increase!<</replace>>

<<widget "decreasehealth">>
	\<<if $args[0]>>
		<<set $healthpoints -= $args[0]>>
		<<replace "#messages">>You feel your vigor decrease!<</replace>>

3. A passage to test the new widgets in.
Click on each of the two links to see the message change.

<span id="messages"></span>

<<click "Increase Health">>
	\<<increasehealth 3>>
	\<<append "#messages">><br>Health is $healthpoints<</append>>

<<click "Deccrease Health">>
	\<<decreasehealth 2>>
	\<<append "#messages">><br>Health is $healthpoints<</append>>

4. You can use the following code to clear the messages ID'ed area.

<<replace "#messages">><</replace>>