Howdy, Stranger!

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

Early evaluation inside of a click replace macro?

edited October 2016 in Help! with 2.0
Hello all,
New arrival here. I've only been tooling around with Twine for a week, so feel free to respond with a link if there's documentation on this that my searching today didn't uncover. (I admit I'm bad at searching, especially for my glasses. if only I could see to look for them...)

I have an object array of 'people' and a for loop that iterates across the array to populate a table.
It "works" and the table is filled out, but when I click on the resultant click macros, they all call the index from the end of the loop, not the pass they were on when they were produced inside the for loop.
So... I keep getting this error:
Error: <<Hehas>>: error within widget contents (Error: <<if>>: bad conditional expression in <<if>> clause: Cannot read property 'type' of undefined)
from:
<<print "<td><<click \"Look\">><<replace \"#displayArea\">> <<Hehas $people[$i]>> ... <</replace>><</click>></td>">>
What seems to be happening is that when I click on the 'Look' button, my widget <<Hehas>> is being passed $people[2] and since I'm just doing testing so far, my character list is only $people[0] and $people[1].

How do I get Twine/Sugarcube2/Javascript to evaluate $i into a number during the creation of the click button rather than when it's clicked? <<print>> seems to work for everything BUT the stuff inside my click-replace macro pair.

Comments

  • First. Some tips:
    1. Unless you're using its value in later turns, you should be using a temporary variable for your loop index, rather than a story variable—i.e. _i vs $i.
    2. Alternate your quotes so you don't have to escape nested quotes so much.
    3. While it will continue to work, <<click>> has been deprecated, use <<link>> instead.

    You want to print the value of the index variable, not the variable itself, so just do that. For example:
    <<print '<td><<link "Look">><<replace "#displayArea">> <<Hehas $people[' + _i + ']>> … <</replace>><</link>></td>'>>
    
Sign In or Register to comment.