Legacy GM Never Run Out Of Alarms Again!

X

Xskode

Guest
GM Version : 1.3
Target Platform : All
Download : N/A
Links : N/A

Summary:
I ran out of alarms while working on my project, because of such, I decided to make a tutorial video that shows how to program your own alarm(s) and active them!
Ladies and Gentlemen, grab a drink, open your notepad, and get ready to program your own custom alarm!

Tutorial:
 

Attachments

johnwo

Member
Warning: Constructive critizism ahead.

Nice format and presentation, but the way you implemented your custom alarms has a very hard-code-y feel to it.
Alarms are arrays, so following that convention would be wise, as it improves the amount of code you have to write, makes everything easier, and if you're using the alarm[0..11] array often, you'll be right at home using custom alarms that function the same way.

Using switches over if, when possible, should be encouraged, not discouraged; It will not create more than ONE more line of code than using IF's, and it is faster than using IF's.

IMHO; Nice tutorial for a beginner/intermediate, but I can't say that I would recommend using this in a serious project.


This is all pseudo-code, but should function with little to no modification.

Create an object.

In the create event you put:
Code:
alarmCount = 32;

for (var i=0;i<alarmCount;i++) {
    c_alarm[i] = -1;
    c_alarm_trig[i] = false;
}
In the step event you put:
Code:
for (var i=0;i<alarmCount;i++) {
    c_alarm[i] = max(c_alarm[i]-1,-1);
    if c_alarm[i] == 0 then c_alarm_trig[i] = true;
}
To set an alarm you'd simply do:
Code:
c_alarm[0..alarmCount] = someVar;
To check if an alarm was triggered you'd simply do:
Code:
if c_alarm_trig[0..alarmCount] == 0 then {
    // Do something
    c_alarm_trig[0] = false;
}
To make this even more effective, you could make a script like this, call it something like scr_alarms_triggered:
Code:
for (var i=0;i<alarmCount;i++) {
    if c_alarm_trig[i] then return i;
}
return -1;
Then, in the object's step event:
Code:
var alarm_trig = scr_alarms_triggered();
while alarm_trig != -1 {
    switch (alarm_trig) {
        case 0:
        // Alarm 0 went off...
        break;
        case 1:
        // Alarm 1 went off...
        break;
        case 2:
        // Alarm 2 went off...
        break;
        // ...
        case 31:
        // Alarm 31 went off...
        break;
        default:
        break;
    }
    c_alarm_trig[alarm_trig] = false;
    alarm_trig = scr_alarms_triggered();
}

Cheers!
 
A

Aura

Guest
You don't need to do comlicated things at all. First of all, I don't think that people run out of available alarms so often. Even if they do, they are doing something that they shouldn't per se. ~~

A better and simple way to make custom alarms would be to use an array. Or simply variables to act as countdown timers. There is no need to hold your left ear with your right hand across the head if you can simply hold it with your left hand. ^^"
 
X

Xskode

Guest
You don't need to do comlicated things at all. First of all, I don't think that people run out of available alarms so often. Even if they do, they are doing something that they shouldn't per se. ~~

A better and simple way to make custom alarms would be to use an array. Or simply variables to act as countdown timers. There is no need to hold your left ear with your right hand across the head if you can simply hold it with your left hand. ^^"
I really like how you speak your mind.
could you get me an honest feedback about my video game project that i am currently work on?
 

zbox

Member
GMC Elder
Try this out
Code:
//Create
alarms[0] = -1;

//Step
for (var i = 0; i < array_length_1d(alarms); ++i) {
    if (alarms > -1) {
        --alarms[i];
    }
    if (alarms[0] == 0)  {
        switch (i) {
            case 0:
                // do stuff for alarm 0
                break;

            case 1: 
                // do stuff for alarm 1 here
                break;
        }
    }
}

// Set alarm
alarms[0] = 60;
 
S

SagyDemn

Guest
I think that's easier.
Code:
///Create Event
ms = 0;//millisecond
maxms = 120;//maxmillisecond

///Step Event
ms ++;
if ms >= maxms{
     //Do something
     ms = 0;//Reset millisecond
}
 
I

icuurd12b42

Guest
I heard you mentioning using the draw. you really should not promote using the draw for logic that has nothing to do with drawing :)

Since everyone's putting in their 2 cents, here's a classic method If your alarms don't need to be turned off and are all ticking constantly...

create
ctr = 0;
alarm1 = room_speed*4;
alarm2 = room_speed*8;
...
alarmN = room_speed*16;

step
ctr++;
if(ctr mod alarm1 == 0)
{
show_debug_message("alarm1");
}
if(ctr mod alarm2 == 0)
{
show_debug_message("alarm2");
}
...
if(ctr mod alarmN == 0)
{
show_debug_message("alarmN");
}
 
R

Rusty

Guest
variable_a=30
variable_a-=1
if variable_a=0
{ do alarm stuff}

Edit:
This doesn't even have anything to do with alarms, this is just basic variable usage.
 

kupo15

Member
I never use arrays for my custom alarms. I think it hurts readability...I just use variables like jumpTimer, deathTimer, etc. Then I don't have to remember what alarms [7] is supposed to do...
that's a good point. I am in the arrays category right now because it makes the coding smaller and easier. But I think the readability is worth manually typing out
if jumptimer > -1 jumptimer -= 1;
else jumptimer = -1;

over and over switching out the different names

Using switches over if, when possible, should be encouraged, not discouraged; It will not create more than ONE more line of code than using IF's, and it is faster than using IF's.
I do NOT think switch statements are a good idea over ifs for this. I used that at first but its not how alarms work and will cause problems. Multiple alarms and timers can go off on the same frame and should be completely independent from each other. Switch statements will only pick one alarm that goes off per frame resulting in other alarms failing and breaking your game. On the same token, no else ifs!
 
I rarely ever use this technique but it can be helpful if you are implementing an alarm on a parent object to which their subordinate objects use alarms[0...11]. If you do not wish to track which objects use a particular alarm or be forced to reserve an alarm for a singular purpose, then I definitely recommend the custom alarm method.

I use a custom alarm to handle the enemy flash event on my own game and found it quite convenient. I am glad the OP mentioned this just in case anyone wishes to experiment its application.
 
Top