I posted this last night in another thread but applies directly here
Sync On : Sync Rate 60
Backdrop On
global WeaponRate as integer
global WeaponTimer as integer
WeaponRate = 250 // fire rate in milliseconds, 250 = 4 shots per second
WeaponTimer=timer()
do
WeaponControl()
sync
loop
function WeaponControl()
if mouseclick()&&1
if Timer()-WeaponTimer=>WeaponRate
// Do stuff to shoot your gun
text screen width()/2,screen height()-50,"BANG!"
WeaponTimer=timer()
endif
endif
endfunction
You can also implement locking/unlocking of states, so that a pistol for instance will not fire automatically. The mouse button must be released and pressed again
Global WeaponLock as boolean
function WeaponControl()
if mouseclick()&&1
if Timer()-WeaponTimer=>WeaponRate and WeaponLock=0
// Do stuff to shoot your gun
text screen width()/2,screen height()-50,"BANG!"
WeaponTimer=timer()
WeaponLock=1
endif
else
if timer()-WeaponTimer=>WeaponRate then WeaponLock=0
endif
endfunction
However this still abides by the timer rule, so even if you mash the button you can't make it shoot faster than the fire rate specified
This code is timer based and not framerate/loop based, and will run the same on any machine. Please also note that if you run the example code with a sync rate of 0 (or greater than your monitor refresh rate) it may appear as though the firing sequence is not being activated. It is though the 'text' command is only appearing once per loop, so it may appear and disappear before the screen has a chance to refresh. I assure you it's still firing, use a sound or increase the text duration to double check