• 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!

[SOLVED] Alarm Firerate Issue

S

SilentCipher

Guest
Hello everyone, i have a shooting mechanic i'm trying to get down for game, and i'm trying to use an alarm to do what i want. I'm having a hard time, was wondering if someone could maybe explain what i'm doing wrong, or explain how to fix what i'm doing.

Also, it is my first time posting, i read the guidelines, i am trying my best.

What i'm trying to do: Essentially what i'm trying to do is make it to where, the player can hold down space bar, and the game will keep shooting for them as long as they hold space bar, but in intervals. I don't want there to be a giant line of bullets, instead i want it, as example, fire a bullet, then 30 frames later, essentially half a second, another bullet goes out.(edited)
Here is a video of how this code is running in game, as you can see i'm also having this issue where, the alarm with run out, and create a new bullet along with the alarm bullet.

Version: Game Maker studio 2 vr 2.2.1.291

Code -

step event
Code:
if (keyboard_check(vk_space)) {
   if (alarm_get(0) > 0) {
       show_debug_message("Alarm Hasn't hit Zero");
       exit;
   } else {
       var _bullet = instance_create_layer(x, y, "Instances", obj_friendly_bullet);
       _bullet.speed = 10;
       alarm_set(0, 30);
       show_debug_message("Alarm Created");
   }
}
alarm[0]
Code:
var _bullet = instance_create_layer(x, y, "Instances", obj_friendly_bullet);
_bullet.speed = 10;

If I am missing any information that you would need please let me know, i'm still ongoing with attempting to solve this on my own. I've spent already 2 hours, and still haven't gotten what i'm aiming for lol. :(

Thus i resorted to coming here for the first time.

PS, im still pretty new to alarms, i feel like i understand the basic of what it is essentially.
 

Slyddar

Member
Sometimes you can just use a variable instead of an alarm.
Set 2 variables in create called
Code:
//create
fire_delay_initial = 10;
fire_delay = fire_delay_initial;
Code:
//step
if fire_delay <= 0 {
  //reset delay for next bullet
  fire_delay = fire_delay_initial;

  //create the bullet
  var _bullet = instance_create_layer(x, y, "Instances", obj_friendly_bullet);
  _bullet.speed = 10;
} else fire_delay--;
The rate of fire is controlled by fire_delay_initial. The bullet will fire if fire_delay is below/equal to zero, if not it minuses 1 off fire_delay. Once it fires, fire_delay is then reset to the initial value for the next bullet.
 
S

SilentCipher

Guest
I just got done solving it myself.

I literally used this, I forgot about keyboard_check_pressed();
 
Top