Create objects at random "fixed" positions

Context

I’ve got a group of objects that I’m spawning at several locations.
I created an Array variable and specified 4 X and Y positions.

What is the expected result

I want to spawn the objects at fixed coordinates, so that 1st X coordinate from the Array is always matched with the 1st Y coordinate and so on.

What is the actual result

Currently, it is matching any X coordinate with any Y coordinate.

Ideas :question:

Related screenshots

The extension is picking a new random number for each array. You can set a variable to a random number and use it for both arrays. If you declare or setup the variable then you can use the variable name without the Variable() part. Arrays start at zero, so you need to subtract 1 from the max count…

Another option is to use a placeholder object, add as many instances to the screen as needed and then hide them at startup and pick a random object and use it’s x,y or CenterX(),CenterY()

I used mouse released for testing so it would only create one at a time.

The placeholder method is much easier to place and adjust plus, you can use a Boolean variable (or other method) to prevent the same spawn point from being used.

Add a placed Boolean to the placeholder object. Leave it set to false. Once there aren’t any objects with placed set to false it resets them all to false.

Yes, because you are spawning at a random x array element position and a random y array element position.

You want to generate a random index (number variable) and use that as the array index when accessing the variable. Something like:


BTW, ToString(Variable(Foe)) is better written as VariableString(Foe). GDevelop will convert between types.

Thank you!

@Keith_1357, your solution with the spawn points it so simple yet brilliant, works exactly as I needed :smile:

@MrMen, thanks for the input, I tried your solution as well, but I’m doing something wrong.
I’m not sure I understand the random index variable. Do I create a new variable with numbers 0-3, or where does this variable come from? (I don’t fully understand variables yet).

1 Like

It’s in the screen snip of the events I included in my post. The variable rand_idx is set to a random number between 0 and the size of Spawn_Object_X. You then use that randomly generated value to select the X & Y values from the same position in Spawn_Object_X and Spawn_Object_Y.