0 votes
by (150 points)

I'm trying to write a simple RPG, and I've encountered a problem.

I have an equip screen set up like this:

<<for _slot range $activeslots>>
	<<link "Unequip _slot.name" "$passage">>
		<<Remove _slot>><</link>><br>

When executed, it outputs something like this:

Remove helmet
Remove jacket

But whatever line I click, it only unequips the last item in the list.

Now, I understand why it happens; when generating lines, the program iterates through every non-empty slot in succession, but by the time I click on it, the _slot is already set to the last non-empty slot.

The question is, is there a way to fix it? Something that will allow every line to "remember" what value of _slot it was generated with, and call Remove with correct args?

Thanks in advance!

1 Answer

+2 votes
by (44.7k points)

The problem is that, by the time the user can click, the value of "_slot" is always going to be the last value.  This is where the <<capture>> macro comes in handy.  Try this:

<<capture _slot>><<for _slot range $activeslots>>
	<<link "Unequip _slot.name" "$passage">>
		<<Remove _slot>><</link>><br>

Now that will "capture" the value of "_slot", so that when the user clicks the link, it will use the value that "_slot" had when the code originally executed, instead of the value that "_slot" currently has.

Enjoy!  :-)

by (150 points)

Thanks, that's exactly what I was looking for!

by (159k points)

Generally the <<capture>> macro is placed within the <<for>> macro's body like so.

<<for _slot range $activeslots>>
	\<<capture _slot>>
		<<link "Unequip _slot.name" "$passage">>
			\<<Remove _slot>>