How to create private function?

L

Lyric8925

Guest
I didn't find the way to approach it. All functions are global whether they are in script or in obj.
when project gets bigger, it will be very terrible...
(the "private" means non-global)
 
To help manage scope, I tend to make certain categories of functions methods of several controller objects. So for example, I have all the combat action functions for my turn-based combat system defined as methods on an object "act" so I reference them as "act.spear_strike" for instance. It's not a private variable, but it IS more narrowly scoped and it helps contain many functions in smaller categories so as to not get overwhelmed.
 

kburkhart84

Firehammer Games
Even for "public" functions, I always prefix things when it makes sense. Every function in my input system that the user calls is fhInputSomething(), or the gml friendlier version fhi_something(). It doesn't limit scope, but it helps with organization in that if you type out fhInput, when the auto-complete box comes up, only those functions show because it is filtered. As soon as you type the wrong letter, they go away since it filters based on what you type.

I also do the same thing for assets. For my own game, I prefix objects with obj, music with mus, and so on. It helps with the same auto-complete filtering when typing code.
 
I didn't find the way to approach it. All functions are global whether they are in script or in obj.
when project gets bigger, it will be very terrible...
(the "private" means non-global)
There is a big difference between private/public members and global/local scope. Method variables (my_method = function() {}) are not globally scoped when set in an object or struct. They are scoped to that specific instance. If you set a method to a local variable (var my_method = function() {}), it's scoped to that block of code whether it's in an object or script. You can create obj_a and obj_b, each with their own foo() method, and each one can be set to something different. Trying to call foo() from obj_c would crash the game because obj_a and obj_b's foo() methods are both locally scoped. However, you can call another object's methods via dot syntax (obj_a.foo()). Everything in GM is effectively "Public", but not global. It really isn't that big of a deal in larger projects as long as you're aware of it and not purposefully creating a massive bowl of spaghetti code.
 
Last edited:
Top