1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 Failing to read an instance's variable

Discussion in 'Programming' started by Rowan, Sep 7, 2019.

  1. Rowan

    Rowan Member

    Joined:
    Sep 1, 2019
    Posts:
    2
    Currently trying to make it so, when my cursor is over a specific object instance (in this case, an instance of obj_cat), it should print the 'lastname' variable stored in the instance.

    I think I am having trouble with setting the variable properly, or else the scope is wrong somehow.

    The variables stored in obj_cat:

    endr = irandom_range(2,5);
    spd = irandom_range(2,5);
    agi = irandom_range(2,5);
    str = irandom_range(2,5);
    res = irandom_range(2,5);
    knw = irandom_range(2,5);
    happ = 50;
    var firstname;
    var lastname;
    firstname = catnamesfirst();
    lastname = catnameslast();

    The returned values are randomized names. The codes look similar to this (minus all the extra entries):
    names2a = ds_list_create();
    ds_list_add(names2a,"Tree");
    ds_list_add(names2a,"Fish");
    ds_list_add(names2a,"Storm");

    names2b = ds_list_create();
    ds_list_add(names2b,"catch");
    ds_list_add(names2b,"watch");
    ds_list_add(names2b,"catcher");

    ds_list_shuffle(names2a);
    ds_list_shuffle(names2b);

    var last_name;

    last_name = string(names2a[| irandom(ds_list_size(names2a)-1)])+string(names2b[| irandom(ds_list_size(names2b)-1)]);

    return last_name;

    From here, in obj_cat's Draw GUI event, I have this code:

    catsontile = ds_list_create();
    if ( place_meeting(self.x,self.y,obj_cursor) ) {
    ds_list_add(other.catsontile, id);
    }
    draw_set_font(mainfont);

    var xx = 690;
    var yy = 150;
    var i = 0;
    if(!ds_list_empty(catsontile)) {
    for(var i=0; i<=ds_list_size(catsontile); i++)
    {
    var cat = ds_list_find_value(catsontile, i);
    draw_text_transformed(xx, yy,string(cat.lastname) + " is here.", 1, 1, image_angle);
    yy += 10;
    }
    }

    The desired output here would be "[cat name] is here." at the specified location. However, instead, I get this error:

    ___________________________________________
    ############################################################################################
    FATAL ERROR in
    action number 1
    of Draw Event
    for object obj_cat:

    Variable obj_cat.lastname(100034, -2147483648) not set before reading it.
    at gml_Object_obj_cat_Draw_64 (line 14) - draw_text_transformed(xx, yy,string(cat.lastname) + " is here.", 1, 1, image_angle);
    ############################################################################################
    --------------------------------------------------------------------------------------------
    stack frame is
    gml_Object_obj_cat_Draw_64 (line 14)

    I'm not really certain why it's doing this. I imagine it's not reading in the right place... or the variables aren't being set properly...

    Any help would be appreciated!
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,694
    There are so many rookie myths in your code, I'd rather rewrite it than fix it.
    • Inappropriate var scope
    • Memory leaks from not cleaning up lists
    • Pretending that place_meeting() sets the identity of other
    • Out-of-bound access from believing that a list of size n runs from 0 to n
    In the cat's Create event, get rid of the var on firstname and lastname so that they can carry to future events like the other properties.
    Code:
    endr = irandom_range(2, 5);
    spd = irandom_range(2, 5);
    agi = irandom_range(2, 5);
    str = irandom_range(2, 5);
    res = irandom_range(2, 5);
    knw = irandom_range(2, 5);
    happ = 50;
    firstname = catnamesfirst();
    lastname = catnameslast();
    
    Then the name helpers should either change to using choose(), or if you insist on lists, clean after them before returning:
    Code:
    return choose("Tree", "Fish", "Storm")+choose("catch", "watch", "catcher");
    Code:
    var listA = ds_list_create();
    ds_list_add(listA, "Tree", "Fish", "Storm");
    var listB = ds_list_create();
    ds_list_add(listB, "catch", "watch", "catcher");
    ds_list_shuffle(listA);
    ds_list_shuffle(listB);
    var last_name = listA[| 0]+listB[| 0];
    ds_list_destroy(listA);
    ds_list_destroy(listB);
    return last_name;
    
    Then in the cursor's Draw GUI event (not the cat's), use instance_position_list() and actually loop it properly (i.e. DO NOT try to access entry n, go only to n-1 --- notice I used < where you used <=):
    Code:
    draw_set_font(mainfont);
    var xx = 690;
    var yy = 150;
    var catsontile = ds_list_create();
    var catscount = instance_place_list(x, y, obj_cat, catsontile, false);
    for (var i = 0; i < catscount; i++) {
      var cat = catsontile[| i];
      draw_text_transformed(xx, yy, cat.lastname + " is here.", 1, 1, image_angle);
      yy += 10;
    }
    ds_list_destroy(catsontile);
    
     
    Rowan likes this.
  3. Rowan

    Rowan Member

    Joined:
    Sep 1, 2019
    Posts:
    2
    The reason I used lists in the way I did is because of the amount of random name-parts :p There are like, over 100 for each! But I'll try your way. Thanks so much for the feedback, I'm very new to coding, but you could probably very easily tell that!
     

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