• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker {Solved} trouble with alarms

I am trying to control my enemy a.i with alarms but it does not seem to work


hspeed=2;
alarm[0]=roomsp*5;
if alarm[0]=0
{
sprite_index=sp_bombhead_t image_speed=.5
alarm[1]=roomsp*200
};
if alarm[1]=0{
sprite_index=sp_bombhead image_speed=.5};
if alarm[0]=-1
{alarm[0]=roomsp*5};

if any can help me i would appreciate it.
 
Is that in the step event? Because if it is then you are constantly setting the alarm[0] to roomsp*5 every step so it never reaches 0.

Instead you could try this:
Create Event:
Code:
alarm[0] = roomsp*5;
Step Event:
Code:
hspeed = 2;
Alarm[0] Event:
Code:
sprite_index = sp_bombhead_t;
image_speed = 0.5;
alarm[1] = roomsp*200;
Alarm[1] Event:
Code:
sprite_index = sp_bombhead;
image_speed = 0.5;
alarm[0] = roomsp*5;
Your coding conventions are quite weird as well, sometimes you have two pieces of code on the same line (and they're not separated by a ";") and sometimes you don't, you've also put ; after a bracket a few times (e.g. "};") which is completely unnecessary and using the actual Alarm events, while not necessary, might help you with structuring the code a bit better, rather than doing checks for -1 (which you did for alarm[1] but not for alarm[0] which is the whole reason your code wasn't working in the first place).

If you go through my code and yours, you can see that mine is basically the same, just neatened up a bit and using some proper conventions (of course, there's not only one style of coding, but the problem with your code was inconsistency: sometimes you were using convention "a" for writing code and sometimes convention "b" which is considered a no-no when coding.
 

Simon Gust

Member
It has been a long time since I've seen such hard to read code.
And I can only guess what you want to do it.
create event:
Code:
state = 0;
hspeed = 2;
alarm[0] = roomspeed * 5;
step event
Code:
if (state == 0)
{
if (alarm[0] <= 0)
{
alarm[0] = roomspeed * 200;
state = 1;
}

sprite_index = sp_bombhead;
image_speed = 0.5;
}
else
{
if (alarm[0] <= 0)
{
alarm[0] = roomspeed * 5;
state = 0;
}

sprite_index = sp_bombhead_t;
image_speed = 0.5;
}
make sure that the object has an alarm[0] event with a piece of code that says:
Code:
/// alarm[0]
 
I'm still new to actually using codes, iv been using drag in drop for years.
Is that in the step event? Because if it is then you are constantly setting the alarm[0] to roomsp*5 every step so it never reaches 0.

Instead you could try this:
Create Event:
Code:
alarm[0] = roomsp*5;
Step Event:
Code:
hspeed = 2;
Alarm[0] Event:
Code:
sprite_index = sp_bombhead_t;
image_speed = 0.5;
alarm[1] = roomsp*200;
Alarm[1] Event:
Code:
sprite_index = sp_bombhead;
image_speed = 0.5;
alarm[0] = roomsp*5;
Your coding conventions are quite weird as well, sometimes you have two pieces of code on the same line (and they're not separated by a ";") and sometimes you don't, you've also put ; after a bracket a few times (e.g. "};") which is completely unnecessary and using the actual Alarm events, while not necessary, might help you with structuring the code a bit better, rather than doing checks for -1 (which you did for alarm[1] but not for alarm[0] which is the whole reason your code wasn't working in the first place).

If you go through my code and yours, you can see that mine is basically the same, just neatened up a bit and using some proper conventions (of course, there's not only one style of coding, but the problem with your code was inconsistency: sometimes you were using convention "a" for writing code and sometimes convention "b"

I'm still new to coding, i'm fresh off drag and drop and still learning ins and outs of it all. I appreciate the help.
 
Code:
if hspeed >.1
{hspeed=0}

sprite_index = sp_bombhead_t;
image_speed = 0.2;
alarm[1] = roomsp*2;
i kinda ran into another problem where even tho i set hspeed to 0 the obj still keeps moving
 
Is that in your alarm[0] event? Also why are your trying to set hspeed to 0 if hspeed is greater than 0.1? Explain the thought process without the code, as in, what you are trying to achieve.
 
Sorry I should elaborate more, Im trying to code for the enemy to walk around then stop to have a laughing animation happen, then for him to resume moving.
I was having trouble getting the enemy to stop when I set the hspeed to 0, so I thought if I have it check if it's greater .1 it would set it to 0 since hsp is =.2
Now that you pointed it out putting >.1 doesn't seem right.
 
Ok, the problem is that hspeed is set in the step event, so setting hspeed to 0 in the alarm will make it 0 for one step (1/30th of a second or 1/60th of a second or whatever you have the FPS set to in the room) and then the next step will set it back to 2. What I would do is remove the hspeed = 2 from the Step Event and put it into the Create Event, then in your Alarm[0] Event, setting hspeed = 0 (without any if statements) should make the enemy stop. I assume that you want them to start moving again with Alarm[1]? If you do, all you need to do is set hspeed = 2 in Alarm[1] and it all should work fine.

To make it clear, this is what you need to add:

Create Event
Code:
hspeed = 2;
Alarm[0]
Code:
hspeed = 0;
Alarm[1]
Code:
hspeed = 2;
If you just add that in on top of the existing code (removing other references to hspeed when they're already there) the enemy should do what you want.
 
thank you sir you been great help, i would be honored if you can trey out my game whens its playable. only if your interested.
 
Top