Variable not set before reading it (I set it just before) [SOLVED]

D

Darren

Guest
I'm getting this FATAL ERROR in action number 1 of Step Event0 for object oControl:

Variable <unknown_object>.y(1, -2147483648) not set before reading it.
at gml_Script_scr_state_ai_turn (line 28) - scr_navigation(x,y,round(nearest_move.x/32)*32, round(nearest_move.y/32)*32,pace);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_state_ai_turn (line 28)
called from - gml_Object_oControl_Step_0 (line 5) - case states.ai_turn: scr_state_ai_turn(); break;



I don't understand why, I set it right before
this is the code

nearest_target = instance_nearest(x,y,pPlayer);
nearest_move = instance_nearest(nearest_target.x,nearest_target.y,oMovesquareai);
nearest_attack = instance_nearest(x,y,oAttacksquareai);

if (type == 1)
{
if (moved == false)
{
sprite_index = run_anim;
scr_navigation(x,y,round(nearest_move.x/32)*32, round(nearest_move.y/32)*32,pace);
}

Can anybody help please? My game wont run anymore, I ran until minutes ago using this exact same code.
 

BLang

Member
Try wrapping it into an if statement with instance_exists(). The problem here is that the code can't evaluate expressions like nearest_target.x if instance_nearest(x,y,pPlayer) returned noone.

Code:
nearest_target = instance_nearest(x,y,pPlayer);

if (instance_exists(nearest_target))
{
    nearest_move = instance_nearest(nearest_target.x,nearest_target.y,oMovesquareai);
    nearest_attack = instance_nearest(x,y,oAttacksquareai);

    if (type == 1)
    {
        if (moved == false)
        {
            sprite_index = run_anim;
            scr_navigation(x,y,round(nearest_move.x/32)*32, round(nearest_move.y/32)*32,pace);
        }
    }
}
 
D

Darren

Guest
Even doing that I get exactly the same thing

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object oControl:

Variable <unknown_object>.y(1, -2147483648) not set before reading it.
at gml_Script_scr_state_ai_turn (line 32) - scr_navigation(x,y,round(nearest_move.x/32)*32, round(nearest_move.y/32)*32,pace);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_state_ai_turn (line 32)
called from - gml_Object_oControl_Step_0 (line 5) - case states.ai_turn: scr_state_ai_turn(); break;
 

BLang

Member
Oh, crud, I misread your code. You have to do that every time you try to access an instance using instance_nearest/any other instance_ function.
So, try doing

Code:
nearest_target = instance_nearest(x,y,pPlayer);

if (instance_exists(nearest_target))
{
    nearest_move = instance_nearest(nearest_target.x,nearest_target.y,oMovesquareai);
    nearest_attack = instance_nearest(x,y,oAttacksquareai);
    if (instance_exists(nearest_move))
    {
        if (type == 1)
        {
            if (moved == false)
            {
                sprite_index = run_anim;
                scr_navigation(x,y,round(nearest_move.x/32)*32, round(nearest_move.y/32)*32,pace);
            }
        }
    }
}
 
Top