GMS 2 "Variable not set before reading it" error. Me very confused...

Discussion in 'Programming' started by monkeyfrommars, Mar 16, 2019.

  1. monkeyfrommars

    monkeyfrommars Member

    Joined:
    Mar 16, 2019
    Posts:
    8
    Hello! I'm pretty new to programming, but I have this assignment that requires me to create a game from scratch. It's a top-down, pixel rpg. Anyways I have this warp block that warps the player to another room and then destroys itself when it collides with the player. Before, I had the command "instance_destroy(self);" at the end of the collide with player event, but that didnt work so i tried this:
    Create Event:
    var targetRoom, targetX, targetY, newmsg;

    Collide with player event:
    instance_create_depth(0, 0, -9999, otransition);

    room_goto (targetRoom);
    oplayer.x = targetX;
    oplayer.y = targetY;

    objective = newmsg;

    marker = true;

    Step Event:
    if (marker = true)
    {
    instance_destroy(owarp2);
    }

    It still wont destroy itself though, this has happened a few times and I dont understand how I fixed the other ones. I'm on a tremendous time crunch so I havent had time to peruse the manual im sorry i know i should have... again im fairly new to programming, but id like to think that i have a natural talent for it (evidently not, i suppose). Sorry for the long thread i tend to ramble a lot...

    EDIT:
    Whoops sorry I forgot to add the creation code for the object.
    Creation Code:
    marker = false;
    targetRoom = inschool;
    targetX = 1215;
    targetY = 720;
    newmsg = "Wait for Mom at the front";
     
    Last edited: Mar 17, 2019
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,716
    First, you need to give marker a starting value in the Create event. You can't reference a variable until you've done that.
    Code:
    marker = false;
    Second, change instance_destroy(owarp2); back to instance_destroy();. You need to learn the difference between objects and instances.

    Third, don't compare Boolean values (i.e. values that can only be true or false) to true and false. It's redundant and shows that you aren't evaluating expressions the way a computer does. This is what you should do instead, for true and false respectively:
    Code:
    if (marker)
    Code:
    if (!marker)
    Last, read these Manual articles. I'm seeing signs in your code that you aren't fully aware of either, and trouble is imminent if you don't learn both.
     
    Crazy Star likes this.
  3. monkeyfrommars

    monkeyfrommars Member

    Joined:
    Mar 16, 2019
    Posts:
    8
    Whoops sorry I forgot to add the creation code for the object.
    Creation Code:
    marker = false;
    targetRoom = inschool;
    targetX = 1215;
    targetY = 720;
    newmsg = "Wait for Mom at the front";
    Also thank you for the reply and advice. I'm... still learning.... I will change it now.
     
  4. monkeyfrommars

    monkeyfrommars Member

    Joined:
    Mar 16, 2019
    Posts:
    8
    It didn't seem to work, I don't know why lol.
    Create Event:
    var targetRoom, targetX, targetY, newmsg;

    marker = false;

    Collision Event:
    instance_create_depth(0, 0, -9999, otransition);

    room_goto (targetRoom);
    oplayer.x = targetX;
    oplayer.y = targetY;

    objective = newmsg;

    marker = true;

    Step Event:
    if (marker)
    {
    instance_destroy();
    }

    I took out the "marker = false;" from the creation code.
     
  5. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    1,955
    Have you read the Variable scope docs Frost linked?

    Especially the bit on variables you create using the "var" keyword.

    Variables created using "var" only exist inside the script or event that you created them in.

    Although as you are also creating these variables in the Creation Code of the instance, that is not directly causing the issue, but it will be a problem in future if you don't understand them, and currently is redundant code as you have written it. Plus, if any of your instances DONT have them declared in the Creation Code as you have now, it will cause errors, as the variables will only exist in the create event, and wont be available in the collision event.

    It may occur to you like a waste of time to read the manual, but doing so and understanding all the functions you are using will actually accelerate your programming speed such that the overall time you spend writing and debugging code will be reduced.

    For example, the room_goto() manual entry tells us:

    Now to be fair, its not 100% clear unless you have also read about Event order etc...

    So I'll be clear, in your collision event, you use the room_goto() command and set marker to true.

    However, what happens is, the collision event finishes, and then the room changes sometime after that, but it changes before the Step event has a chance to run again.

    So your code in the Step event where you check if marker is true and destroy the instance wont be executed, so the instance won't be destroyed.

    Although, unless you object is persistent, it should be removed from the game anyway when the room change happens.

    When you say:

    What do you mean it doesn't work exactly, and how are you wanting it to work?

    Is the marker not destroying itself causing it to appear in the next room? (This shouldnt really happen if it is not persistent. )

    On the other hand you shouldn't rely on GMS to clean up the objects for you, you should explicity clean them up via instance_destroy(), and use the CleanUp event to tidy up any memory usage/data structure it might have been using(but that is a lesson for another time as you don't appear to be using any at the moment)
     
  6. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    713
    I see absolutely nothing in your posts that actually show the error that you started the topic about: "Variable not set before reading it". How are we supposed to help you with an error when you are not even going to show us any info that we need?

    Try including the actual error message(not you retyping it as 90% of the time people don't put the exact error message as they see in the IDE), and then show us all the relevant code (and use code tags to do this, don't just put it in your posts). If you don't know how to use code tags, then go and have a read of the forum guidelines.
     
  7. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    301
    He's just refering to targetX/Y in his collision event, but they are set as temp vars in his create. Altough there is much more important lessons for him in this thread than what the OP asked.
     
  8. monkeyfrommars

    monkeyfrommars Member

    Joined:
    Mar 16, 2019
    Posts:
    8
    Wow there's so much to this that I don't know... the reason I am hesitant to read the manual is because I have trouble retaining info that I read as opposed to info I get through things like videos and such, but it seems that I might just have to deal with it... I don't know how to edit the title but it stopped giving me the error message, but the object does not destroy itself after going to the other room. I think this is due to the step event not being able to run, as IndianaBones stated above. Thank you to everyone who commented, I really appreciate it. I got it to work by having "if (objective = blah) {instance_destroy();}" and that seemed to work (i have an objective box that displays the current objective.
     
  9. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    301
    That's as good a flag as anything else if it works, I guess...
    But what I wanted to say, is you can mouse-wheel/middle click on any function and it opens the manual at the function's page. You can see the type of data it returns, the arguments that need to be provided and a (sometimes obscure, I'll give you that) example for usage. You can't go wrong with it. Official tutorials also covers a lot of grounds, and you can just open them in a side-tab and code-alomg at your pace and combine that with the middle-click function for the parts you understand less
     
  10. monkeyfrommars

    monkeyfrommars Member

    Joined:
    Mar 16, 2019
    Posts:
    8
    Yeah I use the middle click a lot. What ends up happening with anything that I do is I always try to figure it out instead of actually, like, learning so I end up doing it very inefficiently. Thank you so much for being so helpful dude.
     

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