Shavv
Member
GM Version: Studio (all)
Target Platform: all
Download: n/a
Links: n/a
Summary:
This guide will help you out with your projects running out of alarms to use on objects in a very neat way and will completely eliminate using alarms. Hence, provides you with a cleaner overview of your objects and will improve your project drastically.
Alarms are pretty limited in GameMaker and always have been. Thats why I have decided to post up a tutorial about how to use alarms but better, since I still see people struggeling with alarms to this day. This guide will help you out with your projects running out of alarms to use on objects and able to customize them properly.
For the custom alarms, we will not use an index of an alarm but a string. We want to make a variable out of that string, that does not have to be created before it can be used in for example the Create Event. This allows us for easy acces and setting up alarms when we need them instead of preparing them in front of time.
Since we will use strings, alarms are way easier to oversee what they are for also!
To start creating our custom alarm, we first have to create a new script with the name "timer_init". We will use the name timer for our custom alarm scripts. In that script we paste this code in there:
As you can see illustrated above, we will make a variable_name out of a argument for the script with a string "timer_" attached to the front of that argument. This will make sure all your variables in the object will not conflict with the timer names. Below that we create the timer as a variable once it does not exist. This makes sure that this script can be used when you need it and that you don't need to first initialize the timer before you can use it. This also makes the timer count down in constant running events.
Normally in GameMaker an alarm goes down by -1 every step (depending on room speed). So a good place to put your timer_init script is in the Step Event. We will use a timer called "demo".
And we are basically done setting up the timer for use in the object. Notice that you can still use the variable "demo" and have it be seperated from the timer name since we put "timer_" in front of every timer variable name.
Now we want to set the timer to a value or get its value. We can create two scripts that are quite similiar to existing scripts in gamemaker that exists for alarms already:
timer_set
timer_get
We can use the timer_set script to set a timer to a value. And use the timer_get script to get a value. As you can see in the timer_get script, it first checks if the variable exists and if it doesn't returns a -1 meaning the timer is deactivated. This way you can check on a timer without first actually setting one or even initializing it. The same goes for timer_set, but the timer doesn't run if you dont use the timer_init script.
Since we took every step to succesfully read, set and init timers we can look at a good way to use them:
- Check if a timer is running with using a timer_get on the correct string name and seeing if the value is >0
- Check if a timer is done with using a timer_get on the correct string name and seeing if the value is 0
- Check if a timer is deactivated using a timer_get on the correct string name and seeing if the value is -1
I hope this all helped you in your game dev journeys and if there are questions feel free to respond on this post
Ofcourse customize the timer scripts to fit your own projects if you miss additional features
If you have suggestions about making improvements such as increasing a argument count on the timer_init, feel free. But this tutorial has been made to be as clear and simple as it can be.
Shavv.
Target Platform: all
Download: n/a
Links: n/a
Summary:
This guide will help you out with your projects running out of alarms to use on objects in a very neat way and will completely eliminate using alarms. Hence, provides you with a cleaner overview of your objects and will improve your project drastically.
Alarms are pretty limited in GameMaker and always have been. Thats why I have decided to post up a tutorial about how to use alarms but better, since I still see people struggeling with alarms to this day. This guide will help you out with your projects running out of alarms to use on objects and able to customize them properly.
For the custom alarms, we will not use an index of an alarm but a string. We want to make a variable out of that string, that does not have to be created before it can be used in for example the Create Event. This allows us for easy acces and setting up alarms when we need them instead of preparing them in front of time.
Since we will use strings, alarms are way easier to oversee what they are for also!
To start creating our custom alarm, we first have to create a new script with the name "timer_init". We will use the name timer for our custom alarm scripts. In that script we paste this code in there:
GML:
///@description timer_init()
///@argument "timer_string"
//setting up local variable
var variable_name="timer_"+string(argument0);
//check if variable exists
if !variable_instance_exists(id,variable_name)
{
variable_instance_set(id,variable_name,-1);
}
else
{
//countdown timer and set to -1 once reached 0
if variable_instance_get(id,variable_name)>0
{
variable_instance_set(id,variable_name,variable_instance_get(id,variable_name)-1);
}
else
{
variable_instance_set(id,variable_name,-1);
}
}
As you can see illustrated above, we will make a variable_name out of a argument for the script with a string "timer_" attached to the front of that argument. This will make sure all your variables in the object will not conflict with the timer names. Below that we create the timer as a variable once it does not exist. This makes sure that this script can be used when you need it and that you don't need to first initialize the timer before you can use it. This also makes the timer count down in constant running events.
Normally in GameMaker an alarm goes down by -1 every step (depending on room speed). So a good place to put your timer_init script is in the Step Event. We will use a timer called "demo".
GML:
//timers
timer_init("demo")
And we are basically done setting up the timer for use in the object. Notice that you can still use the variable "demo" and have it be seperated from the timer name since we put "timer_" in front of every timer variable name.
Now we want to set the timer to a value or get its value. We can create two scripts that are quite similiar to existing scripts in gamemaker that exists for alarms already:
timer_set
GML:
///@description timer_get()
///@argument "timer_string"
///@argument value
//check if variable exists
var variable_name="timer_"+string(argument0);
variable_instance_set(id,variable_name,argument1);
timer_get
GML:
///@description timer_get()
///@argument "timer_string"
//check if variable exists
var variable_name="timer_"+string(argument0);
if variable_instance_exists(id,variable_name)
{
return variable_instance_get(id,variable_name);
}
else
{
return -1;
}
We can use the timer_set script to set a timer to a value. And use the timer_get script to get a value. As you can see in the timer_get script, it first checks if the variable exists and if it doesn't returns a -1 meaning the timer is deactivated. This way you can check on a timer without first actually setting one or even initializing it. The same goes for timer_set, but the timer doesn't run if you dont use the timer_init script.
Since we took every step to succesfully read, set and init timers we can look at a good way to use them:
- Check if a timer is running with using a timer_get on the correct string name and seeing if the value is >0
if timer_get("demo")>0 { /*timer is running*/}
- Check if a timer is done with using a timer_get on the correct string name and seeing if the value is 0
if timer_get("demo")=0 { /*timer is done*/}
- Check if a timer is deactivated using a timer_get on the correct string name and seeing if the value is -1
if timer_get("demo")=-1 { /*timer is not running*/}
I hope this all helped you in your game dev journeys and if there are questions feel free to respond on this post
Ofcourse customize the timer scripts to fit your own projects if you miss additional features
If you have suggestions about making improvements such as increasing a argument count on the timer_init, feel free. But this tutorial has been made to be as clear and simple as it can be.
Shavv.
Last edited: