List of objects
I am trying to create a game where i want an "object" from a list of "objects" to be chosen randomly.
For example,
x = object y = object z = object
objects: x, y, z
show(random object)
I am trying to create a game where i want an "object" from a list of "objects" to be chosen randomly.
For example,
x = object y = object z = object
objects: x, y, z
show(random object)
Simply create your objects:
x = object end
y = object end
z = object end
Then create a list in which you will place those objects:
object_list = []
object_list.push(x)
object_list.push(y)
object_list.push(z)
Now you just need to get a random element from that list, using:
object_random = object_list[random.nextInt(object_list.length)]
And that's it!
If the variable object_random is x, and you also modify it, for example:
object_random.r = 20
Then x.r will also be equal to 20, because object_random and x are exactly the same object, it's not a "copy" or anything like that, they are truly the same