r/gamemaker • u/SLStonedPanda Scripts, scripts and scripts • Sep 29 '16
Resource I created a custom alarm system, sharing here!
Why use this alarm system instead of the built in one?
Well, the biggest selling point is that you can use a gamespeed variable. For example you can run everything at 127,1976% speed if you want, or any other number, while keeping everything 60 fps, because your roomspeed doesn't change. Good for slowmo effects.
You can also change this var to 0 to freeze all alarms, which is really good for pausing the game.
Another thing is that this supports A LOT of alarms, you can even go as crazy as 1000 alarms or even higher.
Also it works exactly the same as the built in Alarms, meaning that when it triggers it will stay at -1 (I had a case where I needed that to be like that).
Anyways let's get to code
As a fan of scripts I do everything in scripts, so here's the initialize script.
///iniAlarms(amount)
//Alarm settings
AlarmAmount = argument0
for (var i = 0; i < AlarmAmount; i ++){
Alarm[i] = -1
AlarmT[i] = 1
}
This will initialize the amount of alarms you put into.
Next up, the run alarm script.
///runAlarms(object)
for (var i = 0; i < AlarmAmount; i ++){
if Alarm[i] > 0 {AlarmT[i] = 0} //Check if Alarm has been set
if Alarm[i] <= 0 && AlarmT[i] = 0{ //Trigger Alarm if under or at 0
//Reset Alarm
Alarm[i] = -1; AlarmT[i] = 1;
//Run events
if argument0 = objCont{
runAlarmEventControl(i);
}else if argument0 = objPlayer{
runAlarmEventPlayer(i);
}
}
if AlarmT[i] = 0{
Alarm[i] -= 1*global.gamespeed;} //Decrement alarm
}
This needs a little bit more explaining.
First off, you notice the variable global.gamespeed. Change or remove this to you liking, this variable makes every alarm run twice as slow as you input 0.5, or twice as fast if you input 2.
You input the name of the object you're running as argument, this is to make sure you're running the right Alarms. You need to change the "if argument0 = objCont" to whatever object names you have and the change the "runAlarmEventControl(i)" to whatever you named the script I am now going to show you next.
The actual Alarm events go into this script. I'll show you an example of my "runAlarmEventPlayer()" script:
///runAlarmEventPlayer(AlarmArrayIndex)
switch argument0{
//Alarm[0]
case 0:{
cs = 1;
}break;
//Alarm[1]
case 1:{
smoking = 0;
}break;
//Alarm[2]
case 2:{
objCont.shkShoot = 10;
}break;
//Alarm[3]
case 3:{
laserFiring = 1;
Alarm[2] = 5
objCont.shkShoot = 100;
}break;
//Alarm[4]
case 4:{
ecd = 0;
}break;
}
That's it! Those are all the scripts! Now to make it actually work. We need to initialize the alarms in the create event of all objects where you want alarms in like so:
iniAlarms(5) //0-4 are 5 alarms
if you don't want to bother changing this value higher every time you add an alarm, you can choose to just set it to a high number (no higher than 12 just to be sure).
Next up, the step event!
runAlarms(objPlayer) //Put the name of your object here.
Now add those 2 things to every object where you run alarms in.
Aaaaaannndddd that's it! You now have a working alarm system. To set alarms, simply do this.
Alarm[0] = 10;
Yes you saw that correctly, exactly the same as the built in alarm system, except you use an uppercase A instead of a lowercase A.
And there you have a custom alarm system, enjoy!
15
u/LukeLC XGASOFT Sep 29 '16 edited Sep 29 '16
Upvote because alarms are the single most useless thing in GameMaker and explaining why is probably what I do more than anything else in this subreddit, but to be honest creating a replacement system like this isn't necessary either.
Alarms are simply integer variables that, if greater than 0, decrease by 1 every step. If equal to 0, they execute the intended action and then decrease to -1 to prevent the action being executed multiple times. The code looks like this:
That's all there is to it. You have full control over the rate at which the alarm decreases, freedom to pause it or add back to the delay, and there's no limit as to how many alarms of this nature you can create.
Since this is such a fundamental operation I'd be wary of putting a lot of unnecessary code around it. It might be worth doing if your specific project calls for it for some game design reason, but building scripts around it when simpler code serves just as well 99% of the time will only clutter and slow down your project. 'For' loops are possibly one of the easiest ways to kill performance.
3
u/CivilDecay125 Sep 29 '16
wow cool, will need this in the future
1
u/flyingsaucerinvasion Sep 29 '16
instead of looping through all possible alarms, can you just keep a list of active alarms, and only loop through them?
You could run the alarm script only once on a global level, if each alarm had a variable to tell it which instance it belonged to. For example: each alarm has two additional elements, instance id, and script id. Pass alarm index as argument to the script so that the script can reset the alarm if needed.
2
u/oldmankc wanting to have made a game != wanting to make a game Sep 29 '16
If you used a DS_Map, you'd be able to give each alarm a more descriptive name than just an integer index.
1
u/Aerotactics Sep 29 '16 edited Sep 29 '16
This gives me an idea.
EDIT: I lied