Displaying a random image in Inform 7

I would like to display a random image.

For instance, to display a specific image I can do something like:

Figure of Image1 is file "Image1.jpg".

When play begins:
	display Figure of Image1.

But what if there are a collection of images, and I want a random one selected. I was using this sort of code:

Figure of Sun1 is file "Sun1.jpg".
Figure of Sun2 is file "Sun2.jpg".
Figure of Sun3 is file "Sun3.jpg".
Figure of Sun4 is file "Sun4.jpg".
Figure of Sun5 is file "Sun5.jpg".
Figure of Sun6 is file "Sun6.jpg".
Figure of Sun7 is file "Sun7.jpg".

A Sun picture is a kind of file. The Sun pictures are Figure of Sun1, Figure of Sun2, Figure of Sun3, Figure of Sun4, Figure of Sun5, Figure of Sun6, Figure of Sun7.

When play begins:
	show a random Sun picture.

Is what I want a simple adjustment to the above code? Or am I completely barking up the wrong tree?

1 Like

I don’t know the exact phrasing off the top of my head but

  1. have a table of sun figures
  2. let N be a random entry from that table
  3. show N

something along those lines.

2 Likes

I think a table is more than you need. Try:

Figure of Sun1 is file "Sun1.jpg".
Figure of Sun2 is file "Sun2.jpg".
Figure of Sun3 is file "Sun3.jpg".
Figure of Sun4 is file "Sun4.jpg".
Figure of Sun5 is file "Sun5.jpg".
Figure of Sun6 is file "Sun6.jpg".
Figure of Sun7 is file "Sun7.jpg".

Sun pictures is always { Figure of Sun1, Figure of Sun2, Figure of Sun3, Figure of Sun4, Figure of Sun5, Figure of Sun6, Figure of Sun7 }.

When play begins:
        let i be a random number from 1 to the number of entries of Sun pictures;
	display entry i of Sun pictures;

I think that’ll work.

2 Likes

Perfect, thank you!

1 Like

Here’s one approach that depends on a naming convention for the Figure names, such that the third word of the name (here ‘Sun’ or ‘Moon’) defines the type:

Figure of Sun 1 is file "Sun1.jpg".
Figure of Sun 2 is file "Sun2.jpg".
Figure of Sun 3 is file "Sun3.jpg".
Figure of Sun 4 is file "Sun4.jpg".
Figure of Moon 1 is file "Moon1.jpg"
Figure of Moon 2 is file "Moon2.jpg"
Figure of Moon 3 is file "Moon3.jpg"


Definition: a figure name (called the picture) is Sun if word number 3 in "[picture]" is "Sun".
Definition: a figure name (called the picture) is Moon if word number 3 in "[picture]" is "Moon".

When play begins:
	display a random Sun figure name;

With this, you don’t need to keep a list of files and a list of Figure names in sync- just add another appropriately-named one to the list of files- and add another definition if you need another type.

2 Likes

And here’s another- based on the same naming convention for figure names, but using kinds of value. For this, to add a new type you just need to add it to the list of figure-types:

A figure-type is a kind of value. Some figure-types are Uncategorised, Sun, Moon.
A figure name has a figure-type.


Figure of Sun 1 is file "Sun1.jpg".
Figure of Sun 2 is file "Sun2.jpg".
Figure of Sun 3 is file "Sun3.jpg".
Figure of Sun 4 is file "Sun4.jpg".
Figure of Moon 1 is file "Moon1.jpg"
Figure of Moon 2 is file "Moon2.jpg"
Figure of Moon 3 is file "Moon3.jpg"

First when play begins:
	repeat with fn running through figure names:
		repeat with ft running through figure-types:
			if the substituted form of "[ft]" is word number 3 in "[fn]":
				now the figure-type of fn is ft;
				break;
When play begins:
	display a random Sun figure name;
2 Likes

And here’s another strategy using a relation-as-condition, which avoids the need to have a ‘when play begins’ routine to load up figure names with figure-type properties-as-adjectives: instead we use the verb to depict to directly link a figure name to its type.

A figure-type is a kind of value. Some figure-types are Uncategorised, Sun, Moon.

Figure of Sun 1 is file "Sun1.jpg".
Figure of Sun 2 is file "Sun2.jpg".
Figure of Sun 3 is file "Sun3.jpg".
Figure of Sun 4 is file "Sun4.jpg".
Figure of Moon 1 is file "Moon1.jpg"
Figure of Moon 2 is file "Moon2.jpg"
Figure of Moon 3 is file "Moon3.jpg"

Depiction relates a figure name (called the work) to a figure-type (called the subject) when the substituted form of "[subject]" is word number 3 in "[work]".
The verb to depict means the depiction relation.

When play begins:
	display a random figure name depicting the Sun;
3 Likes

These are brilliant, thanks Peter!

(I’m in continued awe at people who know Inform 7 to this sort of depth.)

1 Like