Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Sugarcube 2] Is it possible to change a variable value without refreshing the passage?

I'm trying to do something like this:
(assuming $shipSpeed = "5s")
<<click "Faster">>
<<set $shipSpeed = "1s">>
<</click>>
<<click "Slower">>
<<set $shipSpeed = "10s">>
<</click>>

Distance from Earth: <span id="countdown">10</span> million miles
<<silently>>
<<timed $shipSpeed>><<replace "#countdown">>9<</replace>>
<<next>><<replace "#countdown">>8<</replace>>
<<next>><<replace "#countdown">>7<</replace>>
<<next>><<replace "#countdown">>6<</replace>>
<<next>><<replace "#countdown">>5<</replace>>
<<next>><<replace "#countdown">>4<</replace>>
<<next>><<replace "#countdown">>3<</replace>>
<<next>><<replace "#countdown">>2<</replace>>
<<next>><<replace "#countdown">>1<</replace>>
<<next>><<goto Earth>>
<</timed>>
<</silently>>

I understand this code would "work" if I put a <<goto "this passage">> inside the click macro, or using this: state.display(state.active.title, null, "back")
the problem is in both cases it goes back to 10 in the countdown, or if I had a bunch of click-replace it would undo all the "replaces", <<timedcontinue Xs>> text would disappear and start showing again, etc. It's the third time I hit this wall and I was wondering if there is a solution.

Comments

  • Your subject seems to not be related to what you're actually asking. You really seem to be asking if you can change the duration of a running <<timed>>. The answer to which is no, you cannot.

    You can, however, use the <<repeat>> macro to achieve what I believe you desire. For example: (assume $shipSpeed is 5)
    <<link "Faster">>
    	<<set $shipSpeed to 1>>
    <</link>>
    <<link "Slower">>
    	<<set $shipSpeed to 10>>
    <</link>>
    
    Distance from Earth: <<set _countdown to 10>><span id="countdown">_countdown</span> million miles
    <<silently>>
    	<<set _trigger to 0>>
    	<<repeat 1s>>
    		<<set _trigger++>>
    		<<if _trigger gte $shipSpeed>>
    			<<set _trigger to 0>>
    			<<set _countdown-->>
    			<<if _countdown gt 0>>
    				<<replace "#countdown">>_countdown<</replace>>
    			<<else>>
    				<<goto "Earth">>
    			<</if>>
    		<</if>>
    	<</repeat>>
    <</silently>>
    
    NOTE: Off-the-cuff and untested, as I am, literally, running out the door for some medical appointments.

    _vrus wrote: »
    […] or using this: state.display(state.active.title, null, "back") […]
    Don't do that. While it will still work, due to legacy shims, that is SugarCube v1 code. The equivalent SugarCube v2 code would be the following:
    → Exact equivalent.
    Engine.play(State.passage, true)
    
    → Better equivalent.
    Engine.show()
    
    SEE: Engine.show(), Engine.play(), State.passage.
  • Oh, nice.
    Actually that was just a silly example. The thing is, my passages are loaded with timed stuff and click-replaces, so by the time I want to change a variable value the user has already chosen to do multiple things that will be undone if the passage reloads.
    However your solution does work and solves most cases with some modifications here and there.
    Thank you
Sign In or Register to comment.