Howdy, Stranger!

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

Arrays as object properties

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

  • First. If $ca doesn't need to be part of the history, use a temporary variable—which would be _ca.

    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:
    Continue the Chief's plot at this location by talking to the $chief.who[$chief.plot].\
    
    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:
    → Using <<print>>
    Continue the Chief's plot at this location by talking to the <<print $chief.who[$chief.plot]>>.\
    
    → Using <<=>>
    Continue the Chief's plot at this location by talking to the <<=$chief.who[$chief.plot]>>.\
    
  • That's part of the solution.

    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.
    <<set $NPC to {
    			title: $namelist.pluck(0),
    			name: "",
    			ismale: 0,
    			plot: 0,
    			who: [],
    			where: []
    
    <<set $chief to $NPC>>
    <<set $chief.name to "Elijah">>
    <<set $chief.ismale = 1>>
    <<set $chief.who = ["Chief","Chief","Chief","Chief","Chief","Apprentice","Chief"]>>
    <<set $chief.where = ["ch","lh","ch","ch","sq","lh"]>>
    

    So, $chief.who and $chief.where for $chief.plot = 3 should be "ch", and "Chief", respectively. But that's not what I'm getting.
  • NPC does have a closing bracket, it just got erased in the editing of the code in the post.
  • housellama wrote: »
    This code is set up in the Widgets passage, so it should be available globally.
    Unless the shown code is actually within a widget, do not do that. Only widgets should be placed within a widget passage.

    The StoryInit special passage exists to initialize variables, among other things.

    housellama wrote: »
    <<set $NPC to {
    			title: $namelist.pluck(0),
    			name: "",
    			ismale: 0,
    			plot: 0,
    			who: [],
    			where: []
    
    <<set $chief to $NPC>>
    <<set $chief.name to "Elijah">>
    <<set $chief.ismale = 1>>
    <<set $chief.who = ["Chief","Chief","Chief","Chief","Chief","Apprentice","Chief"]>>
    <<set $chief.where = ["ch","lh","ch","ch","sq","lh"]>>
    
    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:
    <<set $chief to $NPC>>
    
    Do something like the following:
    <<set $chief to clone($NPC)>>
    

    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.

    housellama wrote: »
    So, $chief.who and $chief.where for $chief.plot = 3 should be "ch", and "Chief", respectively. But that's not what I'm getting.
    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:
    		<<case "chief">>\
    			<<run
    				console.log('[case chief] _ca:', _ca);
    				console.log('\t$chief.where:', $chief.where);
    				console.log('\t$chief.plot:', $chief.plot);
    				console.log('\t$actloc:', $actloc);
    			>>
    			<<if $chief.where[$chief.plot] == $actloc>>\
    				Continue the Chief's plot at this location by talking to the $chief.who[$chief.plot].\
    			<</if>>\
    
Sign In or Register to comment.