Mela_dc
(Simona Della Casa)
April 26, 2024, 8:38pm
1
Hi everyone,
I’m new in coding and programming in general.
So this is my first book game and i want to give a random name to a city.
I tried in harlowe first and the code was like this:
(set: $cities to (a: “Trassa”, “Delo”, “Manesh”, “Laaur”, “Grond”))
(set: $city = $cities’s (random: 1,5))
But because harlowe is quite limited instead of sugarcube I moved there,
so now i don’t know how to transport this part of code.
thank you for your help!
JoshGrams
(Josh Grams)
April 26, 2024, 9:01pm
2
<<set $cities to ["Trassa", "Delo", "Manesh", "Laaur", "Grond"]>>
<<set $city to either($cities)>>
A list in square brackets makes an array, and then either picks a random element of an array.
2 Likes
Additional to Josh Grams advice…
There are a couple of other methods you can use to obtain a single random element from a pre-defined Array llike
<<set $cities to [“Trassa”, “Delo”, “Manesh”, “Laaur”, “Grond”]>>
1: the <Array>.random()
method, which returns a copy of a random selected element.
<<set _city to $cities.random()>>
In the above example, the $cities
variable will still contain the five city names, even though the _city
variable contains one of those names.
2: the <Array>.pluck()
method, which returns a random selected element, that has been removed from the Array.
<<set _city to $cities.pluck()>>
In the above example, one of the city names is now stored in the _city
variable, and the $cities
variable now only contains four city names.
3 Likes