How to add random images in the twine (sugarcube)

Twine Version: Twine2 Sugarcube

Hello guys,

I want to add 5 or 6 images in a same passage. But every time my hero come to the this passage I want to this images change to another one randomly. How can I do that ?
(Just the images not the text)

Kind regards.

Here’s one way to do it. I’m sure there are others.

<<set _rndnbr to random(1,3)>>

/* for verification: can be removed */
rndnbr is _rndnbr

<<switch (_rndnbr) >>

<<case 1>>
[img[_img_10]]

<<case 2>>
[img[_img_20]]

<<case 3>>
[img[_img_30]]


/* I'm paranoid: default probably can be omitted */
<<default>>
[img[_img_40]]

<</switch>>

1 Like

Thanks for the reply but I dont quite understand, sorry but Im new at twine and I think this codes not work in sugarcube. When I copy-paste iy on my passage its says:
“Harlowe macros use a different syntax to Twine 1, SugarCube, and Yarn macros.”.
If you guys leave a solution with a example I am very apperice it. Many thanks

Kind regards.

The answer to your problem is right in the error message. You’re using Harlowe, not SugarCube.

I’m assuming that you changed the default story format to SugarCube. Changing the default only affects new projects, not existing ones. So what you need to do is go into your existing project and change its story format.

I haven’t used Twine 2 in a while so this may be off. IIRC, in the latest version of Twine 2 you can find it under story or setting → details, something like that. Older versions have it in different locations.

Anyway. Once you change your existing project’s story format to SugarCube, selden’s example should work.

Once you’ve fixed your story format problem.

Another way to do random images would be to use the either() function within the image markup.

For example:

[img[either(
	"img_url",
	"img_url",
	"img_url",
	"img_url",
	"img_url",
	"img_url"
)]]

Where each "img_url" is a URL for one of the images you plan to use—e.g., "images/bandit1.jpg" or "street-thug-9.png".

NOTE: Do not include a trailing comma at the end of the list—as shown above, the final list item does not end in a comma.

1 Like