Howdy, Stranger!

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

Create List of links from Array

I'm trying to create a list of Links from an Array. On each click a variable should be assigned to the respective Object.
<<for $i to 0; $i lt $knownCities.length; $i++>>
	<<set $tmp = $knownCities[$i]>>
	<<if $knownCities[$i].name neq $currentCity.name>>
		<<click $knownCities[$i].name "TravelTo">>
			<<set $nextCity = $knownCities[$i]>>
		<</click>>
	<</if>>
<</for>>
Apparently the variable $nextCity gets assigned to undef. Which means that the
<<set $nextCity = $knownCities[$i]>>
Part executes with the last $i. What would be a better way to solve this?

Comments

  • As you found out, the code within the click uses the value of the variable at the time of the click occurs. You can use a <<print>> macro to get around this issue:
    <<for $i to 0; $i lt $knownCities.length; $i++>>
    	<<set $tmp = $knownCities[$i]>>
    	<<if $knownCities[$i].name neq $currentCity.name>>
    		<<print "<<click $knownCities[$i].name 'TravelTo'>><<set $nextCity = $knownCities[" + $i + "]>><</click>>">>
    	<</if>>
    <</for>>
    

    note: I am not at a machine ATM with SugarCube installed so the above was written from memory and has not been tested.
  • This worked perfectly, thank you.
Sign In or Register to comment.