0 votes
by (680 points)

Sugarcube 2, twine 2.

Hi, again! I'm trying to figure out how to make an <<if>> statement repeatedly check for a change in value? For instance, 

<<set $choice to true>>

will occur onto the same passage with a

<<if $choices>>Something said here<</if>>

The statement checking if $choices, only seems to work if it's displayed after $choices has been given a bool. But I don't want that to be the case, since this line of code would exist before it's given a set variable.

I know <<for>>  repeatedly executes its functions, though I've never understood how it works. And how exactly I could implement it for this.  Thank you for your guys's patience in answering my questions as of lately! I'm trying to learn everything I can about this :) 

2 Answers

+1 vote
by (2.7k points)
selected by
 
Best answer

This should do the trick:

<<repeat 0.1s>><<if $choices>>Something said here<</if>><</repeat>>

What this does is checks the value of $choices every 0.1 seconds. A 0.1 second delay is not very noticeable, but is long enough so that it does not cause lag.

by (23.6k points)

Don't forget the stop <<stop>>

<<repeat 0.1s>><<if $choices>>Something said here<<stop>><</if>><</repeat>>

 

by (680 points)
Thank you SmuJamesB and Idling! To idling, I was struggling with the <<replace>> for awhile because of the overwriting issues between div class <pblue> and <choice>, but apparently it's fixed now and it was just a few </div> issues. Looking at the fix you've given me, it's solved the issue! But knowing the <<repeat>> exists, especially with <<stop>> that'll really help me with any directions I take in the future. Thanks again guys for your patience!
by (159k points)

WARNING

Timer based events (like the one created by the suggested <<repeat>> macro) briefly interrupts the Reader's ability to interact with the page each time that event occurs.

The small the delay is between occurrences the more noticeable that interruption is to the Reader, this is also true if more than a single timer based event (macro) is active within the same Passage at the same time.

eg. In the suggested example the <<repeat>> macro is set to occur every 10th of a second, this means that every 10th of a second (or 10 times a second) the Reader's ability to do things like selecting a Link is affected.

+1 vote
by (23.6k points)
You can theoretically set up a Javascript repeater function to continously run the same code, but that's not a very great idea. If you could outline in more detail what you are trying to accomplish we could maybe find a better solution. Maybe just trigger the <<if>> whenever the variable is changed?
by (680 points)
edited by

I'm mostly just interested in assigning variables a value, that can be used to advance the story further when the proper value is given. For instance..

 

He scans the room. Looking for an escape of some sort, he tries to get up off his feet. Slowly but surely, with one foot after the other, he was up entirely. He was unusually well built, for someone of his size. He winced, his legs were shaking, yet he stood his 
<div class="pblue">
      <<linkreplace 'ground.'>>grounds.

<<timed 2s>>"Well<<timed 0.5s>>.<<next>>.<<next>>.<<timed 2s>> Then can you at least tell me where I am?”

<<set $choices to true>>

  
<</timed>>
      <</timed>>
             <</timed>>
                   <</linkreplace>>
                                  </div>


<<if $choices>>
<div class="choice">[[I'm Not Allowed to Tell You That Either.]]</div>
<div class="choice">[[You're Dead.]]</div>
<</if>>

It'd be pretty cool to figure out how to cause this specific <<if>> to react when variables change! As far as I can tell though, this is all I could find on it. https://stackoverflow.com/questions/10638769/in-javascript-how-to-trigger-event-when-a-variables-value-is-changed 

But.. I really don't understand it!

 

by (23.6k points)
edited by

As you can see in the answer you yourself linked to, it is not possible to link events to a variable changing values. Instead you'd have to use the <<replace>> macro.

 

He scans the room. Looking for an escape of some sort, he tries to get up off his feet. Slowly but surely, with one foot after the other, he was up entirely. He was unusually well built, for someone of his size. He winced, his legs were shaking, yet he stood his 
<div class="pblue">
      <<linkreplace 'ground.'>>grounds.

<<timed 2s>>"Well<<timed 0.5s>>.<<next>>.<<next>>.<<timed 2s>> Then can you at least tell me where I am?”

<<replace "#test">>
	<div class="choice">[[I'm Not Allowed to Tell You That Either.]]</div>
	<div class="choice">[[You're Dead.]]</div>
<</replace>>

  
				<</timed>>
			<</timed>>
		<</timed>>
	<</linkreplace>>
</div>


<span id="test"></span>

I'm also not sure, why you can't just say:

He scans the room. Looking for an escape of some sort, he tries to get up off his feet. Slowly but surely, with one foot after the other, he was up entirely. He was unusually well built, for someone of his size. He winced, his legs were shaking, yet he stood his 
<div class="pblue">
      <<linkreplace 'ground.'>>grounds.

<<timed 2s>>"Well<<timed 0.5s>>.<<next>>.<<next>>.<<timed 2s>> Then can you at least tell me where I am?”

<div class="choice">
[[I'm Not Allowed to Tell You That Either.]]
[You're Dead.]]
</div>
 
				<</timed>>
			<</timed>>
		<</timed>>
	<</linkreplace>>
</div>

The choices div class should overwrite the pblue div class, if that's what you're concerned about.

...