• 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!

Creating an alarm to call another object and make the original object disappear (then reappear)

C

Costco2

Guest
Hello, totally amateur here, so I'm working on a code that makes one object disappear (or instance destroy), then calls another object briefly to appear (for say 10 seconds). then stop and recreate the original instance and then loop.

I so far have this:

Create:
alarm[0] = 120;

Alarm[0]
instance_destroy(oBoss);

timer = 15;
timer -= 1;

if (timer > 0)
{
var location_x = random_range(0,2056);
var location_y = -2;
instance = instance_create_layer(location_x,location_y, "Rocks", oRocks)
instance.speed = 8;
instance.direction=270;
}
else
{
instance_create_layer(998,168, "Enemy", oBoss)
alarm_set(0,900)
}

}

I think I may have this wrong, I can't seem to get the instance to destroy itself nor call the other objects, any help would be great!
 

samspade

Member
If I understand it, it looks close. But there's a couple issues. First, the alarm event only fires when the alarm goes off. What this means in the code you posted is that the first time it fires, timer is set to 15, then decreased to 14, it is greater than zero so it creates some rocks and then it never resets itself or does anything else.

There are a couple ways to handle this, but probably the most straight forward is just create two alarms. So, alarm 0 looks like this:

GML:
//destroy boss
instance_destroy(oBoss);

//create rocks
var location_x = random_range(0,2056);
var location_y = -2;
instance = instance_create_layer(location_x,location_y, "Rocks", oRocks)
instance.speed = 8;
instance.direction=270;

//set alarm 1
alarm_set(1,900)
and alarm 1 looks like this:

GML:
//create boss
instance_create_layer(998,168, "Enemy", oBoss)

//set alarm 1
alarm_set(0,900)
And whatever is running this code can't be the boss or the rocks of course. And also this chain would go on forever with 0 calling 1 calling 0 calling 1 etc. so you'd want some way to stop it at some point.
 
C

Costco2

Guest
Hello, totally amateur here, so I'm working on a code that makes one object disappear (or instance destroy), then calls another object briefly to appear (for say 10 seconds). then stop and recreate the original instance and then loop.

I so far have this:

Create:
alarm[0] = 120;

Alarm[0]
instance_destroy(oBoss);

timer = 15;
timer -= 1;

if (timer > 0)
{
var location_x = random_range(0,2056);
var location_y = -2;
instance = instance_create_layer(location_x,location_y, "Rocks", oRocks)
instance.speed = 8;
instance.direction=270;
}
else
{
instance_create_layer(998,168, "Enemy", oBoss)
alarm_set(0,900)
}

}

I think I may have this wrong, I can't seem to get the instance to destroy itself nor call the other objects, any help would be great!
Thank you so much that worked! Do you happen to know what I can add to make multiple rocks appear before the timer ends, more so 1 at a time or a few at a time, I tried:

X = 20
var i;
for(i = 0; i < X; i++)

but that seems to just bring them all at once vs. at random times.

Thank you again so much for your help, I've been stumped most of the day trying on youtube/google to find a method that worked!
 
Last edited by a moderator:
C

Costco2

Guest
If you want things to happen over time, use another alarm. You've got 16 of them for each object type.

Yeah I was thinking about that but I got it, ended up just using a variable in the create function, then decreasing it with a delay in the alarm, thanks for the suggestion!
 
Top