A variable that checks the value of another variable and randomly chooses from several lists of pre-determined expressions

This is for SugarCube

I want to have $variableOne to check the value of $variableTwo, and depending on the outcome, express its content in this way:

$variableOne should have 3 sets of data it can express:

set 1 {A, B, C} corresponds to $variableTwo == 1
set 2 {a, b, c} $variableTwo == 2
set 3 {x, y, z} $variableTwo == 3

So, whenever $variableOne is called, it should check which list it should draw from, and then express a single object from said list, chosen randomly.

As I understand it, what you really want here is a widget. Simply referring to a variable’s name will return whatever that variable is currently set to. You’ll need something that will set that variable and output the result:


<<widget "var1">>
	<<switch $var2>>
		<<case 1>>
			<<set $var1 = either(a, b, c)>>
		<<case 2>>
			<<set $var1 = either(d, e, f)>>
		<<case 3>>
			<<set $var1 = either(x, y, z)>>
	<</switch>>
	$var1
<</widget>>

Place this widget (changing the values in the ‘either’ function to whatever you actually need them to be) in a passage with the tag ‘widget’. Then, where you need the random value to appear, simply call the widget: <<var1>>

1 Like

Thanks mate. This does exactly what I wanted. I can’t seem to tag a passage as ‘widget’. It seems to be an invalid tag, producing an error. But the code works regardless, so thanks again!

I can’t seem to tag a passage as ‘widget’

The Passage you’re adding the special widget tag to can not be one of SugarCube’s special passages!

eg. you can not add the widget tag to the StoryInit special passage.

Additionally, make sure you aren’t including the ‘’ around the word ‘widget’. I only included those to seperate it from the other text; sorry about the confusion if that was your issue.

Okay guys i figured it out. I was mistakenly pulling up the widget via a link. I understand now. Thank you both!