# (SOLVED) How do I spawn objects at random position with knowing that the room layout and position of the player are random?

I want to spawn enemies at random locations in the room depending on the position of the player while also taking into account that randomly generated rooms will have different shapes and sizes.

Since the position of the player will change continuously and rooms will be different, common ways of spawning enemies don’t work in this case.

This is a drawn image of how position of the player and room layouts will change.

Hi,
I guess you can take a random x inside lenght of the full rectangle and a random y in the weight. Then loop is as long as x;y coordinates are inside a forbidden zone.
So inside the loop you take x and y randomly, and as long as these coordinates are not wanted, you keep the loop running.

Doing so, the loop will finish with a authorized x and y.

So the problem is to know how your forbidden zones are detected. If there are objects, you can check (in your loop ), for a given Object if x (randomly chosen) is between the x position of object and the position of object + its length.
Same for y.
Object.x < x < object.x + object.lenght.

Hi Bluzima, thanks for the reply.

I sort of understand what you said, but I’m not sure how would I achieve this with the code.
I don’t know how to get the length of the full rectangles. Should I use a raycast to get the distance between the walls? If I get the area where I can spawn objects, I am not sure how to proceed with the conditions.

This was a bad way to do it.
There is a very useful condition called “point inside object.”

So basically you first set variable(x) to RandominRange(0,n)
With n the lenght of your screen /room.
Same for y, a random number between 0 (too of screen) and the max y in your case.
0 can be the min x and y.
In other terms you find a random point in your screen /room.

Then you’ll have a simple loop:
As long as point (x;y) is not inside the wanted object(s), do this :
Set x and y to random number.

So the loop stops when x and y are coordinates of a point that is in the wanted objects (authorized zone)

If I understood you correctly I can make this work only if the room is fully covering the screen. I just noticed I wasn’t maybe clear with my drawing above.
The room is not fully covering the screen. The background will be visible. So besides the condition to spawn objects away from the player “P”, I have to make sure that objects are spawned between the walls, and not outside. The white background outside the green in the drawing is also a restricted zone.

Is the room an object?
Is it always in rectangular shape?
And are the forbidden zones objects ?

Room is a tiled sprite.
It is not always a rectangular shape.
Forbiden zone “P” is a player object, but the space outside the green zone is just an empty space; default background.

This is for a screen size 1400x800.
If you know the maximum limits of your rooms, you can replace 0 and 1400 by the rooms limits. But in any case, the loop will keep on, until it finds a point inside the room but not inside the forbidden zone.

Putting if statement in while causes the game to crash.

It works fine on my side.
Did you make sure the is inside the room condition is inverted…?

I did. Maybe I made mistake somewhere else.

What is this object background? Is it covering the whole screen?
Just asking. Because if it does so, the loop will never find a point outside.

Yes it is covering the whole screen. Once the room is randomly generated, it is placed on top of the background. So the room will block portion of the background depending on the size and shape of the room. But it will never fully block the background. Background is the place where objects shouldn’t be spawned.

OK. I’ll explain better how this loop works.

As long as the point is, either outside the room, either inside a forbidden object, it generates a new random point.

The background doesn’t have to enter into consideration. Since the loop always ends by locate the point inside the room.
My forbidden object, is not the background, but places, inside the room, where the random new point cannot be.

3 Likes

Your loop was looking for a random point inside the screen… That was not on the screen.
So it was endless.

I’m sorry, thank you for the explanation. I changed it and now objects do indeed spawn inside of the room. One thing that is left is to change is the spawn position set amount of times.
Right now, once the position is set, the loop stops and position doesn’t change.

So if the first spawn was at X=1 and Y=2, the next should be some other random X,Y values.

Problem still stands, objects keep spawning in the same spot. I will try repeating the loop desired amount of times and do RandomInRange spawn.

Good it’s working now :))

Are the spawns materialized as objects?
If yes, you can add to your loop the condition, point is inside spawn object.

Note than in this condition you can put an object, but also a group of objects, which can be very handy like group of spanws objects or group of forbidden zones objects.

If spawns are not objects, you can make them hidden objects, just to remember where they are and not spawn again here. With the same method than above except spawns are invisible.

Else there is another possibility : record previous x and y to avoid them later, but that’s a bit more complex.

They materialize as an object. I added the condition in loop to check if the point is inside of an object.

It works perfectly now.

I don’t know what to say. I’m honestly stunned by your knowledge, patience and will to help me.
Thank you so much

2 Likes

You’re very welcome! Glad I could help with it. My knowledge is limited but I already made kind of the same spawning thing before. Once you get familiar with loops and variables it’s easier. Good luck for your game. :))

1 Like