How spawn random objects only inside another object?

Hey ! My name is Hatsuya, i’m new on Gdevelop.I’m sorry for some stupidity, but I’ve been trying lots and lots of ways, and none of them worked.

Basically what I want to do here is spawn some grass randomly inside my area called “chunk”

I want to do it in a way that no matter where the chunk is placed, the grass respects it and will always be randomly placed inside the chunk.

Any idea how I can do this?

You can have something like this

Create grass at X: RandomInRange(Chunk.X(), Chunk.X()+Chunk.Width()), Y: RandomInRange(Chunk.Y(), Chunk.Y()+Chunk.Height())

It should work, it picks a position between the Chunk’s X position (it is at the top left corner of the sprite) and Chunk’X Position + it’s width. So, if the Chunk’s X position is 100 and it’s width is 50. it will create the grass between X 100 and 150. It applies same for the height


That won’t quite work. Grass could end up with the origin on the very right of Chunk or below it. You’ll need to take the width of grass into account. After the grass object has been created, update it’s position as :

Set position of grass as X: RandomInRange(Chunk.X(), Chunk.X()+Chunk.Width() - grass.Width()) , Y: RandomInRange(Chunk.Y(), Chunk.Y()+Chunk.Height()- grass.Height())


It Works ! Thank you so much :smiley:


I just have one last question, how do I make it so when the objects spawn, they don’t collide? is there any way to do this?

I want them to spawn without being on top of each other like in the image above

Try checking if they are colliding, if yes, then delete one of them


1 Like