• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Limiting spawn rate of enemies?

P

PizzaGuy676

Guest
I'm creating a platform shooter in the trial version of GameMaker Studio 2 based off of HeartBeast's Platform Shooter series. I would like to create my own custom enemy spawning system, but I've run into a problem. Basically, I have a spawner object in my room that I've set two alarm events for. The first alarm is called every ten seconds to trigger a second alarm that spawns an enemy each second.

Alarm 0 (o_spawner):
Code:
/// @description Start the wave
if instance_exists(o_player) && enemy_count_ = 0 {
    wave_ += 1;
    alarm[1] = spawn_rate_; //spawn_rate is equal to 1 second (1*room_speed)
}
Alarm 1 (o_spawner)
Code:
/// @description Spawn enemies
if instance_exists(o_player) { 
    instance_create_layer(x, y, "Instances", o_enemy);
    alarm[0] = time_between_waves_; //time_between_waves_ is equal to 10 seconds (10*room_speed)
    alarm[1] = spawn_rate_;
    if enemy_count_ >= max_enemies_per_wave_ {
        alarm[1] = -1; //max_enemies_per_wave_ is equal to 3
    }
}
The second alarm is supposed to spawn three enemies and then stop. But for some reason, the enemies never stop spawning. I've tried setting alarm[1] to -1 in alarm[0], but that didn't work. I also tried using the exit function instead of setting alarm[1] to -1, but that didn't work either. I've been having this problem for several days, so I really want to figure out how to solve it.
 
K

keenjikun

Guest
The problem is probably the enemy_count_ variable.
Code:
if instance_exists(o_player) {
    instance_create_layer(x, y, "Instances", o_enemy);
    alarm[0] = room_speed * 10
    alarm[1] = room_speed
    enemy_count_+=1
    if enemy_count_ >= 3 {
        enemy_count_ = 0
        alarm[1] = -1; //max_enemies_per_wave_ is equal to 3
    }
}
I've tried this and it works fine.
 
P

PizzaGuy676

Guest
The problem is probably the enemy_count_ variable.
Code:
if instance_exists(o_player) {
    instance_create_layer(x, y, "Instances", o_enemy);
    alarm[0] = room_speed * 10
    alarm[1] = room_speed
    enemy_count_+=1
    if enemy_count_ >= 3 {
        enemy_count_ = 0
        alarm[1] = -1; //max_enemies_per_wave_ is equal to 3
    }
}
I've tried this and it works fine.
Wait, why add 1 to enemy_count_? That's just a variable that I set equal to the number of enemies in the room. I don't understand how adding 1 to that variable is supposed to fix the problem. Won't enemy_count_ automatically increase as the number of enemies increases?
 
K

keenjikun

Guest
Wait, why add 1 to enemy_count_? That's just a variable that I set equal to the number of enemies in the room. I don't understand how adding 1 to that variable is supposed to fix the problem. Won't enemy_count_ automatically increase as the number of enemies increases?
I see, but what if the enemies get destroyed? Won't enemy_count_ decrease again, allowing the object to spawn more enemies?
 
P

PizzaGuy676

Guest
I see, but what if the enemies get destroyed? Won't enemy_count_ decrease again, allowing the object to spawn more enemies?
I know your code works, I tried it, and it actually fixed the problem. But I'm still not satisfied because I don't know why it works. I think the reason it was spawning too many enemies was because GameMaker thought this: "if there are at least 3 enemies, stop spawning enemies. else, continue spawning enemies." But even when I tell it to stop spawning enemies when there are more than 3, it doesn't stop. It would really help if you could explain this to someone who's kind of new to GameMaker.
 
S

Silver_Mantis

Guest
I know your code works, I tried it, and it actually fixed the problem. But I'm still not satisfied because I don't know why it works. I think the reason it was spawning too many enemies was because GameMaker thought this: "if there are at least 3 enemies, stop spawning enemies. else, continue spawning enemies." But even when I tell it to stop spawning enemies when there are more than 3, it doesn't stop. It would really help if you could explain this to someone who's kind of new to GameMaker.
I can try to help you out.
So the first problem I saw, was that you had the code inside your alarms. Normally the alarm should stay blank for things like spawning and should be only controlled through your Step Event.
Also, the way you go about telling your spawner "Not to spawn" is by setting a limit , a number. Meaning every time an enemy is spawned, there is an integer involved.
There are two ways you could do this, count up, and have an infinite amount of enemies at a capped number or count down, and have them stop spawning once the spawner number has depleted to 0.

Here are the two examples:
(These are in your Step Event, with your alarms blank.)
(Also it seems as if you only need one alarm? I was confused as to why you had two. I'll show you below lol)

Code:
//Counting Down Spawner:
//Start enemy_count at the number of enemies you
//would like to spawn. Let's say 3 for example.
//After 3 enemies are spawned, the spawner stops in this example.

if instance_exists(o_player)
{
    // If alarm is NOT active or at -1
    if (!alarm[0]) && (enemy_count >= 1)
    {
        //Set alarm time
        alarm[0] = room_speed * 2;

        instance_create_layer(x, y, "Instances", o_enemy);
        enemy_count -= 1;
    }
  
}

Code:
//Limited Number Spawner:
//In this spawner, we are setting a limit to how many
//enemies can spawn, and there are infinite of them.
//enemy_count should start at 0;

if instance_exists(o_player)
{
    if (!alarm[0]) && enemy_count <5)
    {
    alarm[0] = room_speed * 2;
    instance_create_layer(x, y, "Instances", o_enemy);
    }
}
Then inside your enemies code:
Create:
Code:
//Increase the number of enemy_count from here:
YOUR_OBJECT_HERE.enemy_count += 1;

Destroy:
Code:
//Decrease the number of enemy_count from here:
YOUR_OBJECT_HERE.enemy_count -= 1;
I hope that helps you out, or at least gives you an understanding!
 
Last edited by a moderator:
S

Silver_Mantis

Guest
Is that true? How come? To give you more control?
Well if the actions with your alarm are all within your Step event, you only have to look one place to see how your alarm is working.
The action within the alarm only happens once it's triggered, but inside the Step Event, you can have multiple actions as the timer is running down.
Like if the alarm is at 3 do something, and if the alarm is at 1 do something... Catch my drift?
 

Bentley

Member
Well if the actions with your alarm are all within your Step event, you only have to look one place to see how your alarm is working.
The action within the alarm only happens once it's triggered, but inside the Step Event, you can have multiple actions as the timer is running down.
Like if the alarm is at 3 do something, and if the alarm is at 1 do something... Catch my drift?
I see. Thanks for the explanation.
 
Top