0 votes
by (180 points)

So, I just started using Twine a day or two ago... and I've come across a code block that I can't figure out.

I am trying to play with aspects of Twine and so I made a little minigame. The point of it is that you have so many buttons you can press, which have random values of 1 or 0, and you only have so many clicks *before it goes away and the done link appears*. I accomplished the basics, 

You can only choose 5 areas to search. Choose wisely.
(set: $Genergy to 0)
(set: $Gdone to false)




(if: $Gdone is false)[(display: "Gather")]




(if: $Genergy is 5)[(set: $Gdone to true)]
(if: $Gdone is true)[[[Done|Gresults]]]

 

(Gather contains a block of buttons and their (click: macros  that 1) add +/- 1 to the $Gpoints and 2) add +1 to $Genergy)

I thought this would be very straightforward, but I can't seem to make it work. When I run it, the display is there, but the clicks have no limit even after I set it. 

Basically, it seems like the last 2 lines do not exist at all. But, if I set the passage to 5, it behaves correctly.

In the test run, it shows the numbers going up as they should, leading me to think my displayed passage is doing what it is supposed to.

I also tried moving the top set lines to a previous passage, but that didn't work either.

Help..?! Thank you!

 

1 Answer

+1 vote
by (159k points)

The macros within the content of a Passage are only processed / executed / evaluated at the time the Passage is shown, generally the only exceptions to this rule are the macros contained within a hook associated with interactive macros like the (link:) and the (click:) related macros. The content of those hooks isn't processed / executed / evaluated until after the end-user has selected the related link.

So at the time the conditionals of the following two lines of your example was executed / evaluated the value of $Genergy was 0 (zero) and the value of $Gdone was false, and those conditionals won't be re-executed / re-evaluated until next time this Passage is re-shown.

(if: $Genergy is 5)[(set: $Gdone to true)]

(if: $Gdone is true)[[[Done|Gresults]]]

 

There are a number of ways you could implement what you want and most of them rely on being able to repeatably select your links and then using a code to update a marked section of the current page.

The following uses a named hook to mark the section of the current page to be updated, a (replace:) macro to update the section, and a child Passage to contain the logic and links that will appear within the section.

A. The same Passage within your example.

You can only choose 5 areas to search. Choose wisely.
(set: $Genergy to 0)
(set: $Gdone to false)

[(display: "Gather")]<gathering|

B. A prototype of the new Gather Passage.

(if: $Genergy is 5)[(set: $Gdone to true)]\
(if: $Gdone)[[[Done|Gresults]]]\
(else:)[\
	(link: "Increase Genergy by 1")[\
		(set: $Genergy to it + 1)\
		(replace: ?gathering)[\
			(display: "Gather")\
		]\
	]\
]

NOTES:
1. I have used indentation and line-breaks to format the code so that it is more readable, these indentations and line-breaks can be removed.
2. I have replaced the "is true" and "is false" Boolean checks with more correct versions and with an (else:) macro in the case of the "is false" check.
3. I used a (link:) macro instead of a (click:) macro because the (link:) macro creates it's link at the time the macro is process where as a (click:) macro needs to wait until the whole of the passage's HTML structure is generated before it can scan the all of contents of the new page to determine where to add the related link(s).
Under certain circumstances the scanning do for a individual click based link may occur more than once.

Obviously you will need you added any other link that was in your original Gather Passage to my new prototype.

by (180 points)
Thank you for your help!
...