1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GML object.variable Best Practices Question

Discussion in 'Game Design, Development And Publishing' started by samspade, Feb 7, 2020.

  1. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,231
    How do people feel about using object_id.variable to reference or change an instance variable when there is only one instance of that object (e.g. a singleton esq type object)?
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    2,101
    I use the notation only when I am sure there's only one instance, ever. For the most part that would be player characters in single-player games or controller objects that run parts of game logic. Even those I strive to encapsule inside script calls, so should a change ever be necessary I don't need to fix all around the code.
     
  3. Morendral

    Morendral Member

    Joined:
    Jun 27, 2016
    Posts:
    401
    I use it especially if one object is a proxy or stand in for another. I usually make a variable to store the unique id I'm referencing, for instance "vParent". It's easy to reference then and can be changed on the fly depending on needs
     
    SnotWaffle Studios likes this.
  4. Lukan

    Lukan Gay Wizard Freak

    Joined:
    Jun 20, 2016
    Posts:
    429
    I do this in a few places, but only in the main player object in my project, because there's only ever one.
    I usually use instance id's for direct variable manipulation.
     
  5. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,870
    I think it's perfectly fine as long as only one instance of the object exists. I use it all the time for controllers objects.
     
  6. devKathy

    devKathy Member

    Joined:
    Dec 31, 2019
    Posts:
    33
    Yeah, re-reading my old textbook, Singleton means exactly that, one instance that centrally manages something.

    I'd be a little tempted to just use a data structure, though, if we're not going to be operating on that data in a constrained way. (I.e. objects in say, Java are comprised of data and functions that operate on those data...)Are gm objects as resource-expensive as my foggy memory suggests? o_O
     
  7. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,518
    if i'm trying to be proper, i will usually either create the instance in code and assign its id to a global variable, or give it a name when creating it in the room editor.
    That way i can always access the variable through an instance id, rather than the object id.

    That being said if your object is truly a singleton (and you are sure it will stay that way) it doesnt matter much.
    And even if you change from a singleton to a non-singleton you will need just as much refactor one way or the other.
     
  8. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,231
    Thanks for all the answers.

    This is my current 'safe' method (although it would seem that in general people consider the other method safe as well as long as you're careful) as illustrated with my gameplay_state manager.

    Code:
    
    /// @function return_gameplay_state()
    /// @description Returns the gameplay state, otherwise throws and error and returns undefined
    
    with (gameplay_manager) {
        return gameplay_state;
    }
    
    show_error("No instance of gameplay_manager exists", false);
    return undefined;
    
    
    I like the idea of naming the instance and using the instance id, which would insure only checking against the first instance. Alternatively, I supposed if I wanted to add some automatic checking for number of instance:

    Code:
    
    /// @function return_gameplay_state()
    /// @description Returns the gameplay state, otherwise throws and error and returns undefined
    
    if (instance_number(gameplay_manager) > 1) {
        show_error("Too many instances of gameplay_manager", false);
    }
    
    with (gameplay_manager) {
        return gameplay_state;
    }
    
    show_error("No instance of gameplay_manager exists", false);
    return undefined;
    
    
    This is mostly just theory as the above seems unnecessarily complicated and it does seem fine to just reference the object id if there's only one of them.
     

Share This Page