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

Check if a variable exists [Solved]

B

Brandon Gale

Guest
Hey, can someone help me to check if a variable exists?

Here is my code:


Code:
if !variable_instance_exists(enemy[i], enemy_pos_x) enemy_pos_x = 0;
I have a loop that loops through all instances of objects in a room and I'm trying to make sure they all have this variable (even if it is not being used) so that I don't hit an error.

Somehow the code is not doing its job and objects are still hitting an error when they have not been previously assigned the variable.

Any ideas why? Let me know if more of my code is needed here.
 
B

Brandon Gale

Guest
enemy_pos_x isn't a string. "enemy_pos_x" is a string.
Ha! Of course it is a silly syntax error. I'm coming from programming in VBA, so the syntax adjustment is a work in progress.

I will give this a go! Thanks!

I am getting this:
Code:
FATAL ERROR in
action number 1
of  Step Event0
for object obj_player:

Variable obj_Moon.enemy_pos_x(100018, -2147483648) not set before reading it.
 at gml_Object_obj_player_Step_0 (line 14) -        enemy.enemy_pos_x += (room_width/2);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_player_Step_0 (line 14)
I guess I am having problems with variable scope. It has been an area of difficulty for me.
 
Last edited by a moderator:
B

Brandon Gale

Guest
I think I fixed it.

Code:
if !variable_instance_exists(enemy[i], enemy_pos_x) enemy_pos_x = 0;
Changed to this:
Code:
if !variable_instance_exists(enemy[i], "enemy_pos_x") enemy[i].enemy_pos_x = 0;
Using your fix and also putting the instance id in front of the variable. Thanks so much!
 
Top