How to share a variable between rules

Hi all,

I’m trying to implement some rules so as to generate text from tables.

I’ve got one rule to generate a number based on some randomness, and another rule to take that number and use it with a bit more randomness to select text from tables.

(The aesthetic and game design intent for all of this is in some of my other posts, just to avoid debating that particular ‘why!’ in this thread!)

What I’m finding is that the variable from the first rule isn’t carrying over to the second rule.

I haven’t yet figured out why, but I’ve added some description to the rules so as to make sure it’s doing what I think it’s doing. It shows that the first rule works, and generates a number, but the second rule isn’t using that number. (The number it gives is zero.)

I feel like I want to keep these rules separated because, eventually, I want to use the second rule to generate multiple selections of text, but it will have been based on that earlier number. (I don’t want to re-roll the initial number from scratch every time.)

I’ll add the code to the next post so that this one comment doesn’t get too long!

Volume 1 - Locker Generation


Ideology is a number that varies.

This is the random-ideology rule:
	Let ideology be a random number between 1 and 3;
	If a random chance of 1 in 2 succeeds:
		now ideology is ideology plus 7;
	say "Ideology is [ideology]."


This is the fill-locker rule:
	let temp-locker-stuff be text;
	If a random chance of ideology in 11 succeeds:
		say "[ideology]";
		say "jock succeeds";
		choose random row in Table of Jock Details;
		now temp-locker-stuff  is "[Jock Detail entry]";
	otherwise:
		say "[ideology]";
		say "nerd succeeds";
		choose random row in Table of Nerd Details;
		now temp-locker-stuff  is "[Nerd Detail entry]";
	now the description of locker-content is "[temp-locker-stuff]";
	say "[temp-locker-stuff]."
	


Table of Jock Details
Jock Detail
"Jock1"
"Jock2"
"Jock3"
"Jock4"
"Jock5"
"Jock6"
"Jock7"
["Ambiguous"
"Ambiguous"
"Ambiguous"]

Table of Nerd Details
Nerd Detail
"Nerd1"
"Nerd2"
"Nerd3"
"Nerd4"
"Nerd5"
"Nerd6"
"Nerd7"
["Ambiguous"
"Ambiguous"
"Ambiguous"]



Volume 2 - Scenario

Book 1 - Declarations

[A locker-content is a kind of thing.]

Book 2 - Rooms

Chapter 1 - detention hall

[figure out implementing rules]

When play begins:
	abide by the random-ideology rule.
	
Before examining the locker:
	abide by the fill-locker rule.

Detention Hall is a room. "You are in a stereotypical high school detention hall. (Stereotypical to your individual age and background!)"

A locker is in Detention Hall.

In the locker is a locker-content.

If it’s two rules in the same rulebook, you can do:

The foo rulebook has a number called bar.

Otherwise, use a global.

bar is a number variable.

then both rules (as well as any other code anywhere) can access it.

1 Like
This is the random-ideology rule:
	let ideology be a random number between 1 and 3;

That creates a fresh ideology variable local to the rule. Try

This is the random-ideology rule:
	now ideology is a random number between 1 and 3;
4 Likes

You could also make the first rule a phrase that returns the ideology value - see relevant bit of the docs.

(The issue you’re running into here, by the way, is that “let” just creates a local variable that only sticks around for the duration of the rule).

2 Likes

(I think it’s a little unfortunate that let <x> be <v> can do double-duty within code blocks both to declare that there’s a variable x and as an alternative to now to assign it after the fact. A habit I follow is to use let only to create new temp variables, and I always use now thereafter.)

1 Like

That worked, thank you! Now I’m going to work on either tweaking the rule or finding a way to invoke it multiple times to create a list.

Maybe that will be adding the results of the fill-locker rule to a different table?

Thank you for pointing this out to me. This ‘decide’ function might unlock some other concepts for me that are appropriate to what I’m going for!

Zed Lopez solved the immediate issue, but later on I want to implement the ability for the game to ‘score’ you or perhaps ‘decide’ if you were correct or not in making a certain choice about the information presented, and the decide function might connect very well!

1 Like