[Solved] Access to an array of objects from a different object

Discussion in 'Programming' started by Tuxerito, Apr 17, 2019 at 12:35 AM.

Tags:
  1. Tuxerito

    Tuxerito Member

    Joined:
    Wednesday
    Posts:
    6
    Hi.

    I am trying to access to an array of objects created in an object(persistan) from another object but I can't do it.

    For example I have 3 objects(names/variables are just examples not real)

    Object 1 (Person)
    name = "";
    ___________________
    Object 2 (Game) This is a persistan object on the room 1

    person = instance_create(0, 0, Person);
    person.name = "Ben";
    persons[0] = person;

    person = instance_create(0, 0, Person);
    person.name = "Tuxerito";
    persons[1] = person;

    ___________________
    Object 3 (World) this is on room 2

    person = Game.persons[0];
    show_message(person.name); //Here I get an error on run time ("Unable to find any instance for object index '100010' name '<undefined>'")

    ___________________

    The problem is that I can´t get a Person object from the array stored in the Game object.
    if I try to check the size of the array for example if I show a message with the size I get 2
    show_message(string(array_length_1d(Game.persons)));
    That work but why I can't get the object?

    If I access to that array from the same object(Game) I can do it. In object Game
    person = persons[0];
    show_message(person.name); //Here I get "Ben"

    I have another variables in the Game object and I can access/modify its values from external objects but the only problem is with array objects.


    Thanks.
     
  2. AlexDerFerri

    AlexDerFerri Member

    Joined:
    Aug 24, 2017
    Posts:
    91
    That's because you're storing the ID of the Person object inside the array and trying to reference it inside a room where there isn't any. Make sure the Person object you've created still exists in the room you're calling it.

    This is what is happening in your code:
    ---------------------------------------------------------
    Room1:
    persons[0] = 100010;
    persons[1] = 100011;

    Room2:
    Person objects don't exist anymore
    persons[0] = undefined;
    persons[1] = undefined;​
    -------------------------------------------------------------
     
  3. Tuxerito

    Tuxerito Member

    Joined:
    Wednesday
    Posts:
    6
    Hi AlexDerFerr.

    But the Game object is persistan and all variables/instances created in room1 from that object are visible in room2.

    In room2 persons[0] and persons[1] are not undefined both of them have the instances ID for the Person objects
    persons[0] = 100010;
    persons[1] = 100011;
    I can see it using the debug mode and also if I show a message with the value stored in each position in the array like this(in room2 in the World object)
    for(var c = 0; c < array_length_1d(Game.persons) ; c++) {
    var a = Game.persons[c];
    show_message(string(a)); //Here I get the IDs 100010, 100011
    show_message(a.name); // And here I get the error when I try to get the value stored in the name variable(Unable to find any instance for object index '100010' name '<undefined>'). The "name in the error is not referring to the name variable as I said in my first message variables/names are not real"
    }


    It is not posible to do that using GML?
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,858
    Your explanation is wrong on two levels.
    • Instance IDs are being stored in the array, not object IDs. Make a distinction between objects and instances.
    • Stored instance IDs don't suddenly become undefined just because the instance stops existing. If you have someone's name card and that person dies, the card doesn't magically disappear, but you'll run into trouble if you attempt to call the number on the card.
    Other than that, your diagnosis is correct. This is a case of storing instance IDs that became stale after changing rooms.
     
  5. Tuxerito

    Tuxerito Member

    Joined:
    Wednesday
    Posts:
    6
    Hi FrostyCat.

    Did you read my last post? Just to know because you and me posted replies at the same time and probably you got a notification and you thought that was for your post jejejeje

    So I can't access to instance objects stored in an array from a different room no matter that the object is persistan?

    Instances aren't destroyed because I added code in the Destroy event(Person) to test if the instance is destroyed but nothing happens in that event unless that event is not called.
     
  6. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,858
    Those Person instances have been removed for not being persistent, but you won’t detect that with the Destroy event. Only explicit destroys trigger Destroy events, being cleaned out by changing rooms doesn’t. If you checked with instance_exists() instead, you would know they’re gone.
     
    Tuxerito likes this.
  7. Tuxerito

    Tuxerito Member

    Joined:
    Wednesday
    Posts:
    6
    Puuuuuuuuuuuuuuuffffffffffffffffffffffff

    FrostyCat you are right. I never thought that I also needed to make persistan the "Person" object ¬¬ I thought that just because the "main" object is persistan then I will be able to access to all variable/instances created on it. also because the Destroy event aren't called.

    GML is different to other programming languages XD

    I spend some days trying to find why it was not working and also I used a script/function as a workaround to initialize the array in different locations XD but for my that was just a "bad solution"

    I made persistan the object Person and now is working.

    Thank you :D
     
  8. Tuxerito

    Tuxerito Member

    Joined:
    Wednesday
    Posts:
    6
    I am new in this forum. How can I close/mark as solved this post?

    Do I need to edit the title and add "[Solved]"? because I can't see an option to mark it as solved.
     
  9. Tuxerito

    Tuxerito Member

    Joined:
    Wednesday
    Posts:
    6
    I forgot it, also thanks to AlexDerFerri :)
     

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