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.
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).
(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.)
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.
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.
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;