1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 How do I refer to an object using it's id?

Discussion in 'Programming' started by Levi Davis, Jul 8, 2019.

  1. Levi Davis

    Levi Davis Member

    Joined:
    Jul 8, 2019
    Posts:
    4
    In my game you place down a few points and hit go, and when you hit go the character appears at point one and is supposed to move from point 1 to point 2 to point 3 etc, so I'm using instance_find() to find the id of the next point, but I'm unsure how to then save that id and use it later on to find the x and y of the object that has that id. Any ideas?
     
    Last edited: Jul 8, 2019
  2. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,317
    instance_find() returns the instance id of the found instance, or noone if none is found.

    Store the return value from the instance_find() function and then you can use it later and retrieve it's x/y position.
    Make sure you check the variable holding the return value actually contains an id before trying to use it, in case it has returned noone.

    If it's a preset series of points and you only need x/y values of each point then you could also just use a path.
     
    Last edited: Jul 8, 2019
  3. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,362
    The instance if can be used with the dot notation like so:
    Code:
    var inst = <some id>;
    var xCoord = inst.x;
    var yCoord = inst.y;
    
    Where <some id> is a function / variable that returns an instance id, like instance_find
    inst will hold the instance id.
    inst.x returns the 'x' variable of the object with id 'inst'.
     
  4. JackTurbo

    JackTurbo Member

    Joined:
    Oct 19, 2016
    Posts:
    821
    Might be a good idea to create a list or array (maybe a queue) and add the instance IDs to that as they're created.

    Then the player objects can work its way through the list rather than relying on separate objects and instance find.
     
  5. Levi Davis

    Levi Davis Member

    Joined:
    Jul 8, 2019
    Posts:
    4
    Thanks for the help, much appreciated.
     
  6. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    418
    I highly recommend storing the ID of a created instance in a variable if you are going to reference it later. Remember that instance_create returns the ID of the newly created instance. Might save you a step so you don't have to use instance_find at all!
     

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