• 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] Variable not set before reading it.

D

DeadZombie

Guest
Hello, everyone!

I'm trying to understand the instance creation order and can't figure out why I can't read a variable in my case.

I've got object "obj_current_weapon" where I try to do this:
Code:
current_weapon_ammo = obj_no_weapon.ammo;
"obj_no_weapon" code:
Code:
entity = "no_weapon";
type = "weapon";
ammo = obj_no_ammo;
And when I start the game I got an error:
Variable obj_no_weapon.<unknown variable>(100025, -2147483648) not set before reading it.

All objects are in one room layer. What did I miss? I understand that obj_no_weapon's variables were not initialized when I was trying to read them, but why?
 

CloseRange

Member
your problem is simply the order that things are created.
obj_current_weapon is created before obj_no_weapon so the current_weapon code is executed first.

I belive you can go to your room -> settings -> instance order
and make the obj_no_weapon created first (it might be differant in gm2)
 
D

DeadZombie

Guest
your problem is simply the order that things are created.
obj_current_weapon is created before obj_no_weapon so the current_weapon code is executed first.

I belive you can go to your room -> settings -> instance order
and make the obj_no_weapon created first (it might be differant in gm2)
Thank you! You're right, it works. I also heard that people usually create some init_room to this purposes, just to make sure they initialize all needed instances, global variables and to be able to control their order?
 

CloseRange

Member
@DeadZombie yeah that's better practice but it can very game to game how you want to approach it.
You could simply make a script called initGame();
Code:
/// initGame();
instance_create(0, 0, obj_current_weapon);
instance_create(0, 0, obj_no_weapon);
and then the order is never off.

I think that might need to be instance_create_depth for gms2 also
 
D

DeadZombie

Guest
@DeadZombie yeah that's better practice but it can very game to game how you want to approach it.
You could simply make a script called initGame();
Code:
/// initGame();
instance_create(0, 0, obj_current_weapon);
instance_create(0, 0, obj_no_weapon);
and then the order is never off.

I think that might need to be instance_create_depth for gms2 also
Thanks a lot!
 
Top