0 votes
by (1.1k points)
edited by

When defining a new object within an array something seems wrong.



<<set $children = []>>

<<set $mom to {
	name: 'mom',
	hair: 'blonde',
	skin: 'white',
	eyes: 'blue'

<<set $dad to {
	name: 'dad',
	hair: 'brown',
	skin: 'white',
	eyes: 'brown'

In the passage...

<<set _hair to [$mom.hair, $dad.hair]>>
<<set _skin to [$mom.skin, $dad.skin]>>
<<set _eyes to [$mom.eyes, $dad.eyes]>>

<<set $children.push( { 
    hair: _hair.random(), 
    skin: _skin.random(), 
    eyes: _eyes.random() 
} )>>

<<print $children[0].hair>>

<<print $children[1].skin>>

<<print $children[2].eyes>>

The settings are set in StoryInit, but only the first variable works normally, the others do not....


"Brown hair"

"Error: <<print>>: bad evaluation: Cannot read property 'skin' of undefined"

"Error: <<print>>: bad evaluation: Cannot read property 'eyes' of undefined"

Is it likely that I'm doing something wrong? It seems like a problem to set the final object with all variables. As I'm a beginner this happens a lot.

1 Answer

0 votes
by (64.2k points)
selected by
Best answer

Carefully check the indices you're using with $children.  You show that you add a single member to the array, yet you attempt to access the first (index 0), second (index 1), and third (index 2) members.

by (1.1k points)
Thank you! It was a small detail... I need to improve my vision.
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.