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

GML Problem With Alarms

M

MrSanfrinsisco

Guest
I'm creating a game called bomb run and in the bomb spawner object I have 3 alarms. One alarm creates the bomb somewhere random in the map, another alarm decreases the spawn rate of the bombs but does not let it hit 0, and the third one makes the bomb animation play faster so the bombs will explode faster and faster as the game plays on. That's where I'm having problems Let me show my code and then explain what's going wrong.
Create Event
Code:
spawnRate = 60;
spawnRateDecrease = 60*7;
bombExplodeRate = 60;
imageSpeed = 1;
alarm[0] = spawnRate;
alarm[1] = spawnRateDecrease;
alarm[2] = bombExplodeRate;
Alarm 0 Event
Code:
with (instance_create_layer(random(room_width), random(room_height),
                                            "lyr_bombs", obj_bomb)) {
    image_speed = imageSpeed;
}
alarm[0] = spawnRate;
Alarm 2 Event
Code:
imageSpeed += 2;
alarm[2] = bombExplodeRate;
Here's the error I'm getting as soon as a bomb spawns in the game:
############################################################################################
FATAL ERROR in
action number 1
of Alarm Event for alarm 0
for object obj_bomb_spawner:

Variable obj_bomb.imageSpeed(100012, -2147483648) not set before reading it.
at gml_Object_obj_bomb_spawner_Alarm_0 (line 3) - image_speed = imageSpeed;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_bomb_spawner_Alarm_0 (line 3)

The error is saying that imageSpeed is not set before reading it but if you look in the create event imageSpeed clearly is set to 1. So I'm failing to understand what's going wrong here.
Ignore the imageSpeed += 2; I know in Game Maker studio 2 it works as a multiplier, The variables I have set right now are for testing.
 

Simon Gust

Member
If you are using a with() statement your code flow switches over to the new instance you created there.
Meaning it will try to set it's own image_speed to it's own imageSpeed.
Of course, it doesn't have a variable called imageSpeed.
Either use a dot-operator, have imageSpeed be of local scope or scope the variable with the "other" operator.
 
M

MrSanfrinsisco

Guest
If you are using a with() statement your code flow switches over to the new instance you created there.
Meaning it will try to set it's own image_speed to it's own imageSpeed.
Of course, it doesn't have a variable called imageSpeed.
Either use a dot-operator, have imageSpeed be of local scope or scope the variable with the "other" operator.
So trying to understand, I would do something like?:
Code:
with (instance_create_layer(random(room_width), random(room_height),
                                            "lyr_bombs", obj_bomb.image_speed = imageSpeed))
 

Simon Gust

Member
So trying to understand, I would do something like?:
Code:
with (instance_create_layer(random(room_width), random(room_height),
                                            "lyr_bombs", obj_bomb.image_speed = imageSpeed))
That doesn't make a whole lot of sense.
try
Code:
var xx = irandom(room_width);
var yy = irandom(room_height);

var inst = instance_create_layer(xx, yy, "lyr_bombs", obj_bomb);
    inst.image_speed = imageSpeed; // inst holds the id of the new instance, variables can be set to it.
or
Code:
var xx = irandom(room_width);
var yy = irandom(room_height);

with (instance_create_layer(xx, yy, "lyr_bombs", obj_bomb))
{
    image_speed = other.imageSpeed; // other refers to the owner of this code window
}
or
Code:
var xx = irandom(room_width);
var yy = irandom(room_height);
var img_spd = imageSpeed; // make a local variable of an existing variable

with (instance_create_layer(xx, yy, "lyr_bombs", obj_bomb))
{
    image_speed = img_spd; // local scope comes trough with statements
}
It is generally good practice to keep code clear and spread it out.
 
M

MrSanfrinsisco

Guest
That doesn't make a whole lot of sense.
try
Code:
var xx = irandom(room_width);
var yy = irandom(room_height);

var inst = instance_create_layer(xx, yy, "lyr_bombs", obj_bomb);
    inst.image_speed = imageSpeed; // inst holds the id of the new instance, variables can be set to it.
or
Code:
var xx = irandom(room_width);
var yy = irandom(room_height);

with (instance_create_layer(xx, yy, "lyr_bombs", obj_bomb))
{
    image_speed = other.imageSpeed; // other refers to the owner of this code window
}
or
Code:
var xx = irandom(room_width);
var yy = irandom(room_height);
var img_spd = imageSpeed; // make a local variable of an existing variable

with (instance_create_layer(xx, yy, "lyr_bombs", obj_bomb))
{
    image_speed = img_spd; // local scope comes trough with statements
}
It is generally good practice to keep code clear and spread it out.
Okay I see what you mean. I understand what the code is saying but I'm still getting the same result for all 3. I keep getting "imageSpeed not set before reading it" error
 
Top