GMS 2 [HELP] Reload System

C

Cmdr_Firezone38

Guest
Hello everyone,

I am new around here and with GML as well. So I am trying to make a reloading system for when my gun runs out of ammo. What I want to happen is when the "reload" variable gets to zero or when the user presses the "R" button, alarm[0] will get set to 2 seconds and then 11 will be put into "reload".

The issue is that alarm[0] is not working how I thought it would. When I press "R" or when "reload" gets to zero nothing happens. I no longer have any bullets.

If anyone has any ideas of how this could be fixed I would very much appreciate it!

Thank you!

Here is what I have right now:

Create Event -
spd = 4;
hspd = 0;
vspd = 0;
reload = 11;
reloadT = room_speed * 2;

Alarm 0 -
reload = 11;

Step -
move_state();

if(mouse_check_button_pressed(mb_left) and (reload > 0))
{
instance_create_layer(obj_player.x, obj_player.y, "Layer_Bullet", obj_bullet);
reload -= 1;
}

if(keyboard_check_pressed(ord("R")) or (reload == 0))
{
alarm[0] = reloadT;
}
 
A

Aura

Guest
A couple of logic overlooks. You want to set the alarm when the key is pressed AND reload is equal to 0. Currently, using or makes the alarm to get reset every step as long as reload is 0. You also need to check if the alarm isn't already active, otherwise it would keep getting reset.

Code:
if (keyboard_check_pressed(ord("R")) and (reload == 0)) {
   if (alarm[0] == -1) {
      alarm[0] = reloadT;
   }
}
 
C

Cmdr_Firezone38

Guest
A couple of logic overlooks. You want to set the alarm when the key is pressed AND reload is equal to 0. Currently, using or makes the alarm to get reset every step as long as reload is 0. You also need to check if the alarm isn't already active, otherwise it would keep getting reset.

Code:
if (keyboard_check_pressed(ord("R")) and (reload == 0)) {
   if (alarm[0] == -1) {
      alarm[0] = reloadT;
   }
}
Thank you very much for your help! I now understand alarms a lot better than before and I was able to make a pretty good (at least for my first try :) ) reload system. I was able to make it to where you can reload even if you are not out of bullets (it will pause for the reload time) and then when you are out of ammo it will reload automatically.

Here is the code:

Code:
if(mouse_check_button_pressed(mb_left) and (reload > 0) and (alarm[0] == -1))
{
    instance_create_layer(obj_player.x, obj_player.y, "Layer_Bullet", obj_bullet);
    reload -= 1;
}


if(keyboard_check_pressed(ord("R")))
{
    if(alarm[0] == -1)
    {
        alarm[0] = reloadT;
    }
}


if(reload == 0)
{
    if(alarm[0] == -1)
    {
        alarm[0] = reloadT;
    }
}
 
Top