0 votes
by (150 points)

Hello,

 

I am still quite inexperienced with Twine but I am trying to learn. If I miss out some information below please ask - thank you in advance for even reading! I am using Sugarcube 2.21.0

 

I have created the following code for one of my programs (I am trying to generate random characters):

 

<<set given: $male_names= ["Noah", "Liam", .... "Antoine"]>>

<<set given: $female_names= ["Emma", "Olivia", ... "Alianna"]>>

<<set given: $surname = ["SMITH", "JOHNSON", ... "NOEL", "VANG"]>> 

 

<<set given: $sex = ["male", "female"]>>


<<set $uniformed_commander to {sex: either($sex)}>>
<<if $uniformed_commander.sex eq "female">>
    <<set $uniformed_commander to {name: either($female_names), surname: either($surname), age: random(30,50)}>>
<<else>>
    <<set $uniformed_commander to {name: either($male_names), surname: either($surname), age: random(30,50)}>>
<</if>>

$uniformed_commander.sex
$uniformed_commander.name
$uniformed_commander.surname
$uniformed_commander.age

 

Now this code works - it generates correctly male or female etc. (i have tested it). However when I run it I get the following outcome every time:

 

$uniformed_commander.sex
Ezequiel
ROBERTSON
40

 

Everything works, it goes through randomly but it does NOT give me the first parameter. I tried assigning it to some other value, tried renaming it, tried dropping the $sex and downright putting there male/female. It does not work - what am I doing wrong? 

1 Answer

0 votes
by (23.6k points)

You're forgetting to set sex in your if statement:

<<if $uniformed_commander.sex eq "female">>
    <<set $uniformed_commander to {name: either($female_names), surname: either($surname), age: random(30,50), sex: "female"}>>
<<else>>
    <<set $uniformed_commander to {name: either($male_names), surname: either($surname), age: random(30,50), sex: "male"}>>
<</if>>

Since you are setting the value of $uniformed_commander again down there, the previous value gets overwritten.

by (150 points)

Dear idling,

 

thank you for your answer. Your solution works :)

 

But... Wait, do you mean that everytime I set up some parameters I have to set them all again? Because I did setup the sex before the if statement (<<set $uniformed_commander to {sex: either($sex)}>>). 

If so is tere any other way how I do not reset it but only add additional parameters? Because if I have to always set them up again it is actually quite hard to use them.

by (23.6k points)

To add properties to an existing object you would have to do something like this:

<<set $uniformed_commander.newProperty to "A new property">>

$uniformed_commander.newProperty

 

by (150 points)
Dear Idling,

 

thank you again. This however only adds to confusion.

 

I tried searching for the arrays on the wiki but can't find enough information. Is there any good source I could look at that would explain this?

 

Thank you!
by (23.6k points)

If you set a variable, then set the variable again, the first instance gets overwritten:

<<set $example to "this">>
<<set $example to "that">>

Our first set is going to be overwritten by the second and the variable is going to be "that". The same is true for objects:

<<set $character to {
name: "Tom",
}>>

<<set $character to {
age: 20,
}>>

Since $character as a whole was set for a second time, this will overwrite the first instance. Now $character.name is not defined anymore since that version of the objects was changed later on. If you wanted to just add a property to the object without deleting the stuff you previously defined, you'd have to specifically only set that property but not the entire object:

<<set $character to {
name: "Tom",
}>>

<<set $character.age to 20>>

 

by (150 points)
Idling, thank you very much for you kind answer! I really appreciate it and now everything is clear! :)
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.
...