Please specify version and format if asking for help, or apply optional tags above:
Twine Version: 2
Story Format: Sugarcube
Hey there, I’m helping my brother with a Twine game and I’ve run into what is probably a rookie error with arrays/objects.
I’m defining a list of companions with an array of actions and I need to loop through these actions, however whenever I try to access the length variable it doesn’t exist (and just prints out the variable name). Here’s my code:
<<set $companions={
"brother":{
"hp":100,
"name":"brother",
"actions":{
0:{
"name":"punch"
},
1:{
"name":"taunt"
}
}
},
"sister":{
"hp":80,
"name":"sister",
"actions":{
0:{
"name":"kick"
},
1:{
"name":"punch"
}
}
}
}>>
$companions['brother']['actions'].length /* Prints out the variable string not the actual length of the actions array! */
<<set _companion="brother">>
$companions[_companion]['actions'].length /* Prints out the variable name rather than an int length */
<<for _i=0; _i lt $companions[_companion]['actions'].length; _i++>>
Action is: $companions[_companion]['actions'][_i]['name']
<</for>>
Basically it never enters the for loop.
There seems to be some weirdness with multidimensional arrays in sugar cube, but even if I simplify it and just try to get the length of the array inside an array it returns nothing. Is there another way to get the length of an array in Sugarcube 2?