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: