GMS 2 Reliably finding same instance upon game restart

Discussion in 'Programming' started by Relic, Jul 3, 2019.

  1. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    627
    I made a great little AI system that was recording player input and using this as the basis for moving the AI around. I would start recording player input when a player moved over a node instance, X, and stop recording when the player landed on a platform, Y. This would then give the AI a set of inputs that would reliably get from node X to platform Y.

    It works great at first but breaks down over multiple compiles. I now realise it is because I was storing the data in a JSON that would use instance ids as a way of organising the data. These instance ids can change and so after adding a couple of new instances in the room editor, everything went pear shaped.

    Looking for some inspirational ideas about reliably identifying nodes and platforms across game restarts. Ideally can be stored as a single integer so I don't have to change the system too much. All the instances are in the room editor.
     
  2. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,929
    I use strings for this. They can be randomly generated or specifically designed. You could also use a custom number, but I like the readability of strings, especially for debugging purposes.

    I don't think there is a way to do it with anything built into GML.
     
    Relic likes this.
  3. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    627
    Thanks.

    I've started looking at using instance_find() as a solution. By storing the data against the "number" of the instance I care about , I hope to bypass the volatile nature of the ids. My only concern now is if instance_find() will return reliable results - even through future updates. The instances are added via the room editor. The manual explains that the instances are sorted arbitrarily - which is fine, as long as they are always sorted exactly the same way.

    Quick checks confirm that between game restarts and adding new instances in other rooms, the results are reliable. Anyone with feedback on this suggestion?
     
  4. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    887
    How about using the creation code editor in the room editor for each instance, or the variable editor, to set instance variables to unique values that makes consistent sense regardless of instance ids?
     
  5. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,156
    If the instances never overlap exactly, combining xstart, ystart and the room's name makes for a workable form of ID. I usually recommend that for tracking persistent pickups, but it may be useful here too if the instance_find() doesn't work out.
     
  6. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    627
    Both ideas have merit. The reason for not using the creation code was time saving - 10 mins coding last night has made instance_find() work with the old system. Setting unique variables manually for 20ish instances in each of the 48 rooms would work, but is tedious.

    I’m keeping the x,y combo to make an auto generating reliable id as backup. Using instance_find has the advantage of being able to grab the actual id very easily but a script to iterate over all instances to grab the real id would be fine too.

    Thanks!
     

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