Create enemies inside the dungeon but off camera

hi, I’m back on gdevelop after a long time, my name is Kevin and I’m trying to create a new project.
my idea is to develop a game similar to Vampire Survivors.
I ask for help in finding a solution to this problem of mine.
I would like to spawn enemies inside the dungeon but off camera

Can you give me some ideas on how to create this mechanic?

I’m assuming the camera moves around the dungeon. Have you tried spawning them in a random location with an X value that’s between 0 & dungeon_width, and a Y component that between 0 and Dungeon height?

yes the camera follows the player, at the moment I’m simply using this

what I would like is an off-camera enemy creation but within the confines of the dungeon

another important thing, the enemies created are counted so they must all be created inside the dungeon without having to eliminate them if created outside

Ah, gotcha. Here’s an earlier thread with pretty much the same question. Does it answer your question?

Yes, looking around the forum, it’s the only post that talks about it but I haven’t found a real solution.
they talk about how to create enemies outside the camera but not inside the confines of a dungeon

So this approach may use some “magic numbers” and there may be better ways of doing it, but I would try something like this to get things up and running:

For X:

32 + Random(1216)

For Y:

32 + Random(656)

Honestly, that code should work. Not sure why it’s not working.

I don’t think it works…
if the player is in the top right of the dungeon and the enemy is spawned in a random position always to the player’s right he would end up outside.
I thought about eliminating enemies created outside the dungeon but I also need to keep a count of enemies created for wave management.

Change the X on the event in the screenshot to:

100 + Random(1080)

Change the Y to:

100 + Random(520)

You may need to adjust all these numbers as needed.

Let’s brainstorm and discuss all of the possible ways to spawn objects.

Brute force: Some people like the brute force approach where you pick a random X, Y and then delete or move the object until a valid location is found. This approach isn’t my favorite especially when there are a large number of possible invalid locations. Although, separate objects can help in some situations.

Choose a random side and a random position.
Choose a number from 1 to 4
If 1 pick a location between 0,0 and camera bounding box left, top
If 2 pick a location to the right meaning
Camera bounding box right, top to dungeon width, camera bounding box bottom

The downside is it’s not weighed. A larger side would have the same odds of getting choosen plus counting spawned objects would be vague because there would only be 4 areas. I asked a bot for weighting, IDK if it would work but it still doesn’t help with the count.

Spawn points or spawn zones
Both approaches would be similar. Add either objects to be used a spawn point or an object used to create a spawn zone.

You would then pick a random object that isn’t on the screen or for zones the player isn’t in collision with. Edit: although, the collision check approach wouldn’t work when the player was close to another zone. So, IDK how to choose this. Maybe a mixture of both approaches. This is similar to a room based method.

For points you could either spawn from the point which would require more points to spread them out or spawn with a random distance from the point like X() + random in range (-200,200) for X and Y.

For a zone object, you could spawn within the bounding box of the object.

To count the number of spawned you can use the spawn behavior settings although adding objects manually is much more efficient and customizable.

To count the objects near a point, you can use a distance condition and the number of picked objects condition.

For zones, you can use in collision with the zone with the picked object count condition or expression.

Zones and points sound like they might use resources but the image could be 1 pixel, they’d be stationary, hidden and wouldn’t need a behavior. You also would only try to spawn an enemy every second or 2.

I’m sure there are tons of other ways and variations. But this should create a starting point.


To add to @Keith_1357’s suggestions, you could repeatedly generate a random position within the confines of the dungeon until you have one that’s not also within the camera bounds.

To do this:
add the “Object is on screen detection” extension,
add the IsOnScreen behaviour to the zombie object and then use the events like:


That is what I meant by brute force. Sorry, if I wasn’t clear enough. My problem is that random numbers are chatotic. The first numbers could be valid or the 100th or 1000th.

I’m not dismissing the idea. Brute force is good for guessing a 1 letter password but the longer the password the less efficient it would be. The same for something that needs to ignore all of the X, Y positions within the camera bounding box.

I would test it and see what the profiler shows. I love the method. I’m just concerned with lagging. No matter how unlikely. Hope for the best. Plan for the worst.

Yes, because there’s a possibility, not matter how remote, that it takes a long time to generate 2 random numbers not in the camera’s bounds.

I think this is where this post requesting a random number generator with gaps is relevant


thanks for your help guys. thinking about it, I believe that the best solution is to create spawn points inside the dungeon

maybe I could use the “is on screen” extension to avoid enemy spawns when the camera is viewing one of the spawn points.
What do you think about it?

A nice way of doing it. If it solves your problem, then it’s a good solution.

1 Like

A solution may be to put big rectangular areas just outside of the visible screen and use spawners.

The position of the areas must be updated all the time, and the spawners must… well, spawn, only when they collide with the areas.

1 Like

I think it is not necessary to use generators and move them constantly.
It would be enough to create spaces of Spawn at the ends of the dungeon avoiding the generation of enemies only on the spawn area that “collides” with the camera.
In this way you should not understand where they are generated.

I’m doing some tests but I don’t know the “is on screen” extension … you can tell me what is wrong with this? no enemy is generated

It works for me on my phone. As long as there are objects not on the screen and they have the behavior.

[ignore the last edit]

I wrote the code in a new project added the is on screen extension to the Spawawnemy object but are not created

If that’s the case, then you can just use spawners that only spawn enemies when they are off-camera. You don’t need anything else.

1 Like