ammo reload code

G

Guest User

Guest
recently I wrote a code for reloading the machine for my game Ghost team in gml:

in create obj_player: reload=0 time_reload=0 bullets=32 ammo=96 patrons=0

when you hold down the R key: if reload=0 reload=1

in step:
if reload=1{time_reload+=1 if time_reload=90{patrons=32-bullets if ammo<patrons{patrons=ammo ammo=0}
if ammo>=patrons ammo-=patrons bullets+=patrons time_reload=0reload=0patrons=0}
if time_reload=1 audio_play_sound(sound_reload,0,0)}
 

Sedgwick2K

Member
Code:
//Create event
reload = 0;
time_reload = 30; //for 60fps
clip = 32;
ammo = 96;

//R key pressed
if reload = 0
{
    reload = 1
    alarm[0] = time_reload;
    audio_play_sound(sound_reload,0,0)
}

//alarm[0]
ammo -= clip;
clip = 32;
I would write it like this.
I didn't understand the "patrons" variable, so I eliminated it from code. I used "time_reload" as the lifespan of alarm[0], which will make the gun reload at the end. I used "clip" variable for single clip instead of "bullets", since it can be a little bit confusing.
And depending on your architecture, you might not even need "reload" variable. You just can set alarm[0] and disable shooting when alarm[0] is not equal to time_reload.
And writing code side-by-side makes its reading harder.
That's my 2 cents on this code. Good job anyways, hope you'll get better in making games.🙌
 
Top