Push member to array when clicking on a link

If you are requesting technical assistance with Twine, please specify:
Twine Version: 2.34.1
Story Format: SugarCube

I have an array:

<<set $tarray to []>>

I need to push members to $tarray upon clicking a link, can’t figure it out.

[[Go to Zone|Zone Name][$days += 1, $tarray.push("member")]]

The array does not change upon clinking the link. Any advice?

The code you posted works fine, so I’m going to guess that that code isn’t exactly the code that’s failing for you. Thus, I’m going to have to guess what code you’re actually using.

If you’re generating the links within a loop and using the loop variable to determine what data gets pushed into the array when the user clicks the link, then you’re going to need to use the <<capture>> macro on that loop variable. That macro makes it so that the code encompassed within that macro will use the values of the variables passed to that macro that they had at the time the macro was being executed.

For example, if you’re doing something like this (where $zones is an array of zone names):

<<for _i = 0; _i < $zones.length; _i++>>
	[["Go to " + $zones[_i]|$zones[_i]][$days += 1, $tarray.push($zones[_i])]]
<</for>>

That .push() code in the links won’t work, because the value of _i at the time the user clicks on any of those links will be set to $zones.length, which will be out of bounds of the $zones array, thus causing an error when it’s executed, thus nothing gets pushed into the $tarray array.

To correct that, you’d need to use the <<capture>> macro like this:

<<for _i = 0; _i < $zones.length; _i++>>\
	<<capture _i>>\
		[["Go to " + $zones[_i]|$zones[_i]][$days += 1, $tarray.push($zones[_i])]]
	<</capture>>\
<</for>>

With that change, the value of _i within the <<capture>> macro will be fixed at the current value that _i had within the loop at the time that each of those links were generated.

If you need a bit more help understanding how the <<capture>> macro works, you might want to take a look at the “<<capture>> Macro Help” section of my Twine/SugarCube sample code collection.

If that doesn’t solve your issue, then we’ll need to see the actual code that you’re using.

Hope that helps! :slight_smile:

Oh shoot you’re right it does work! And you’re also right that that’s not the real code. My bad, I don’t have real code since I’ll do it up once I make it work, but the real problem is me wanting to push a member to an array without leaving the cell where it occurs.

I have an array:

<<set $tarray to []>>

I have a cell called [[Test]]
I want a button in [[Test]] that pushes a member to the array without leaving [[Test]], so:

[[Push to Array|Test][$tarray.push("member")]]

I’m absolutely certain the array remains unchanged in the above. When ‘push to array’ goes to somewhere else (such as Test2), it works fine, but that’s not what I’m going for. No loops involved.

Hold that thought, I just realised why nothing seems to work.

The first line of the test cell is:

<<set $tarray to []>>

So every time I reload the cell, my array is wiped. FML. I’m dumb ):

Thanks for helping me see the light though!

Ah, yeah, that was my second thought.

For variables that you’re going to use throughout your game, you should initialize them in the “StoryInit” special passage. That will avoid problems like that.

Also, if you really don’t want to leave the passage, then you could do this instead:

<<button "Push to Array">>
	<<run $tarray.push("member")>>
<</button>>

Clicking that button will update the array without leaving the passage.

If you want to update something displayed in the passage at the point the button is clicked, you could use a <<replace>> macro within that <<button>> macro to update the content within a <span> somewhere.

For example:

''Array size:'' <span id="asize"><<= $tarray.length>></span>

<<button "Push to Array">>
	<<run $tarray.push("member")>>
	<<replace "#asize">><<= $tarray.length>><</replace>>
<</button>>

(Note: The <<=>> macro is a short version of the <<print>> macro.)

When you click the “Push to Array” button, it would then add “member” to the $tarray array and update the displayed array size.

Enjoy! :slight_smile: