GM:S 1.4 variable not set

Discussion in 'Programming' started by Liam Earles, Apr 15, 2019.

Tags:
  1. Liam Earles

    Liam Earles Member

    Joined:
    Jul 16, 2018
    Posts:
    25
    Hey guys! So I've been play testing one of my own games and I got an error like this:
    Code:
    ___________________________________________
    ############################################################################################
    FATAL ERROR in
    action number 3
    of  Step Event1
    for object SmashF:
    
    Variable <unknown_object>.move(100010, -2147483648) not set before reading it.
     at gml_Object_SmashF_StepBeginEvent_3 (line 8) - hsp = move * movespeed;
    ############################################################################################
    --------------------------------------------------------------------------------------------
    stack frame is
    gml_Object_SmashF_StepBeginEvent_3 (line 8)
    This error happens when I press the left and right arrow keys at the same time. Here's one of the codes that I have in the begin step event:
    Code:
    //Get the player's input
    key_right = keyboard_check(vk_right) || (gamepad_axis_value(0,gp_axislh) > 0);
    key_left = -(keyboard_check(vk_left) || (gamepad_axis_value(0,gp_axislh) < 0));
    key_jump = keyboard_check_pressed(vk_space) || (gamepad_button_check_pressed(0,gp_face1));
    
    //React to inputs
    move = key_left + key_right;
    hsp = move * movespeed;
    if (vsp < 10) vsp += grav;
    
    if (place_meeting(x,y+1,BrownBlock))
    
    {
        vsp = key_jump * -jumpspeed
    }
    //Horizontal Collision
    if (place_meeting(x+hsp,y,BrownBlock))
    {
        while(!place_meeting(x+sign(hsp),y,BrownBlock))
        {
            x += sign(hsp);
        }
        hsp = 0;
    }
    x += hsp;
    
    //Vertical Collision
    if (place_meeting(x,y+vsp,BrownBlock))
    {
        while(!place_meeting(x,y+sign(vsp),BrownBlock))
        {
            y += sign(vsp);
        }
        vsp = 0;
    }
    y += vsp;
    And here's the code for both the create and begin step:
    Code:
    ///Initialize Variables
    grav = 0;
    hsp = 0;
    vsp = 0;
    jumpspeed = 0;
    movespeed = 0;
    key_left = 0;
    key_right = 0;
    move = key_left + key_right = 0
    If you have any idea how to fix the error, let me know. Thanks.
     
  2. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,402
    There's your issue I'd say...
     
  3. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,692
    It's telling you that in your begin step event, line 8 move is not set yet. I don't actually see anything wrong in this code except that it seems unlikely that move = key_left + key_right = 0 doesn't seem likely to do what you want. However, I don't know why it should cause an error, as given that could it should just evaluate to true or 1 and be set to that.

    I'd run the debugger and see what the variables actually are.

    Also, you statement "Here's one of the codes that I have in the begin step event" is strangely worded. Do you have other code you haven't posted?
     
  4. Liam Earles

    Liam Earles Member

    Joined:
    Jul 16, 2018
    Posts:
    25
    There are few other pieces of code that I have in the begin step event. Here are some codes:
    Code:
    key_right = keyboard_check(vk_right) || (gamepad_axis_value(0,gp_axislh) > 0);
    key_left = -(keyboard_check(vk_left) || (gamepad_axis_value(0,gp_axislh) < 0));
    
    movespeed = 0
    Code:
    grav = 0
    hsp = 0
    vsp = 0
    jumpspeed = 0
    movespeed = 0
    key_left = 0;
    key_right = 0;
    move = key_left + key_right = true
    
    Code:
    var key_left = -keyboard_check(vk_left)
    var key_right = keyboard_check(vk_right)
    
    Code:
    ///Initialize Variables
    grav = 0;
    hsp = 0;
    vsp = 0;
    jumpspeed = 0;
    movespeed = 0;
    key_left = 0;
    key_right = 0;
    move = key_left + key_right = true
     
  5. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,692
    It's very difficult to tell what is what when you post code that way (though thank you for using the code blocks).

    Please post the entire event, no editing and clearly mark what event it is. For example:

    Code:
    
    /// Begin Step Event - For Object
    post all code for entire event - just do select all, copy all and paste (might need to go through and edit some of the formatting)
    
    
     
  6. Liam Earles

    Liam Earles Member

    Joined:
    Jul 16, 2018
    Posts:
    25
    Here's the codes in order:
    Code:
    key_right = keyboard_check(vk_right) || (gamepad_axis_value(0,gp_axislh) > 0);
    key_left = -(keyboard_check(vk_left) || (gamepad_axis_value(0,gp_axislh) < 0));
    
    movespeed = 0
    grav = 0
    hsp = 0
    vsp = 0
    jumpspeed = 0
    movespeed = 0
    key_left = 0;
    key_right = 0;
    move = key_left + key_right = true
    
    //Get the player's input
    key_right = keyboard_check(vk_right) || (gamepad_axis_value(0,gp_axislh) > 0);
    key_left = -(keyboard_check(vk_left) || (gamepad_axis_value(0,gp_axislh) < 0));
    key_jump = keyboard_check_pressed(vk_space) || (gamepad_button_check_pressed(0,gp_face1));
    
    //React to inputs
    move = key_left + key_right;
    hsp = move * movespeed;
    if (vsp < 10) vsp += grav;
    
    if (place_meeting(x,y+1,BrownBlock))
    
    {
        vsp = key_jump * -jumpspeed
    }
    //Horizontal Collision
    if (place_meeting(x+hsp,y,BrownBlock))
    {
        while(!place_meeting(x+sign(hsp),y,BrownBlock))
        {
            x += sign(hsp);
        }
        hsp = 0;
    }
    x += hsp;
    
    //Vertical Collision
    if (place_meeting(x,y+vsp,BrownBlock))
    {
        while(!place_meeting(x,y+sign(vsp),BrownBlock))
        {
            y += sign(vsp);
        }
        vsp = 0;
    }
    y += vsp;
    
    var key_left = -keyboard_check(vk_left)
    var key_right = keyboard_check(vk_right)
    
    ///Initialize Variables
    grav = 0;
    hsp = 0;
    vsp = 0;
    jumpspeed = 0;
    movespeed = 0;
    key_left = 0;
    key_right = 0;
    move = key_left + key_right = true
    For object - step event

    begin step event
     
    Last edited by a moderator: Apr 15, 2019
  7. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,692
    That is helpful, but I actually don't see where the error is. The error message says that move isn't set, but it does appear to be set before it is used. Best advice would be the debugger - see what values are actually there.

    One thing I did notice is you have:

    var key_left = -keyboard_check(vk_left)
    var key_right = keyboard_check(vk_right)

    at the end meaning that key_left and key_right are local variables. But you're declaring them after they've been used. I don't know how GML handles this, but it is possible that it doesn't like it. Even if it was for some reason allowed, you shouldn't do this. It's possible that this is causing some type of chain reaction - e.g. key_left and key_right are strange because they are used as instance variables but later in the same event declared as local and this messes up the setting of move which then causes an error.

    Otherwise, you could try putting move = 0; at the very top of the begin step event.

    As a side note, if all of that is really in your begin step event, that isn't great coding practice. You should declare or initialize your variables in your create evnet. That would be this part:

    Code:
    
    ///create event
    key_right = 0;
    key_left = 0;
    
    movespeed = 0
    grav = 0
    hsp = 0
    vsp = 0
    jumpspeed = 0
    movespeed = 0
    key_left = 0;
    key_right = 0;
    move = 0;
    
    
    Then use those variables later:

    Code:
    
    ///begin step event
    
    //Get the player's input
    key_right = keyboard_check(vk_right) || (gamepad_axis_value(0,gp_axislh) > 0);
    key_left = -(keyboard_check(vk_left) || (gamepad_axis_value(0,gp_axislh) < 0));
    key_jump = keyboard_check_pressed(vk_space) || (gamepad_button_check_pressed(0,gp_face1));
    
    //React to inputs
    move = key_left + key_right;
    hsp = move * movespeed;
    if (vsp < 10) vsp += grav;
    
    if (place_meeting(x,y+1,BrownBlock))
    {
       vsp = key_jump * -jumpspeed
    }
    
    //Horizontal Collision
    if (place_meeting(x+hsp,y,BrownBlock))
    {
       while(!place_meeting(x+sign(hsp),y,BrownBlock))
       {
           x += sign(hsp);
       }
       hsp = 0;
    }
    x += hsp;
    
    //Vertical Collision
    if (place_meeting(x,y+vsp,BrownBlock))
    {
       while(!place_meeting(x,y+sign(vsp),BrownBlock))
       {
           y += sign(vsp);
       }
       vsp = 0;
    }
    y += vsp;
    
    
     
  8. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,402
    This is still not valid syntax. ;)
     
  9. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,692
    Isn't it? I used to use versions of that in my projects. Though I would do it like this:

    Code:
    
    variable = (value_1 + value_2) == true;
    
    
    Maybe I used:

    Code:
    
    variable = (value_1 + value_2) >= 0.5;
    
    
    I would agree that it is bad practice, but won't GML both accept a single = as an evaluator and evaluate true/false to numbers with with == > and <?
     
  10. Liam Earles

    Liam Earles Member

    Joined:
    Jul 16, 2018
    Posts:
    25
    Okay, so I got a little bit of the error fixed, but I got another error saying this:
    Code:
    
    ___________________________________________
    ############################################################################################
    FATAL ERROR in
    action number 1
    of  Step Event1
    for object SmashF:
    
    Variable <unknown_object>.key_left(100008, -2147483648) not set before reading it.
     at gml_Object_SmashF_StepBeginEvent_1 (line 7) - move = key_left + key_right;
    ############################################################################################
    --------------------------------------------------------------------------------------------
    stack frame is
    gml_Object_SmashF_StepBeginEvent_1 (line 7)
    I do have the variables with the key_left and key_right in the create event, but it seems like it either shows the above error message, or just crashes the game. Again, this happens when I press left and right arrow keys at the same time.
     
  11. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,692
    I think at this point you just need to run the debugger. This is what it's made for.
     
  12. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,436
    key_left and key_right can be local variables; there's no issue there. They should only be declared in the current block of code that is using them. If you're going to be mixing blocks of code, though, then they'll need to be instance variables. However, once you use var anywhere in the object, then those variables become local variables and the instance variables get ignored. So, go back through your code and make sure you don't use var anywhere in SmashF's code. Until you're ready to use it, don't.

    Also, if you haven't cleaned it up by now, the only thing that should be in your Begin Step Event is:
    Code:
    ///begin step event
    
    //Get the player's input
    key_right = keyboard_check(vk_right) || (gamepad_axis_value(0,gp_axislh) > 0);
    key_left = -(keyboard_check(vk_left) || (gamepad_axis_value(0,gp_axislh) < 0));
    key_jump = keyboard_check_pressed(vk_space) || (gamepad_button_check_pressed(0,gp_face1));
    
    //React to inputs
    move = key_left + key_right;
    hsp = move * movespeed;
    if (vsp < 10) vsp += grav;
    
    if (place_meeting(x,y+1,BrownBlock))
    {
       vsp = key_jump * -jumpspeed
    }
    
    //Horizontal Collision
    if (place_meeting(x+hsp,y,BrownBlock))
    {
       while(!place_meeting(x+sign(hsp),y,BrownBlock))
       {
          x += sign(hsp);
       }
       hsp = 0;
    }
    x += hsp;
    
    //Vertical Collision
    if (place_meeting(x,y+vsp,BrownBlock))
    {
       while(!place_meeting(x,y+sign(vsp),BrownBlock))
       {
          y += sign(vsp);
       }
       vsp = 0;
    }
    y += vsp;
    Your old code was a mess and just showed you were haphazardly throwing variations of the same code around without any thought as to what they did, how they differed, and how having the same lines of code called three or four times would affect things. So make sure all those old codes are gone. Your Begin Step event should have ONE action in it -- an Execute Code action. This isn't a rule-of-thumb, it's just something I'm telling you to do because your code was a mess.
     
    Nocturne likes this.
  13. Liam Earles

    Liam Earles Member

    Joined:
    Jul 16, 2018
    Posts:
    25
    Alright, so I put the code in the begin step with nothing else, and it seems to work only once. I forgot to tell you that it could get the bug fixed by pausing and unpausing (resuming) the game, and only one time it actually worked. But then after the second time doing the glitch, it will pull up the error message or crash the game. I tried running the game in debug mode, and I'm not fully understanding the debug system. All I can/could get with the debug menu is when I performed the glitch where I press left and right at the same time then it would pull up the error and shows me the line of code in whatever event. But again, it would sometimes crash the game, which is annoying.

    I put the code that was in the begin step event in the regular step event and it doesn't work. Same goes for the end step event. Before you ask, it's the same error I've been getting with the key_left not being set.
     
  14. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,436
    How is your pause system set up?
    Is the player (i'm assuming this is in the player's object) getting deactivated or destroyed? Are you using instance_change() anywhere?

    Are you pressing right and left on the keyboard for both directions, or is one direction on the keyboard and the other on the joystick?

    It shouldn't make a difference here, but one thing that does bug some of us is the practice of making key_left a negative. Instead of using key_left=-(blahblahblah), remove the - and make key_left a positive value. Then when you set move, swap the code to

    move = key_right - key_left;

    This keeps key_left and key_right as both booleans instead of one being a boolean and the other being an integer (although yes, they are both technically still integers in GMS1.4).
     
  15. Liam Earles

    Liam Earles Member

    Joined:
    Jul 16, 2018
    Posts:
    25
    I have a separate pause object, where if you pause the game by pressing enter, it "Saves the game" and goes to the pause menu which is a separate room. If I want to go back to the game, I press enter, then it loads the "save file" which goes back to the game. The way I did the pause mechanic is with the Drag & Drop blocks.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice