samspade
Member
While I like using the built in alarms when possible, sometimes it isn't. And sometimes it is just fun to mess around with the new things in 2.3, so here's another way to make alarms.
While a little basic, it allows you to create alarms, set some basic values, and add and remove them from sets. Also, because you have access to the internals, they are much easier to modify (e.g. add delta time, pause, etc.) or change in other ways that you might want.
This was mostly just for fun, though I probably will use them, and I'd be curious about suggestions for additions or improvements.
While a little basic, it allows you to create alarms, set some basic values, and add and remove them from sets. Also, because you have access to the internals, they are much easier to modify (e.g. add delta time, pause, etc.) or change in other ways that you might want.
GML:
//create alarm set (not necessary)
my_alarm_set = new alarm_set();
//create an alarm
my_hello = new struct_alarm(room_speed, function() {
show_debug_message("Hello World");
my_hello.set();
});
//start an alarm
my_hello.set();
//add alarm to set
my_alarm_set.add_alarm(my_hello);
//update an alarm
my_hello.update();
//update all alarms in a set (this would actually update the my_hello alarm twice so you wouldn't want to both update a set and alarms in the set individually)
my_alarm_set.update();
GML:
// Struct Alarm Script File
function struct_alarm(_default_timer, _func) constructor {
timer = 0;
default_timer = _default_timer;
my_func = _func;
triggered = false;
static set_to = function(_value) {
timer = _value;
triggered = false;
}
static set = function() {
timer = default_timer;
triggered = false;
}
static set_default_timer = function(_value) {
default_timer = _value;
}
static set_function = function(_func) {
my_func = _func;
}
static trigger = function() {
triggered = true;
my_func();
}
static update = function() {
timer -= 1;
if (timer <= 0) && (!triggered) trigger();
}
}
function alarm_set() constructor {
alarm_amount = 0;
alarm_array = [];
static add_alarm = function(_alarm) {
alarm_array[alarm_amount++] = _alarm;
}
static remove_alarm = function(_alarm) {
var _pos = -1;
for (var i = 0; i < alarm_amount; i += 1) {
if (alarm_array[i] == _alarm) {
_pos = i;
break;
}
}
if (_pos != -1) {
var _new_array = array_create(alarm_amount - 1);
for (var i = 0; i < (alarm_amount - 1); i += 1) {
if (i < _pos) {
_new_array[i] = alarm_array[i];
} else {
_new_array[i] = alarm_array[i + 1];
}
}
alarm_array = _new_array;
}
alarm_amount--;
}
static update = function() {
for (var i = 0; i < alarm_amount; i += 1) {
alarm_array[i].update();
}
}
}
This was mostly just for fun, though I probably will use them, and I'd be curious about suggestions for additions or improvements.
Last edited: