Howdy, Stranger!

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

Sugarcube 2: Issues setting a variable in a link

Hi,

I have an array $houses containing a bunch of objects. I have a passage set in a street, where I load a list of houses that link to a House passage that would then describe whichever house the player decided to go to.

The code in my street passage to display the list of houses as links is as follows:
::Street

You are on <<print $activeStreet>>.

You can go to:
<<for $i to 0; $i lt $houses.length; $i++>>
	[[$houses[$i].housenumber + " " + $houses[$i].street|House][$activeHouse to $houses[$i]]]
<</for>>

The code in my House passage is as follows:
::House

You are standing in front of <<print $activeHouse.housenumber>> <<print $activeHouse.street>>.

The problem is that the passage link is not setting $activeHouse to $houses[$i]. Am I doing something wrong?
Error: <<print>>: bad evaluation: Cannot read property 'housenumber' of undefined 
Error: <<print>>: bad evaluation: Cannot read property 'street' of undefined.

Thanks.

Comments

  • Ok, turns out it was setting the variable, it's just that it evaluates the value of $i within the links at the end of the for loop so the value was out of bounds of the array.

    Is there a way I can get around this so it creates the link with the correct value of $i for each link?
  • note: I am not on a machine with access to Twine at the moment so the following code example has not been tested.

    You will need to use a <<print>> macro to dynamically create the markup link, the line within your for loop needs to look something like the following:
    <<print '[[$houses[' + $i + '].housenumber + " " + $houses[' + $i + '].street|House][$activeHouse to $houses[' + $i + ']]]'>>
    
    ... notice the usage of single quotes to define each of the String literals, this is to allow the double quotes to be used within the literals.
  • clorax wrote: »
    Ok, turns out it was setting the variable, it's just that it evaluates the value of $i within the links at the end of the for loop so the value was out of bounds of the array.

    Is there a way I can get around this so it creates the link with the correct value of $i for each link?
    When directly using a variable within a code section which is evaluated later—e.g. the setter component of the link markup or the contents of the <<link>>/<<button>> macros—whose value will change between the time the code section is set up and the time in which it is evaluated, you must capture its value when when doing the setup.

    IOW, you need to capture the value of your index variable when you create the link. You'd do this with the Stupid Print Trick™.

    For example:
    You can go to:
    <<for _i to 0; _i lt $houses.length; _i++>>
    	<<='[[$houses[_i].housenumber + " " + $houses[_i].street|House][$activeHouse to $houses[' + _i + ']]]'>>
    <</for>>
    
    Notes:
    1. You should probably be using temporary variables to hold temporary values, like a loop index, as it keeps your variable store clean.
    2. The only instance of the temporary variable _i which is captured is the one within the setter component of the link, as that's the only one which is resolved late.
  • Thank you greyelf and TheMadExile for helping me with this. Your solutions worked and I've also switch to using temporary variables where appropriate.
Sign In or Register to comment.