0 votes
asked by (300 points)
edited by

Okay, I'm not sure if this is possible, but I wanted to know if there was a way to create and name an individual child after a player's character gives birth. (according to this code

As I have it, there I can increase the $kids variable, which reflects the total number of children.

<<if def $birth and $birth lte $gameDate>>\
	It's time for the birthing event!\
	<<if $health gte 50 and $multiples is "singleton">>
	<<set $kids to $kids += 1>>
	<<elseif $health gte 50 and $multiples is "twins">>
	<<set $kids to $kids += 2>>
	<<elseif $health gte 50 and $multiples is "triplets">>
	<<set $kids to $kids += 3>>
	<<elseif $health gte 50 and $multiples is "quadruplets">>
	<<set $kids to $kids += 4>>
	<</if>>
	Congratulations!\
	<<if $health lt 50>>
	<<set $kids to $kids += 0>>
	The baby didn't make it.
	<</if>>
	<<unset $birth>>	
<</if>>

What I'd like to do, in addition to that, is automatically create a new variable that the user then renames to reflect the name of the child and be able to list all the children by name w/ links to their own specific pages in a "home" passage. I also wanted to attach certain characteristics to that variable so they might take after the player's character and/or the npc the player chose to reproduce with. Eye color, hair color, and skin color are some things the player can choose for their character and some things that are defined in the npc parent.

NPC coding

<<set $companion1 to {}>>
<<set   $companion1.name to "sebastian",
		$companion1.skinColor to "olive",
		$companion1.eyeColor to "black",
		$companion1.hairColor to "black",
		$companion1.persona to "quiet"
		>>

Example of what I wanted to try:

(birthing event) > $kids +1 > $kid1 (randomized gender and appearance taken from parents) > congratulations, it's a boy! > rename $kid1 to jim > "Jim was born healthy, and has (his mother's eyes) and (his father's hair)!" 

Then the next time the character is pregnant and gives birth, it would be like:

(birthing event) > $kids +1 > $kid2 (randomized gender and appearance taken from parents) > congratulations, it's a girl! > rename $kid2 to jane > "Jane was born healthy, and has (his father's eyes) and (his father's hair)!" (if all traits from single parent) "She looks just like her father."

Is that too much for twine to manage? I have no clue what macro I should go for, if there is one. 

2 Answers

0 votes
answered by (14.6k points)
selected by
 
Best answer

If you make your setup something like this:

<<set $child to {
name: "",
male: true,
}>>

<<set $children to []>>

The following should work:

<<set $child.male to either(true, false)>>

Congratulations! It's a <<if $child.male>>boy<<else>>girl<</if>>.
Name your child: <<textbox "$child.name" "">> @@#name;@@


<<link "Continue">>
	<<if $child.name == "">>
		<<replace "#name" t8n>>
			@@color:red;Name your child before proceeding!@@
		<</replace>>
	<<else>>
		<<set $children.pushUnique($child)>>
		<<goto "hospital">>
	<</if>>
<</link>>

 

commented by (300 points)

Thank you so so much! I actually combined your and @greyelf's answers to get this

:: StoryInit

<<set $child to {
name: "",
female: true,
birthday: "$birthday",
hair: "$hairColor",
eye: "$eyeColor",
skin: "$skinColor",
persona: "$persona"
}>>

<<set $children to []>>

<<set $companion1 to {}>>
<<set   $companion1.name to "sebastian",
		$companion1.skinColor to "olive",
		$companion1.eyeColor to "black",
		$companion1.hairColor to "black",
		$companion1.persona to "quiet"
		$companion1.preferredHair to "silver"
		>>

:: labor passage

<<if def $birth and $birth lte $gameDate>>\
	It's time for the birthing event!\
	<<if $health gte 50 and $multiples is "singleton">>
	<<set $kids to $kids += 1>>
	<<elseif $health gte 50 and $multiples is "twins">>
	<<set $kids to $kids += 2>>
	<<elseif $health gte 50 and $multiples is "triplets">>
	<<set $kids to $kids += 3>>
	<<elseif $health gte 50 and $multiples is "quadruplets">>
	<<set $kids to $kids += 4>>
	<</if>>
	
<<set $child.female to either(true, false)>>
<<set $child.birthday to $gameDate>>
<<set $child.eyeColor to either($eyeColor, $companion1.eyeColor)>>
<<set $child.hairColor to either($hairColor, $companion1.hairColor)>>
<<set $child.skinColor to either($skinColor, $companion1.skinColor)>>
<<set $child.persona to either($persona, $companion1.persona)>>

Congratulations! It's a <<if $child.female>>girl<<else>>boy<</if>>.
Their hair is $child.hairColor and their eyes are $child.eyeColor. Their complexion is $child.skinColor. Name your child: <<textbox "$child.name" "">> @@#name;@@


	<<if $health gte 50 and $multiples is "singleton">>
<<link "Continue">>
	<<if $child.name == "">>
		<<replace "#name" t8n>>
			@@color:red;Name your child before proceeding!@@
		<</replace>>
	<<else>>
		<<set $children.pushUnique($child)>>
		<<goto "hospital">>
	<</if>>
<</link>>
	<<elseif $health gte 50 and $multiples is "twins">>
<<link "Continue">>
	<<if $child.name == "">>
		<<replace "#name" t8n>>
			@@color:red;Name your child before proceeding!@@
		<</replace>>
	<<else>>
		<<set $children.pushUnique($child)>>
		<<goto "kid2">>
	<</if>>
<</link>>
	<<elseif $health gte 50 and $multiples is "triplets">>
<<link "Continue">>
	<<if $child.name == "">>
		<<replace "#name" t8n>>
			@@color:red;Name your child before proceeding!@@
		<</replace>>
	<<else>>
		<<set $children.pushUnique($child)>>
		<<goto "kid2">>
	<</if>>
<</link>>
	<<elseif $health gte 50 and $multiples is "quadruplets">>
<<link "Continue">>
	<<if $child.name == "">>
		<<replace "#name" t8n>>
			@@color:red;Name your child before proceeding!@@
		<</replace>>
	<<else>>
		<<set $children.pushUnique($child)>>
		<<goto "kid2">>
	<</if>>
<</link>>
	<</if>>
	
	



	<<if $health lt 50>>
	<<set $kids to $kids += 0>>
	The baby didn't make it.
	<</if>>

	
<</if>>

Then I made three more pages the player should be directed to to set the child's gender/name/appearance in the case of multiples. And finally,

You have <<= $children.length>> children.
Their names are: <<= $children.map(function (child) {return child.name;}).join(", ") >>
They look like: <<= $children.map(function (child) {return child.eyeColor;}).join(", ") >>

	<<unset $birth>>

I did have two questions, though. 1: Is there any way to print the information stored in the array? Say in case the player wanted to go back and check what their child's eye color was.

2: Is there any way to list an individual child or now that it's an array can they only be listed in a group? If I wanted to refer to the second-born child, for example, if the player had one:

(Triggered if player has two children) "You returned home to find [child2] was missing."

commented by (14.6k points)
edited by

Arrays are zero-based. So the first element in your array is $children[0], the second one is $children[1], etc.

Accordingly <<= $children[0].eyeColor>> then gives you the eyecolor of your first child and so on. Your example-sentence would be realized like so:

You returned home to find <<= children[1].name>> was missing.



You can now also get rid of your $kids variable and all code refering to it. As @greyelf already pointed out $children.length will give you the number of your children with no further code required.

commented by (300 points)

Ohh, I see. I was trying 

$child.name
$child2.name

Silly me. Thank you so much for your assistance! I really appreciate it. 

0 votes
answered by (120k points)

You could change your $kids story variable from a simple count to an Array of 'kid' objects like so.

/* Within your StoryInit special passage. */
<<set $kids to []>>

/* Within the passage were children are born. */
<<set $kids.push({
	name: "abcde",
	eyes: "blue"
})>>
<<set $kids.push({
	name: "fghij",
	eyes: "green"
})>>


Getting a count of how many kids there are is a simple as

You have <<= $kids.length>> children.


Accessing each of the kids names is more complex, the following is one way to do it.

There names are: <<= $kids.map(function (kid) {return kid.name;}).join(", ") >>

... althought I suggest creating your own custom functions / widgets / macros to do it instead, that way you hide the complexity of code such as the above.

commented by (300 points)
Thank you so much for your help! I wasn't sure how to format it as a widget, but I combined your answer and @idling's below and got something that works.
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.
...