Fire delay alarm

Eui

Member
I have an enemy that moves or shoots based on distance to the player.
After looking at lots of different tutorials I've just become confused and am not really sure how I should be using this alarm because the if just seems to ignore it.
I also think it could be to do with how I'm using my boolean.
(The two if's at the bottom is where my alarm is)
enemy step event:
GML:
var PX = x + lengthdir_x(-16, image_angle);
var PY = y + lengthdir_y(-10, image_angle);

var player = obj_ship;


// Look at and follow the player until certain distance then shoot
if(instance_exists(obj_ship)){
    image_angle = point_direction(x, y, obj_ship.x, obj_ship.y);
  
    var sx, sy;
    sx = instance_nearest(x, y, player).x;
    sy = instance_nearest(x, y, player).y;
  
    if(point_distance(x, y, sx, sy) > 200){
        // Move towards the player
        motion_add(image_angle, 0.03);
        part_type_color2(global.ptBasic, c_red, c_orange);
        part_particles_create(global.partSystem, PX, PY, global.ptBasic, 1);
    }

    if canshoot
    { 
        if(point_distance(x, y, sx, sy) <= 180){
            canshoot = false;
            alarm[0] = 90*room_speed;
            var inst = instance_create_layer(x,y, "Instances", obj_bullet_enemy);
            inst.direction = image_angle;
            //audio_play_sound(snd_shoot, 1, false)
        }
    }
}

move_wrap(true,true,sprite_width/2);
Alarm [0] event:
GML:
canshoot = bool(true);
 
Last edited:

Nidoking

Member
if is_bool(global.canshoot = true)
global.canshoot = bool(true);
When you don't care what the answer is, as long as it's DEFINITELY either true or false.

So, clearly, you're only ever going to have one enemy, because the canshoot variable is global. All enemies will share a value. And that value is ignored, because you're always setting it to true at the start of the step event.
 

Eui

Member
When you don't care what the answer is, as long as it's DEFINITELY either true or false.

So, clearly, you're only ever going to have one enemy, because the canshoot variable is global. All enemies will share a value. And that value is ignored, because you're always setting it to true at the start of the step event.
Thank you this made me realise I didn't make a create event for the bool. But the problem now is that the alarm will not set the bool back to true after being set to false by the if. I think this is why I was trying to use global variables.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Then your problem is something other than global variables, because what you're describing mechanically works the same whether you use instance or global variables.

If you don't know why you did something, that's a problem by itself and is a clear indicator that your code needs more documentation. Because if you don't know why you're doing something, there's barely a chance of us knowing it for you.
 

Eui

Member
Then your problem is something other than global variables, because what you're describing mechanically works the same whether you use instance or global variables.

If you don't know why you did something, that's a problem by itself and is a clear indicator that your code needs more documentation. Because if you don't know why you're doing something, there's barely a chance of us knowing it for you.
Sometimes I don't really say things right I did know why I was using them. But your right my code doesn't have many comments.
 

Eui

Member
I fixed it I needed to set the alarm time again in the alarm event but I had that above the bool so it wouldn't change it. I'm very sorry for being so cryptic but thanks for the help.
 
Top