Assigning random colours to off-stage things

I’m trying to figure out how to apply random colours to off-stage items that I’m creating.

(Building off the last solution I received over here: Figuring out how to create multiple things? - #3 by jasonmehmel)

I can create the ‘off stage’ pens, and I can give them a colour value, but I can’t figure out how to make them random.

[code]
Colour is a kind of value. The colours are blue, yellow, indigo and violet.

A pen is a kind of thing. A pen has a colour. Before printing the name of a pen: say "[colour] ". Before printing the plural name of a pen: say "[colour] ". Understand the colour property as describing a pen.

There are 10 pens.

When play begins:
Repeat with item running through off-stage pens:
if a random chance of 1 in 2 succeeds:
now item is in locker.

[code]

I tried putting in a line: ‘let the pen be a random colour’ in the ‘when play begins’ section, and I tried putting it after the ‘A pen has a colour’ statement, but I think I’m missing something in terms of when the random assignment should be called.

(I’ve also been examining the code here for random colours, but haven’t found a way to apply it to my situation. [6L38] Randomly generate requested items during play? - #4 by otistdog)

This should work:

When play begins:
	Repeat with item running through off-stage pens:
		Let hue be a random colour;
		Now the colour of item is hue;
		if a random chance of 1 in 2 succeeds:
			now item is in locker.

(BTW you can post code using the little button that looks like </>, or adding three backticks above and below the code excerpt).

3 Likes

(Thank you for the post code advice! I’ll follow that next time; doesn’t look like I can edit the original post!)

Aha! That worked!

I think what keeps tripping me up is understanding that I need another more specific variable (hue) to capture or ‘get’ the more universal kind-of-value variable (colour), if that makes sense. (I’m very much self-taught in programming as a neophyte.)

1 Like

Glad it worked! And yes, that exact thing always feels awkward to me too so as far as I’m concerned you’re in good company there :slight_smile:

You don’t need it; it just makes the code clearer.

This works just as well:

When play begins:
	Repeat with item running through off-stage pens:
		now the colour of item is a random colour;

On the other hand, you don’t want to go this far:

When play begins:
	now all off-stage pens are a random colour;

That would make all off-stage pens the same color, rather than picking a different random color for each.

1 Like

Aha! Thank you for digging into this further.

Maybe the conceptual leap I’m having trouble with isn’t ‘specific vs universal’ variables, but understanding how to apply them.

Before, I was trying to use:

let the pen be a random colour

But then your example:

now the colour of item is a random colour;

What I think I’m starting to understand is that the ‘colour’ variable needs to be both ‘called’ and then ‘managed’ if that makes sense. My first attempt missed that step, just trying to say the ‘let the pen be a random colour.’

Not to mention the usage of ‘item’ to sort of ‘hold space’ for the pen while things are getting applied to it.

In the little bits of Javascript I’ve been learning, I could probably add a variable to itself or use it to modify the process in some way, but the natural language of Inform 7 obscures that point for me, if that makes sense.

The problem is that “the pen” doesn’t tell Inform which pen you want to apply things to. If you’re familiar with JavaScript, what zarf’s code does is equivalent to:

for(let item of getOffstagePens()){
    item.color = randomColor();
}

In other words, you iterate over the offstage pens (using a temporary variable to show which one you’re currently working on), and set the “color” property of that one to a random color.

2 Likes

The item is in the context of the loop

	Repeat with item running through off-stage pens:

That sets up item as a temporary (local) variable.

1 Like

I wonder if you are being confused by the double usage of the word colour here. The first use of color refers to a property of the pen whereas the second refers to a kind of value.

1 Like

You can make the assignment with the item is [color value] instead of the color of the item is [color value], which might slightly improve readability (and reduce chances of making the confusion Phil described if that had been occurring…)

Color is a kind of value. The colors are red, green, blue.
A thing has a color.

when play begins:
repeat with x running through off-stage things begin;
  now x is a random color;
end repeat;
1 Like