<<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?
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
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.
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.