• 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 Spawn enemies at random intervals

B

Bokkie2988

Guest
Hello,

Is there a way to spawn enemy ships at random intervals, and as the game progresses there will spawn more and more at a faster rate and the game gets harder as you play?

Thank you.
 
Last edited by a moderator:

samspade

Member
Yes. But without knowing anything else or seeing any of your other code it is hard to say much more. However, here are two examples

Example one:

Code:
///some create event
elapsed_game_time = 0;
spawn_time_interval = room_speed * 10;
spawn_time_interval_decrease = room_speed;
next_spawn_time = spawn_time_interval;

///some step event
elapsed_game_time += 1;

if (elapsed_game_time > next_spawn_time) {
    //spawn something
    spawn_time_interval -= spawn_time_interval_decrease;
    next_spawn_time += spawn_time_interval;
}
Note that in this example the spawn_time_interval decreases linearly. Also note that there is no protection for 'long' games. Eventually spawn_time_interval will be less than or equal to zero and the spawning will happen every frame.

Example two:

Code:
///some create event
spawn_delay_decrease = room_speed;
spawn_delay = room_speed * 10;
alarm[0] = spawn_delay;

///alarm[0]
//spawn something
spawn_delay -= spawn_delay_decrease
alarm[0] = spawn_delay;
Note, also a linear decrease here. No protection for 'long' games here either although with an alarm once the value becomes negative, the alarm won't run. You could also do this with custom alarms.

Neither of these use random intervals, but you can easily add that in by adding a random amount to the base calculation.
 

Artie_Kyle

Member
Note, also a linear decrease here. No protection for 'long' games here either although with an alarm once the value becomes negative, the alarm won't run. You could also do this with custom alarms.

Neither of these use random intervals, but you can easily add that in by adding a random amount to the base calculation.
Couldn't he create a way to prevent the constant increase after the objects in the room pass a certain number tho?
Code:
if instance_number(obj_enemy)>50 {
       with (obj_enemy) {
             instance_destroy();
       }
}
Didn't run this obviously, but shouldn't the logic be sound, even if my code isn't?

EDIT: how about doing it in the opposite way, like if instance_number is less than 50, keep creating enemies?

He'd put your code inside the if instance number part and it should keep creating until the game reaches the desired number of objects.

Does that make more sense, or am I still off?
 

samspade

Member
Couldn't he create a way to prevent the constant increase after the objects in the room pass a certain number tho?
Code:
if instance_number(obj_enemy)>50 {
       with (obj_enemy) {
             instance_destroy();
       }
}
Didn't run this obviously, but shouldn't the logic be sound, even if my code isn't?
Sure, but I just didn't want to code. As you thought, your code would have a significant problem as it would delete all obj enemies once there were more than 50. But the idea is right. Still, if I were to limit the increase, I would probably just add a line that says:

Code:
spawn_delay = max(spawn_delay, min_spawn_delay);
Where min_spawn_delay was set to whatever you wanted the lowest spawn_delay to be. You'd add this, or some similar line in.

Alternatively you could add a check to the spawn code portion where you only run the spawn code (even if the timer has gone off) if there are less than a certain amount of enemies.

Code:
if (instance_number(obj_enemy) < 50) {
    ///spawn enemy
}
 

Artie_Kyle

Member
Ahh, so I was write in the second part where it is less than 50. Glad to hear I'm not a lost cause!

Question though; does the max function here still serve the same purpose as instance_number less than 50?

Way I imagine it, we would put that max function in your alarm event under the spawn enemy part. Am I correct here?

This figuring things out is starting to get addictive!
 

samspade

Member
Ahh, so I was write in the second part where it is less than 50. Glad to hear I'm not a lost cause!

Question though; does the max function here still serve the same purpose as instance_number less than 50?

Way I imagine it, we would put that max function in your alarm event under the spawn enemy part. Am I correct here?

This figuring things out is starting to get addictive!
The max function accomplishes a similar goal but is doing something different. The max function prevents the alarm time from ever dropping below a certain point - e.g. so you could set min_spawn_delay to 5 * room_speed and now spawn_delay will always be at least 5 seconds. But there's no enemy cap. The other version allows the alarm speed to drop to negative but only spawns enemies if there are less than 50. So they do different things and cap the enemy spawn rate in different ways. You could also run them both at the same time.
 

Artie_Kyle

Member
You could also run them both at the same time.
...Mind. Blown.

I'm yet to use both min and max long enough to understand exactly what you're saying (or 'imagine' in this case is the more accurate word), but let me try to explain what I'm thinking in my head and tell me if I'm right:

If we combine both these methods, it would create an enemy every 5 seconds (assuming room speed is 60fps), until it hit 50, then it would stop creating more enemies.

Is the thought process right here?

Thanks for entertaining me on this!
 

samspade

Member
...Mind. Blown.

I'm yet to use both min and max long enough to understand exactly what you're saying (or 'imagine' in this case is the more accurate word), but let me try to explain what I'm thinking in my head and tell me if I'm right:

If we combine both these methods, it would create an enemy every 5 seconds (assuming room speed is 60fps), until it hit 50, then it would stop creating more enemies.

Is the thought process right here?

Thanks for entertaining me on this!
Yes.
 
Top