• 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 Code works as intended for a little while...

W

Will

Guest
Below is the object that manages spawning enemies in my game.

How it should work: If there are 0 enemies left to spawn (enemyCount), and no enemies are left (obj_enemy), increase the round, increase the amount of enemies spawned this round by 1 (enemiesMax) and set enemyCount = to enemyMax. If there are enemies left to spawn, spawn one every 1-3 seconds at a random spawn point (there are 5 instances of obj_spawner).

This works fine for the first 2 rounds. As soon as round 2 is complete, enemyCount will continue ticking down like it's creating enemies, and when it's 0, round_ will increase and so will enemiesMax and enemyCount, however no instances of obj_baby are created after round 2. obj_baby holds code turning it into a different enemy type on create, and this works as intended.

Furthermore, enemies are not spawning at spawner.x,spawner.y. They are being created at the player's x,y position. This was working fine before, but now that I have this system in place (no changes to their code, only to the spawn/round system), they don't spawn in the right place.

Step Event:
Code:
if enemyCount <= 0 && instance_number(obj_enemy) <= 0 {
    round_ ++;
    enemiesMax ++;
    enemyCount = enemiesMax;
    alarm[7] = room_speed * 5;
   
    itemSpawn = instance_random(obj_spawner);
    instance_create_layer(itemSpawn.x,itemSpawn.y,"layer_enemies",obj_item);
}

if instance_number(obj_enemy) <= enemiesMax and canSpawn {
    randomize();
    spawner = instance_random(obj_spawner);
    instance_create_layer(spawner.x,spawner.y,"layer_enemies",obj_baby);
    canSpawn = false;
    enemyCount --;
    alarm[6] = irandom_range(1,3) * room_speed;
}
Alarm 6:
Code:
if enemyCount > 0 {
    canSpawn = true;
}
Alarm 7:
Code:
canSpawn = true;
Does anyone have any idea why this might not be working? Did I create a bad loop somewhere? :(
 
At first glance it looks basically ok.

What's your code for instance_random? The fact that the enemies are spawning in the wrong position suggests this function is not returning an obj_spawner id.
 
T

tomsaram

Guest
This instance_random() does look a little bit fishy. As a test, I suggest you replace the line with:
Code:
spawner = obj_spawner;
So that the enemies will always spawn at one of the obj_spawner, if there is any. My guess is that this will make enemies spawn at a correct rate, but only at one of the spawners. If that turns out to be the case, then most definitively something is wrong with instance_random().
 
Top