Last updated: 21 May 21 13:11:23 (UTC)

Arrays in Twine

https://www.glorioustrainwrecks.com/node/5034

Javascript is supposed to be able to read the twine variables.

/% the Array#join() method combines all array elements into a single string, with each element separated by the argument given %/\

<<= $inventory.join(', ')>>

/% we use the Array#push() method to add new items to our inventory array %/\ You find yourself inside a small room. In the corner, you see a sword, and decide to pick it up.

<<run $inventory.push(‘a sword’)>>
 [[Continue|hallway]]

*To print the entire contents of an array in order (usually for debug purposes): <<print $inv >>

*To see if the value “Magic Knife” is inside an array: <<if $inv.indexOf(“Magic Knife”) gte 0>>

*To get the number of values inside an array: <<set $size = $inv.length >> *To get the first value inside an array: <<set $first = $inv[0] >> *To get the last value inside an array: <<set $first = inv[inv[inv[inv.length - 1] >>

var varNames = [“name1”,“name2”,“name3”]; for (var i=0;i<varNames.length;i+=1){   window[varNames[i]] = 0; }