Referencing/Using cycled links/variables in Sugarcube 2.34.1

Alright! so! I have this code:

<<set $character to [
"son",
"daughter",
"child"
]>>

You are the <<cycle "$char">><<optionsfrom $character>><</cycle>> of a wealthy and powerful king, though unfortunately[[...]]

And I want things to change through the story whether you shuffle the link to son, daughter, or child. Is there a way to accomplish this?
Sorry if that’s not clear, I had a hard time wording it haha
But yeah, Id love some help. Thanks!

So for example, if you cycled through to “son” the “…” would take you to the next passage and say “You are no prince”, but if you cycled through to “daughter”, it would say “you are no princess”.

When you follow a link to another passage, the $char variable should contain the value that the player had cycled to.

So then you need a way to map “son” to “prince”, “daughter” to “princess”, and “child” to…uh…whatever that is. heir, maybe? There are a couple ways to do that.

The mapping is never going to change, so you don’t have to keep it in a story variable that gets copied into the history every turn: you might want to use SugarCube’s setup object. In your StoryInit passage:

<<set setup.prince to new Map([ ['son', 'prince'], ['daughter', 'princess'], ['child', '...'] ])>>

Then where you want to use it you could say <<print setup.prince.get($char)>>

Or you could use a plain JavaScript object instead of a Map. The syntax is slightly shorter, but it is theoretically possible for some JS library to add properties to every object so you get extra entries, but that’s considered bad form so I suspect it’s vanishingly rare these days. And anyway you’re going to set all the entries you care about, so it would overwrite any default property from wherever. That’s:

<<set setup.prince to { "son": "prince", "daughter": "princess", "child": "..."}>>

And <<print setup.prince[$char]>>

Edit: you might also find good stuff if you look up things about showing pronouns?

1 Like

This worked [: I’m gonna leave it open for a little longer to see if there are other solutions that might work better in terms of the whole story. but I could definitely work with this [:
Because I wanted that selection to change several things in the story. like. I was hoping to be able to do something like
but you are no <<if $char = "son">> prince<<elseif $char = "daughter">> princess<</if>>
Almost like saving the choice to reference it in conditional statements later. so I can use that choice several times throughout.
Sorry that was long winded. I just wanted to see if that made it more clear, less clear, or clear that its impossible to do xD

1 Like

Like is there a way to basically store your choice into its own true or false value?

If you’re going to make it more complex than what Josh suggested, it might be easier to just use an object instead of a map. And then you can set the properties as they get defined in the story.

:: Start

<<set $char = {
    name: "default",
    relation: "child", 
    role: "nothing",
    hair: "black",
    gender: "unspecified"
}>>

[[I'm your son!|next][$char.relation = "son"]] 
[[I'm your daughter!|next][$char.relation = "daughter"]]


:: next

<<if $char.relation == "son">>
    <<set $char.role = "prince">>
    <<set $char.gender = "male">>
<<elseif $char.relation == "daughter">>
    [etc...]

You don’t have to actually define any of the members ahead of time like this. It just helps track errors because returning a default value and returning a value of undefined would be two different errors

As a side note, just in case this wasn’t just a typo, this code is no good: <<if $char = "son">> It should be ==, ===, or is. A single = is only for setting values, not comparing.

1 Like

Thank you! and it was more just pseudocode to communicate the idea. all very good points!

Another thing you could do is use a template. I haven’t actually used this myself, so forgive me if I get something wrong here.

Basically in your javascript section you would put this:

Template.add('them', function() {
    var gender = State.variables.gender;
    if (gender == "unspecified" || gender == undefined) return "them";
    return (gender == "male") ?  "him" : "her";
});

You use templates by placing a ? in front of the name while using them inline. So it’d be like:

This is my <<=$char.relation>>. I'm so proud of ?them!

Which would return:
This is my son. I'm so proud of him!
or
This is my daughter. I'm so proud of her!

You get the idea.

1 Like

Thank you! I am combining the ideas as the situation calls for basically, and its working great [: thank you both for your time!