# How can I spawn different items at different locations after interval infinitely

Hello,
I am Sarthak Agrawal and I am facing problem in making a game in Gdevelop. The problem is that I do not have any clue about how I can spawn different objects at different locations. Example: Suppose I am making a game and there is a building in which people throw different objects at a person below. I don’t know how to make different objects appear at different balconies of the apartment. Please help me solve this problem.
Thank you
Regards

If the balconies are instances of the same object you can use the For each event to iterate through each instance.

`For each balcony object repeat : create random object at position balcony.X() and balcony.Y()`

balcony.X() and balcony.Y() is an expression that return the current position on X and Y axis of the object called balcony. You can use this expression at places where you want to reference the location of an object.

In case all balcony is a single object / background, you need to use a Repeat event where you repeat the object generation in position of each balcony.

Let say there are 10 balconies 5 in a row and each balcony is 100 pixels wide.

``````Do = 0 to scene variable X
Do = 0 to scene variable Y
Do = 0 to scene variable counter.
Repeat 10 times : create random object at position X and position Y
Do +1 to scene variable counter
if counter < 5 : Do + 100 to scene variable X
if counter = 5 : Do + 100 to variable Y and Do = 0 to scene variable X
``````

In this example we are using 3 variables X and Y to position the objects and counter to count how many balconies we have iterated through. We start at the first in the top left corner and move down to the last at the bottom right corner, remember I count with 10 balconies in 2 rows, 5 in each row and each 100 pixels wide. If what I wrote doesn’t makes sense to you I’m afraid I don’t have time to explain further here. But I hope you get the idea.

To create a random object is very simple. Have a single object and add each item you want to generate as an animation of this object and then simply pick a random animation. Let say we have 10 different items so it is 10 animations.

``````Create object at position X and Y
Do = Random(9) to the animation of object
``````

This is one way to do it. Random(9) is an expression that return a random value in range 0-9. The first animation of an object is always 0, so when we calculate with 10 animation, it means 9 including the 0. This is something you need to get used to because this is how it works with most game engines.

I hope I was able to answer some of your question but I can imagine what I just told you raises even more . It is maybe a little too advanced topic for a beginner. Try to learn and practice the basics first in case you find this too complicated.

1 Like