0 votes
by (160 points)

I try to make an inventory where I can delete Items on click, which would look ingame like this:

Items:

Bread
Fish
Salad
Chocolate

and when I click on an item, it disappears. (Actually that's a trash section where I can throw Items away)

The code I've written for that looks like this:

Items:

<<nobr>>
	<<for _i = 0; _i < $inv.length; _i++>>
		<<link $inv[_i].name "inventory:trash">><<set $inv.deleteAt(_i, 1)>><</link>>
		<br>
	<</for>>
<</nobr>>

But when I click on something, the second items disapperas. How can I do it that the clicked item disappears?

1 Answer

+1 vote
by (63.1k points)

The two core issues are that you're using <Array>.deleteAt() method incorrectly, all parameters are indices to remove, and you're not capturing the value of your loop variable (via the <<capture>> macro) even though you're using it within an asynchronous macro (i.e. <<link>>), which will not yield the value you expect when the player activates it (except accidentally).

Based on your example, it should look something like the following:

	<<for _i = 0; _i < $inv.length; _i++>>
		<<capture _i>>
			<<link `$inv[_i].name` "inventory:trash">>
				<<run $inv.deleteAt(_i)>>
			<</link>>
		<</capture>>
		<br>
	<</for>>

 

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...