r/gamemaker • u/nanaki626 • 9d ago
Help! Help with Randomly Spawning Enemies
Hi all, I am really new to learning Gamemaker, so far mostly just watched a couple of tutorials and am experimenting with stuff. I am trying to make a very basic game where you just move around a room and kill enemies that randomly spawn in just to get a handle on how to do these things.
I seem to have figured out how to make enemies randomly spawn in but was having an issue with them spawning inside walls and am having trouble figuring out how to not do that.
This is what I came up with as a quick and dirty way but it basically only works with the current room I am working in, so not something that can be easily reused
if (instance_number(obj_enemy) < max_spawns)
{
var _rx = irandom_range(411, 914);
var _ry = irandom_range(173, 561);
instance_create_layer(_rx, _ry, "Instances", obj_enemy);
}
Can anyone help me with a way to make something cleaner and resuable that prevents the enemies from spawning in walls or on top of each other?
Thank you in advance.
2
u/Lethalogicax 9d ago
I'm not sure what your exact setup is, but there are a few different ways of approaching this...
You can make your placement algorithm intelligently find valid locations before spawning (might be difficult, will definitely be a lot of math), or you could make the spawn mechanic fail when choosing an invalid spot so it rerolls RNG and tries again in a new spot (quick and dirty solution).
Either way, you need to find a way to detect when an object has been placed into an invalid location and then have your code reject the invalid location, and to then select a new random location.
Are you using Gamemaker's built-in collision system? If so then you can use place_meeting() to detect if a set of coordinates is within an object's hitbox.
2
u/JaXm 9d ago edited 9d ago
You can do something like
What this does is it runs a while loop, and if rhe randomly selected coordinates return a collision, will find new coordinates until they no longer do, THEN, create rhe enemy at those coordinates.
Then you just call the function somewhere in your code whenever you want to spawn in a new enemy.
Edit: corrected using the wrong collision function check.
1
u/azurezero_hdev 9d ago
you cant prevent them from spawning on walls, but you can have them rapidly jump around on creation until they find an empty space (with a while statement)
1
u/azurezero_hdev 9d ago
while !place_free(x,y) or place_meeting(x,y,obj_enemy){
x = whatever
y = whatever
}
1
u/Krilantis 9d ago
Test the spawn with the enemy's own mask rather than a single point: create it, then `with (_e) { if (place_meeting(x, y, [obj_wall, obj_enemy])) instance_destroy(); }` and retry. That catches both walls and other enemies (the array form needs a recent GameMaker version). Put a cap on the retries, like 10 per spawn, so a crowded room can't hang in a while loop, and use room_width/room_height or a spawn-area object instead of hardcoded ranges so it's reusable across rooms.