(Fixed)Global variable/Alarm/instance_create issue

Corey

Member
Edit: Disregard. I fixed my problem by adding a Step event with global.enemyCounter = currentEnemies; to check the counter every frame and switched my Alarm event code to check conditions before spawning an enemy.

Hello all.

I have tried countless times to fix my problem. I am somewhat intermediate with programming with GML but new to GMS2 logic/UI which is why this is boggling me since I've used this type of code before. I want to set up a counter for enemies that spawn in an Alarm event. Each time the Alarm is called, and conditions are met, it's supposed to spawn an instance and increase the global variable by 1; however, my issue is the global variable is increased by 1, but it doesn't go above 1, and the game continuously spawns instances -- assuming since the global variable does not go above 1 and the conditions are never met to stop spawning.

The enemy that is spawning is set up through a ds_map, and it spawns the enemy based on the 'object' array that I have listed in my Alarm event.

I've initialized the global variable in the Create event as shown below:

Code:
enemyAlarm = false;
currentEnemies = 0;
global.enemyCounter = currentEnemies;
maxEnemies = 10;

alarm[0] = 60;
My Alarm event has the following code, and is set to repeat every 60 frames:

Code:
if (enemyAlarm = true)
    {
        instance_create_layer(x+irandom(room_width)/2,y+irandom(room_height)/2,"Enemies",object);
        currentEnemies += 1;
    }
  
if (global.enemyCounter >= maxEnemies)
    {
            enemyAlarm = false;
    }
  
if (global.enemyCounter < maxEnemies)
    {
        enemyAlarm = true
    }
  
alarm[0] = 60;
 
Last edited:
Top