0 votes
by (370 points)
I'm using Twine2.1.3, and at the moment I have no idea how to make random events with Sugarcube 2. I tried converting the Harlowe 2's version of random events to Sugarcube 2's, but that didn't worked. So what am I doing wrong? I tried looking through the old Twine Forums, but there's little to work with.

Anyone know what to do?

1 Answer

+1 vote
by (2.1k points)

If I were making something with several random outcomes, I'd use the <Array> methods that are available within SugarCube. 

The methods are located here!

If you'd just like to pick something at random from a certain array you can use <Array>.random()

The example that's given is:

// Given: $pies = [ "Blueberry", "Cherry", "Cream", "Pecan", "Pumpkin" ]
$pies.random()      → Returns a random pie from the whole array
$pies.random(2)     → Returns a random pie from the range 0–2 (i.e. [ "Blueberry", "Cherry", "Cream" ])
$pies.random(1, 2)  → Returns a random pie from the range 1–2 (i.e. [ "Cherry", "Cream" ])

 

Given the array $pies, you can randomly pull one of the entries. If you want a random event to happen in your story you can have something like this:

// First make your array
<<set $pies = [ "Blueberry", "Cherry", "Cream", "Pecan", "Pumpkin" ]>>\
\
// Now set a temporary variable to store the random selection
<<set _storyEvent1 = $pies.random()>>\
\
//And have an <<if>> statement which displays the event you want to happen
<<if _storyEvent1 == "Blueberry">>\
	<<print "You're hit in the face with a blueberry pie!">>
<<elseif _storyEvent1 == "Cherry">>\
	<<print "A cherry pie flies over your head, what a waste you love cherry!">>
// etc...
<</if>>\

If you put a \ in the empty lines and lines with just code you can prevent a lot of unnecessary spacing in the passage. That's just how I do things though, it's up to you.

by (370 points)
I've seen that explanation before I think. I understand the gist of arrays, but I'm not sure where to display the coding. Does the  $pies.random() go into the StoryInit? Or in the same passage as the making the array?  Could you give more examples if possible?
by (2.1k points)

Depending on your needs, you can do it wherever you want. <Array>.random() is a function that returns a random element in the array. So, we use a variable to store it while we use it.

// Example using $pies again

// Here we create an array with 5 elements and store it in _pies as a temporary variable. 
// Each one is a string which we know contains a flavor of pie. 
// Depending on whether we will use this past this passage we can make it a story variable ($pies) or a temporary variable (_pies) (which goes away after we leave the passage).

<<set $pies = [ "Blueberry", "Cherry", "Cream", "Pecan", "Pumpkin" ]>>

// Now, we need to pick a random flavor of pie.
// We'll need to check what flavor we got several times so we'll also store it in a temporary variable.
// This uses the .random() function on our array $pies to pick a random flavor, then stores it in _randomFlavor

<<set _randomFlavor = $pies.random()

// NOTE: We can also use the SugarCube function either() and it would look like 
// <<set _randomFlavor = either($pies)>>

//Then we can use that stored random variable in an if statement to end up with a result.

<<if _randomFlavor == "Blueberry">>
	//Something here happens related to blueberry
<<elseif _randomFlavor == "Cream">>
	//Something here happens related to cream
//etc.
<</if>>

 

We use the $pies.random() inside the <<set>> macro so that we can store the result in a variable. Another example is something like the following. 

<<set $pies = [ "Blueberry", "Cherry", "Cream", "Pecan", "Pumpkin" ]>>

// If we use a <<print>> macro several times with $pies.random() we'll get a random result every time.

<<print $pies.random()>>
<<print $pies.random()>>
<<print $pies.random()>>
<<print $pies.random()>>

// That might output something like

Pumpkin
Cream
Pecan
Pumpkin

// Every time we use .random() on $pies it selects a new random pie flavor.

 

You probably want to create and use the variable containing the random pie flavor in the passage that you're using it. If you make your variable in StoryInit, it will be the same every time you call on it. That's because it will have selected a random flavor when the story started up and stored it. For example

If we have this in our StoryInit:

<<set $pies = [ "Blueberry", "Cherry", "Cream", "Pecan", "Pumpkin" ]>>
<<set $randomFlavor = $pies.random()>>

then when we call on it, it will be the same every time.

// Using these print statements, the variable was already created in StoryInit. 

<<print $randomFlavor>>
<<print $randomFlavor>>
<<print $randomFlavor>>
<<print $randomFlavor>>

// That would now output something like this 

Pecan
Pecan
Pecan
Pecan

// Or if you start the story up again and do the same thing.

Pumpkin
Pumpkin
Pumpkin
Pumpkin

That's because we ran $pies.random() when the story started, so what we got was already stored.

by (370 points)
Thank you for answering my questions! Printing randomFlavor works just fine!

However I'm still having difficulty using your code where the array is used to pull randomly. I copied the whole thing and paste it into a passage, then play the passage and all I get is a blank passage sometimes. Other times I get the Blueberry event or the Cherry event.

Is there an event0 that I need to fill in? Or is there more to the random event code that I don't know of?
by (63.1k points)

You could try Array.random($pies). 

I.e. 

<<set $randomFlavor to Array.random($pies)>>

I'm not sure why the other wouldn't be working for you, though. 

by (2.1k points)

That code isn't 100% complete, if you notice the //etc. I meant to imply that you should have an elseif case for each of the flavors of pie! I just didn't want to write one out for each one. I apologize for the confusion. Here's a complete example with no comments in it for reference's sake.

<<set $pies = [ "Blueberry", "Cherry", "Cream", "Pecan", "Pumpkin" ]>>\

<<set _randomFlavor = $pies.random()>>\

<<if _randomFlavor == "Blueberry">>\
	<<print "You're hit in the face with a blueberry pie!">>
<<elseif _randomFlavor == "Cherry">>\
	<<print "A cherry pie flies over your head, what a waste you love cherry!">>
<<elseif _randomFlavor == "Cream">>\
	<<print "A simple cream pie hits the wall behind you, making a simple splatter.">>
<<elseif _randomFlavor == "Pecan">>\
	<<print "A pecan pie tin rolls along the floor, just stuffing and chunks of crust left in it.">>
<<else>>
	<<print "A pumpkin pie takes flight and ruptures mid-air, crumbling all over you!">>
<</if>>\

 

by (370 points)
Thank you so much! It works now!
...