Howdy, Stranger!

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

Get position of the element of the array (SugarCube)

How to get position of the element of the array and put it in a variable when you press the button?

So, we have an array with objects inside. Just for example:

screen1.PNG

Here is the code to display this array (objects set previously, of course):
<<for $y = 0; $y < $moduleArray.length; $y++>>		
	<<for $x = 0; $x < $moduleArray[$y].length; $x++>>		
		
		
		<<set $moduleArray[$y][$x].xy = ($y + ',' + $x)>>	
	

		<<button "<<= $moduleArray[$y][$x].xy>>" >> 		
			
					
			<<set $currentPosition.xy = $moduleArray[$y][$x].xy>> <-This, of course, does not work. The question how to make so that worked.
		
					
			<<script>>		
		
			Dialog.setup("Module", "module");
                        Dialog.wiki(Story.get("modCreate").processText());
			Dialog.open(null, function() { Engine.show() } ); 
	
			<</script>>
			

		<</button>>
		
		

	<</for>>
	<<= "<br><br>">>
<</for>>


p.s. I apologize for my English

Comments

  • Since you're attempting to use two variables ($x, $y) whose values change within the body of the <<button>> macro, which is only executed much later when the player clicks upon it, you must capture the current values of the two variables as part of the <<button>> macro's invocation.

    For example, something like the following should work:
    <<for $y = 0; $y < $moduleArray.length; $y++>>
    	<<for $x = 0; $x < $moduleArray[$y].length; $x++>>
    		<<set $moduleArray[$y][$x].xy = $y + ',' + $x>>
    		<<=
    			  '<<button `$moduleArray[$y][$x].xy`>>'
    			+ '<<set $currentPosition.xy = $moduleArray[' + $y + '][' + $x + '].xy>>'
    			+ '<<script>>'
    			+ 'Dialog.setup("Module", "module");'
    			+ 'Dialog.wiki(Story.get("modCreate").processText());'
    			+ 'Dialog.open(null, function() { Engine.show(); } );'
    			+ '<</script>>'
    			+ '<</button>>'
    		>>
    	<</for>>
    	<br><br>
    <</for>>
    
    Additional notes:
    • Use a backtick expression (top of the linked document) to yield the value of the button's link text, rather than using a print macro.
    • You do not need to print your two <br> elements.
  • Thank you very much, it works. Previously I tried to do something similar, but apparently got confused in the quotes.
  • NotDrunk wrote: »
    […] Previously I tried to do something similar, but apparently got confused in the quotes.
    That can happen, unfortunately. :(

    I'd like to be able to make capturing values easier one day.
Sign In or Register to comment.