GML object.variable Best Practices Question

samspade

Member
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)?
 

NightFrost

Member
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.
 

Morendral

Member
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
 

Lukan

Gay Wizard Freak
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.
 

FrostyCat

Member
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.
 

devKathy

Member
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
 

GMWolf

aka fel666
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.
 

samspade

Member
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.
 
Top