Making a random choice happen for a variable?

If you are requesting technical assistance with Twine, please specify:

Twine Version:2.30
Story Format: Sugarcube

So I have reached a point in my RPG story game where:

I want to simulate / cause a variable to randomly be assigned to two possible passages.

Is this some way to go about it?

<<set $widget to random([[passage a]], [[passage b]]) >>

You cannot assign links to variables. Assign the passage name instead, then use the variable in a link or whatever you had planned.

Also, thatā€™s not what the random() function does. You probably want either().

For example:

<<set $passage to either("passage a", "passage b") >>
1 Like

Iā€™m just wondering how Iā€™d make it so that variable ā€˜appearsā€™ inside the ā€˜eitherā€™ passage so itā€™s like someone finds it or does not, depending on that.

Like maybe I should set the variable to being ā€˜trueā€™ or something?

What Iā€™ve done right now is Iā€™ve put:

<<if $passage is true>>The Passage is here!
You spot the Passage!
<<else>>No sign of the passage here!
<</if>>
<<if $passage>>The Passage is here!
You spot the Passage!
<<else>>No sign of the passage here!
<</if>>

Iā€™m struggling. The code either makes it not appear in both and appear in both.
The premise is basically someone is hunting for the right thing and I want it to be either one or the other, but not both. So the player has a 50/50 chance of finding it (hence why I wanted to try and get the random thing used).

Assuming youā€™re using TMEā€™s example to set $passage, the first of your codes snippets will always fail because a string assignment isnā€™t going to equal true. The second one will always pass because any string that isnā€™t empty (not "" in other words) is truthy, so <<if "bleh">> will pass.

So what you need to do is something like

<<if $passage is "passage a">>

Be sure to either match the case exactly or use $passage.toLowerCase() instead.

First of all thereā€™s two variable types.

  1. Variables with a $ (example: $passage) are story variables, once set they will be part of the whole story, or until unset.
  2. Variables with a _ (example: _passage) are passage variables, they are set inside a passage and automatically deleted when the user hit a link or button leaving the passage.

If I understand you correctly, you want the player to be sent to a random passage. Hereā€™s one way to do so.

Given Passage 1 and Passage 2 created.

<<set _advance to random(1,2)>>
<<if _advance == 1>><<set _passage to "Passage 1">><<else>><<set _passage to "Passage 2">><</if>>
<<link "You venture forth" _passage>><</link>>
1 Like

Ok folks Iā€™m trying but this is still being a PITA.
Iā€™m not getting errors but itā€™s just showing the ā€˜elseā€™ bit and never the true bit.

<<set $passage to either("Passage1", "Passage2") >>

Which corridor do you take?

[[Head down the left corridor!|Passage1]]

or

[[Head down the right corridor!|Passage2]]

You follow the bright corridor that winds around on itself and see....
<<if $passage is "passage1">><<set $passage to true>><</if>>
<<if $passage is true>>The thing is here and up ahead of you!

You race closer to try and stop it!
[[Continue|Magazine - Agent Sighted!]]
<<else>>No sign of him here!
[[Continue|Magazine - Hear the Agent!]]<</if>>
You follow the dark corridor that winds around on itself and see....
<<if $passage is "passage2">><<set $passage to true>><</if>><<if $passage is true>>The thing is here and up ahead of you!

You race closer to try and stop it!
[[Continue|Magazine - Agent Sighted!]]
<<else>>No sign of him here!
[[Continue|Magazine - Hear the Agent!]]<</if>>

I think itā€™s just that youā€™re setting the variable to ā€œPassage1ā€ (capital ā€˜Pā€™) and then checking if itā€™s ā€œpassage1ā€ (lowercase ā€˜pā€™).

2 Likes

Obviously I didnā€™t understand you correcty.

As a side note, if you plan to keep the same flavour text for each corridor, and if they point to the same exits, you donā€™t need two passages, but only one.

Which corridor do you take?

[[Head down the left corridor!|Corridor]]

or

[[Head down the right corridor!|Corridor]]
<<set _passage to either(1,2) >>
You follow the bright corridor that winds around on itself and see....
<<if _passage==1>>The thing is here and up ahead of you!

You race closer to try and stop it!
[[Continue|Magazine - Agent Sighted!]]
<<else>>No sign of him here!
[[Continue|Magazine - Hear the Agent!]]<</if>>
1 Like

I think that was the last piece of the puzzle!

Thanks all for helping, I should be ok with this now. :slight_smile: