Variable "not set before reading it"

Discussion in 'Programming' started by Will, Aug 7, 2016.

Thread Status:
Not open for further replies.
  1. Will

    Will Member

    Joined:
    Jun 21, 2016
    Posts:
    33
    Hey everyone,

    I was experimenting with a different system for obtaining powers in my game when I suddenly ran into this issue that I've never seen before. I've been toying with the code trying to figure it out myself but I can't seem to find what's causing the issue. It seems to be that the game isn't recognizing "obj_enemy.element", and for some reason returning "obj_enemy.<unknown variable>". Here is the error message:

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

    Variable obj_enemy.<unknown variable>(100021, -2147483648) not set before reading it.
    at gml_Object_obj_absorb_StepNormalEvent_1 (line 19) - getpower = obj_enemy.element;
    ############################################################################################

    This is odd to me because the first three lines of the create event of obj_enemy are:

    Code:
    randomize()
    
    var element;
    
    element = (irandom(9) + 1);
    obj_enemy is always on screen in this instance and obj_absorb is called by the push of a button. The error occurs when obj_absorb and obj_enemy collide. If they are not colliding, everything works as expected.

    Any ideas on a quick fix for this issue? It's fairly late at night and I'm tired so I can't help but feel that I'm overlooking something pretty simple.

    Thanks!
     
  2. bojack29

    bojack29 Member

    Joined:
    Jun 21, 2016
    Posts:
    407
    You cant 'var' a variable if you want to access it from another object. What var does is throw away a variable immediately after finishing the script. If someone tries to access the var variable, its too late and it has already been thrown away.
     
    Marvelo, ParodyKnaveBob and Will like this.
  3. Will

    Will Member

    Joined:
    Jun 21, 2016
    Posts:
    33
    Oh. Duh. I had a hunch this was a simple oversight on my end...

    Thank you!
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,435
    And after you get some sleep, pop open the Manual entries for variable scope and read up.

    I see at least 5 or 6 topics every week that are in some way related to improper scoping, and that's 100% preventable by learning the basics.
     
  5. RegieWrecker

    RegieWrecker Member

    Joined:
    Nov 6, 2018
    Posts:
    2
    hay, i have the same issue but i declared my variable without var, and i get this error : not set before reading it. It is declared in the create event and is just a standard variable...
     
  6. RegieWrecker

    RegieWrecker Member

    Joined:
    Nov 6, 2018
    Posts:
    2
    this is my error message
    ############################################################################################
    FATAL ERROR in
    action number 1
    of Step EventObjbrain
    for object objplayer:

    Variable objplayer.brians(100013, -2147483648) not set before reading it.
    at gml_Object_objplayer_Collision_bc0b8895_e6d2_4bc4_9290_7595eb0963b2 (line 1) - brains=brians+1;
    ############################################################################################
    --------------------------------------------------------------------------------------------
    stack frame is
    gml_Object_objplayer_Collision_bc0b8895_e6d2_4bc4_9290_7595eb0963b2 (line 1)
     
  7. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,980
    Check the spelling. (And also thank you for posting the error message - this is a good example of why you should always post the error message when asking for help on the forum - and also why you should read them carefully.)
     
    Michael Bateman likes this.
  8. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    909
    If you try to read a variable: THIS = THAT

    if THAT hasnt been created or set to anything before it will cause this error,

    if you do:

    var THAT;

    You havent defined the value for it, you've only created it, so reading it will cause this error.

    this only applies to "var" (local variables) as said before these are only active in the current script, ( and are the fastest to read & write )

    If your in a script and do: THIS = THAT
    and you've done:
    var THAT = 10;
    then THIS will equal 10, from the "var"
    if THAT was previously created as a normal(permanent variable) also beforehand, and the same named variable is in the script as a local variable, the permanent variable is ignored and the local one is read instead,
    this is actually really useful in some cases

    But moreover, as the question states, the clue is in the question, the variable hasn't been set before reading it, of course it will cause an error :pP
     
  9. crasshassin

    crasshassin Member

    Joined:
    Mar 20, 2019
    Posts:
    5
    if you are facing this problem in an object where you declared the variable in create and then try to use it in another variable in the same object, then the easiest solution is to associate it with the object.
    like in create event of a object named "something" it should be like for example
    Code:
    var total
    obj_something.total=10
    and then if you want use it any event just write
    Code:
    obj_something.total
    and then do any operation you want to with it
     
  10. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,845
    NOOOOOOOOOOO! This is (and sorry to say it) absolute nonsense. @FrostyCat has already referred you to the pages in the manual that explain this and I would return the favour by additionally linking to this topic of theirs: https://forum.yoyogames.com/index.php?threads/whats-the-difference-objects-and-instances.29005/

    I'm going to close this topic now, as the OP has their reply.
     
Thread Status:
Not open for further replies.

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