GML How could I use the "alarm" event? [SOLVED]

G

GalGames

Guest
I'm making a "rolling shooters" for practice and I want to know how I could use the "alarms". I read the GameMaker's guide and I did not understand anything.

What I want to do is when the player shoots he has to wait 2 or 3 seconds to be able to shoot again. What have I to put in there?

My code to shoot is:

upload_2018-9-4_9-17-45.png
 

Rob

Member
Code:
if (keyShoot) and (can_shoot == 1){
      instance_create(x, y, obj_bullet);
      can_shoot = 0;
      alarm[0] = 3;
}

//Inside alarm[0]
can_shoot = 1;
This is the same as saying:
Code:
if (keyShoot) and (can_shoot == 1){
      instance_create(x, y, obj_bullet);
      can_shoot = 0;
}

if (can_shoot == 0){
      timer ++;
      if (timer >= 3){
            can_shoot = 1;
            timer = 0;
      }
}
I prefer not to use alarms because I want all of my code to be visible at the same time and not have to go hunting for it. There's also a limit on how many alarms you can use.
 
Last edited:

Simon Gust

Member
first, you need to add the alarm event in your object with following code
Code:
can_shoot = 1;
then in the code you're showing here, you set the alarm to the amount of time it should run down.
Code:
if (keyShoot and can_shoot == 1)
{
   instance_create(x, y, obj_bullet);
   can_shoot = 0;
   alarm[0] = 2 * room_speed;
}
room_speed in this case is how many frames there are per second.
so 2 times room_speed is essentially just 2 seconds.
 
G

GalGames

Guest
Code:
if (keyShoot) and (can_shoot == 1){
      instance_create(x, y, obj_bullet);
      can_shoot = 0;
      alarm[0] = 3;
}

//Inside alarm[0]
can_shoot = 1;
This is the same as saying:
Code:
if (keyShoot) and (can_shoot == 1){
      instance_create(x, y, obj_bullet);
      can_shoot = 0;
}

if (can_shoot == 0){
      timer ++;
      if (timer >= 3){
            can_shoot = 1;
            timer = 0;
      }
}
I have an error...

upload_2018-9-4_9-35-43.png
 

Rob

Member
You were only supposed to put "can_shoot = 1" into alarm[0]

The other code goes into the step event

Did you already create the variable "keyShoot" in obj_player's create event too?
 
G

GalGames

Guest
You were only supposed to put "can_shoot = 1" into alarm[0]

The other code goes into the step event

Did you already create the variable "keyShoot" in obj_player's create event too?
Yes I did! This is the code on create_event:

upload_2018-9-4_9-44-4.png

and if it can helps, here is the step_even's code:

upload_2018-9-4_9-50-45.png
 

Attachments

Top