Create enemies off-screen

Hi everyone, do any of you know how to create enemies off the game screen?

The exact same way that you create them on-screen.

I would go ultra lazy way and simply create them in some distance from screen center
So it would be enough for them to spawn off screen


I was trying to follow the advice of @ZeroX4 but something is not working, I don’t know where it is generating enemies.
the resolution of the project is 640x480 I would like to create enemies in random areas outside the screen

What you are doing
Your screen starts at position X0 Y0 in upper left corner
NOW if your screen width is for example 200
And your player is spawning in center of your screen
Then from spawn it is at X 100
Now you move to the right 100 pixels
And now your player is at X200 meaning if you used something to spawn at screen width then it will spawn LEFT from player and that is wrong

What you want to do is

For spawning from left to right border of screen above top border
Create enemies at
X RandomInRange(CameraBorderLeft()-enemies.Width(),CameraBorderRight())
Y CameraBorderTop()-enemies.Height()

For left to right below bottom screen border

Create enemies at
X RandomInRange(CameraBorderLeft()-enemies.Width(),CameraBorderRight())
Y CameraBorderBottom()

For top to bottom beyond left screen border
Create enemies at
X CameraBorderLeft()-enemies.Width()
Y RandomInRange(CameraBorderTop()-enemies.Height(),CameraBorderBottom())

For top to bottom beyond right screen border
Create enemies at
X CameraBorderRight()
Y RandomInRange(CameraBorderTop()-enemies.Height(),CameraBorderBottom())

Why sometimes you want to use object.Width() object.Height()
You see everything in gdevelop have its origin point
Top left corner by default and if you spawn something on idk X 100 Y 100
It means that 1st pixel of that object will be placed on X 100 Y 100
So if your object have 20 width and 50 height
It means it will spread 20 pixels to the right and 50 pixels down
And to compensate for that you subtract height or width of object
For example if you spawn object at CameraBorderTop()
TOP edge of object will be touching top screen edge
So you will perfectly see it right away
BUT if you spawn it at CameraBorderTop()-object.Heigth()
BOTTOM edge of object will be touching top edge of screen
Meanning it will perfectly be placed above top screen edge so you won’t see it
And you need to compensate same way for left camera border by subtracting objects width
You can add even idk -50 or +50 to each expression so it spawn even further beyond screen

There is also extension called ObjectSpawner
You can add it to your player and it would become spawner

And it would spawn stuff not in your screen coordinates
BUT some pixels away from your spawner obejct
In this case your player

BUT how you gonna approach it is up to you
Both methods will work and are fine to use

1 Like

If you add the “IsOnScreen” extension to your project, and add it as a behaviour to your enemies, then you can use this:




[Edit]
The ScreenWidth() and ScreenHeight() should be ScreenWidth()/2 and ScreenHeight()/2 respectively.

1 Like

Look, something like this will spawn objects in a circle. Just increase the radii until they start off-screen and you have the distribution you want.

change the global variable rada = 256
change the global variable radb = rada + RandomInRange( 16, 128 )
change the global variable na = RandomFloatInRange( 0, Pi() * 2 )
create object mob at position CameraCenterX() + RandomFloatInRange( rada, radb ) * cos( na ),CameraCenterY() + RandomFloatInRange( rada, radb ) * sin( na )

1 Like

thank you very much @ZeroX4 for the accurate explanation but I noticed that the camera spawner points are not updated while the player is moving and remain the same as at the beginning of the scene.
I was following the example sent by @MrMen which seems to work although it is creating problems for me when the enemies are eliminated since I am following a wave creation.
what I would like to do is create the enemies off-screen and that do not go beyond the game map.
I’ll try to attach my events to make you understand what I need


when enemies are eliminated there are fewer than there should be

To do that, you need to change those enemies position checks to have the map left, right, bottom and top edges instead of the 0, 640, 480 and 0 values.

1 Like

You are not using what i wrote
So idk how you expect it to work as i said it will work

1 Like

The above solutions should all work mostly fine. In my case I just have 4 subevents (one for each side of the camera), and a random variable number being defined in a parent that decides which one to spawn from.

Here’s one without any conditions (you’ll definitely want to add some or it’ll infinitely spawn, putting it into your while event should also work fine with the conditions you have in the “While” portion).

  • You can tweak the values as needed, right now it spawns the enemy anwhere between 2000 and 150 pixels off screen in that direction.

  • Make sure you update the expressions to use whatever layer your actual game objects should be on, right now it’s using the base layer (“”)

  • The CameraBorderLeft/Right/Top/Bottom expressions simplify a little on some of the above methods by not having to use the onscreen/offscreen extension as well.

Per’s method will get you a radius (circle) of enemy generation, whereas this method will get you more of a rectangle around the camera.

1 Like

Sorry for my delay in replying but I haven’t had the chance to get to the computer these days.
yes @ZeroX4 I used your method initially but as I described the problem was that enemies were spawning at points in the base layer.
following the code sent by @Silver-Streak I corrected and reproduced what you sent and it seems to work.

what I would like to achieve now since I have limited spaces is to create enemies inside the game arena (640,480).
if the player is in the upper left part then the creation of enemies must be carried out either to the right of him or at the bottom and not in the upper or left part otherwise they will be created outside the map.

Wait wtf
Even post title says
Create enemies off-screen
And now you want to spawn them on screen?

And as i am looking at Silvers method
This is exactly same thing i suggest
He just specified layer which could be done in my method also

I now have no idea what was your initial goal and why my method does not work for you

yes you are correct I need to create enemies off screen but not beyond certain limits if possible

For clarification, what did you find unsuitable about my suggestion to spawn at a radius from camera center? If I understand that, I can make a more suitable suggestion.