Twine Version: 2.8.1.0
[also choose a Story Format tag above]
I’m making a stat-heavy game and using a menu screen that can lead to other pages, so I had looked into how to avoid triggering any stat-increases twice when the player returns to a stat-boosting page from the menu. Long story short, I found I could do it by running this command on every stat increase:
<<if Fresh()>><</if>
It worked. So now all my stat increases look like:
<<if Fresh()>><<set $user.stat to $user.stat + 2>><</if>
What I want to know is, is there any way to make a change to the <<set>>
command to make it implement this automatically so I don’t have to type it out a few hundred or thousand times?
warning: none of the following code has been tested!
If you showed that “menu screen” within a Dialog then you could control if a Passage Transition occurs when one of the links in that dialog is selected, because you can also use a link to close that dialog.
eg. each of the “navigation” links in the Dialog’s body could test if it points to the “current” Passage or another Passage, and only do a Passage Transition in the later case.
The content of the Menu....
<<set _passage to passage()>>
<<link "Library">>
<<if _passage is "Library">>
<<run Dialog.close()>>
<<else>>
<<goto "Library">>
<</if>>
<</link>>
<<link "Study">>
<<if _passage is "Study">>
<<run Dialog.close()>>
<<else>>
<<goto "Study">>
<</if>>
<</link>>
But to answer your original question, you could create a Widget (or custom Macro) that behaves similar to the existing <<set>>
macro.
<<widget "fset">>
<<if Fresh()>>
<<run State.setVar(_args[0], _args[1])>>
<</if>>
<</widget>>
…which you would use something like…
<<fset "$user.stat" `$user.stat + 2`>>
warning: the quoting of the 1st argument is need so a variable reference is passed to the widget, instead of the current value of that reference. And 2nd argument needs to be within Macro Argument markup so the result of the calculation is passed to the widget for assignment to the variable reference.