Looking for help with how to create a particular variable in Twine Sugarcube.

Tigger254

New Member
Jan 27, 2018
13
10
Hello everyone

I am new to Twine and not well up on programming, i have managed to find sources of information on how to create other variables and such but i cannot find any source of information about how to create the type of variable(or maybe conditional) for what i want to do.

Thanks in advance.
 

HiEv

Member
Sep 1, 2017
384
785
Nobody is going to be able to help you if you don't describe what you want to do.

That said, I'd recommend reading through the to see if that helps (you can also download the latest version of SugarCube from there). Also, SugarCube is largely based on JavaScript, so if you know how to do it in JavaScript, then you likely already know how to do it in SugarCube.

If that doesn't help, there's also the where you can ask for help (don't forget to put the Twine and SugarCube versions you're using in the tags there). Make sure you explain exactly what you're looking for.

Hope that helps! :)
 

Tigger254

New Member
Jan 27, 2018
13
10
The variable or conditional that i am trying to achieve is where i use one variable to set a random number say between 1 & 10 and then have each number with an outcome to either modify another variable or give a passage link which will be determined by the variable setting the number randomly, something like a RAGS conditional where it is if variable is 1 do an action, if it is 2 do a different action and so on.

Hope i have managed to explain what i am after well enough.:oops:

I will also try and register on the Twine Q&A site.:)
 

Epadder

Programmer
Game Developer
Oct 25, 2016
568
1,064
Now I'm not a Twine expert... but as I've been picking through Adam and Gaia, I recognize some things from general programing paradigms.
Code:
<<set $Var to random(1, 10)>>
<<if $Var is 1>>
      <<goto "Some Passage">>
<<elif $Var is 2>>
     <<set $OtherVar to somevalue>>
Continue the pattern till you have enough entries to cover the possibilities of the randomly generated #s or if your okay with dud #s can have an else at the end to catch any result you don't care about.

Every time you need to make a decision like this you will have to set the variable your using to random(min,max).

This is at least what I know works.
 

HiEv

Member
Sep 1, 2017
384
785
For what you described, the best way to do it is like this:
Code:
<<switch random(1, 10)>>
    <<case 1>>
        (code goes here for a roll of 1)
    <<case 2 3>>
        (code goes here for a roll of 2 or 3)
...etc...
    <<default>>
        (code for any cases not covered above)
<</switch>>
See the and the for details.

Code:
<<set $Var to random(1, 10)>>
<<if $Var is 1>>
      <<goto "Some Passage">>
<<elif $Var is 2>>
     <<set $OtherVar to somevalue>>
That almost works, though I'd make a few changes and corrections:

Code:
<<set _Var to random(1, 10)>>
<<if _Var is 1>>
      <<goto "Some Passage">>
<<elseif _Var is 2>>
     <<set $OtherVar to somevalue>>
<<else>>
    <<print "Some text">>
<</if>>
I used "_Var" instead of "$Var" because you likely only need that value for this passage, so a temporary variable should be used instead of a story variable.

Also, it's "elseif", not "elif", and there needs to be a "<</if>>" at the end (you might see "<<endif>>" in older code, but that usage is depreciated).

That said, for this particular case, a <<switch>> works better than an <<if>>.
 

Vigilant1

New Member
Apr 25, 2018
7
5
I know I'm a bit late to the party, but I think the easyest way to achieve what you want is by using an array, where all the array items are the names of the nodes you want to visit.
Something like:
<<set $EventPool = []>>
<<set $EventPool.push("outcome1","outcome2","outcome3")>>
<<set $passageName= $EventPool.random()>>
<<goto $passageName>>
 
  • Like
Reactions: CCCC2C

HiEv

Member
Sep 1, 2017
384
785
I know I'm a bit late to the party, but I think the easyest way to achieve what you want is by using an array, where all the array items are the names of the nodes you want to visit.
Something like:
<<set $EventPool = []>>
<<set $EventPool.push("outcome1","outcome2","outcome3")>>
<<set $passageName= $EventPool.random()>>
<<goto $passageName>>
Tigger254 also described wanting to set variables, so that code won't help with that.

However, if you did only want to show a random link to a new passage, this is much shorter:
Code:
<<set _lnk = ["outcome1", "outcome2", "outcome3"].random()>><<link _lnk _lnk>><</link>>
That uses a temporary variable (_lnk) and then uses the to create a link, since using the would either create an unused "garbage passage" or a "missing branch" marker in Twine 2.

Also, the should be used with caution, because any code after that will be executed as normal before going to the next passage.
 
  • Like
Reactions: Vigilant1

Vigilant1

New Member
Apr 25, 2018
7
5
I'm no coder, but I see no reason why he wouldn't be able to set variables that way too- just link to a node, that plays the role of a variable setter block(you set all your conditions and variables in it and auto exit it with goto).
Using the shortened version from above will work great with that strategy too.
 

HiEv

Member
Sep 1, 2017
384
785
I'm no coder, but I see no reason why he wouldn't be able to set variables that way too- just link to a node, that plays the role of a variable setter block(you set all your conditions and variables in it and auto exit it with goto).
You shouldn't link to passages just to set variables, because creating passages merely for the purpose of setting variables isn't good coding style. It unnecessarily increases the number of passages, the size of the story history, and the complexity of the code. This makes it much more difficult to debug as well.

Worse yet, it breaks the "Back" button. If you go through one of those passages, and then try to hit "Back", that passage will then re-set those variables and the <<goto>> will send you forward again to where you just were.

So, yes, it could be done that way, but there are far better ways to do it.
 

Vigilant1

New Member
Apr 25, 2018
7
5
You shouldn't link to passages just to set variables, because creating passages merely for the purpose of setting variables isn't good coding style. It unnecessarily increases the number of passages, the size of the story history, and the complexity of the code. This makes it much more difficult to debug as well.
I'd argue most of those points. Yes, there will be an increase in number of pasages and history size, but in the given example of 10 possible outcomes, some of which would be passage links, it would be insignificant. I'm not saying you should create setter nodes every time you decide to change a variable in general. That would indeed be strongly unadvisable.
I don't agree on the "more difficult to debug" point at all, at least for what I have in mind, but I see no point of argiung about it.

Worse yet, it breaks the "Back" button. If you go through one of those passages, and then try to hit "Back", that passage will then re-set those variables and the <<goto>> will send you forward again to where you just were.
I admit you have a valid point here. I did not think of that- I always disable my back button. Hate the functionality.

I'd also want to use the opportunity to thank for:

<<set _lnk = ["outcome1", "outcome2", "outcome3"].random()>><<link _lnk _lnk>><</link>>
I'd probably use it at some point.