Howdy, Stranger!

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

Creating buttons with dynamic effects

Hi, I've been struggling for the past few days with creating setter buttons for my game's inventory. I need to generate buttons for each item in the inventory that set a $target variable to the value in the $i position of the $inv[ ] array. I know that due to the nature of twine's processing I'm only going to be able to set the last value, which is why I've come to you for answers on this prickly matter.
<<for $i=0; $i<$inv.length; $i++>><br><<print $inv[$i].name>>
	<<if $inv[$i].hasOwnProperty('amountmax')>>
		$inv[$i].amount/$inv[$i].amountmax <br>
		<<button "Set Target">> <<set $target=$inv[$i]>> <</button>>

Thank you for your time!


  • You need to capture the value of $i within the body of the <<button>> macro, so you'll have to <<print>> it. For example:
    <<print '<<button "Set Target">><<set $target = $inv[' + $i + ']>><</button>>'>>
  • Thank you so much! It works perfectly! So, by using print I can nail down variable values before the parser runs through everything, perfect!
Sign In or Register to comment.