I am using the most current version of Sugarcube and Twine 2 and I have an object set up that has an array as one of its properties, and now I need to pull out a specific element in that array and I'm having problems.
Code below.
<<for $ca=0;$ca<4;$ca++>>\
<<switch $chararray[$ca]>>\
<<case "chief">>\
<<if $chief.where[$chief.plot] == $actloc>>\
Continue the Chief's plot at this location by talking to the $chief.who[$chief.plot].\
<</if>>\
When I use this particular notation $chief.where[$chief.plot], I get back the full array of $chief.where, plus the current value of $chief.plot, not the specific element within the $chief.where that is located at the $chief.plot place, which is what I want.
Any suggestions?
Comments
Since you haven't shown what $chief.where or $chief.plot are, it's a bit difficult to offer specific advice.
That said, are you sure you don't mean $chief.who and $chief.plot from the following line: If so, the answer may be found by reading the naked variable markup docs. In summary, that's not a supported construct, so you'll have to use one of the print macros—e.g. <<print>> or <<=>>. For example:
The temp variable thing explains something else.
However, the main problem is with the if statement, and to give you more info, let me provide the code.
This code is set up in the Widgets passage, so it should be available globally. And $chief.plot is a number between 0 and 6, set by the player's progress through the game.
So, $chief.who and $chief.where for $chief.plot = 3 should be "ch", and "Chief", respectively. But that's not what I'm getting.
The StoryInit special passage exists to initialize variables, among other things.
Are you reusing $NPC for other NPCs? If so, you may be overwriting your properties. By simply assigning $NPC to $chief you're making a copy of the object reference, not the object. If you then make a similar assignment using $NPC within the same turn, you're simply copying the same reference again.
If $chief should be distinct, then instead of this: Do something like the following:
Though, the way you're handling its title property, makes it look like you might be better off with a constructor or factory function—hard to say at the moment.
What are you getting? I ask because you're not printing/logging anything in the example code you gave, save for one member of $chief.who.
Have you tried adding some console logging? For example: