• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Truly Private Variables? IIFE

samspade

Member
In messing around I learned that the following is valid code:

GML:
//in a script asset
global_private = (function() {
    return method({private_var : "A truly private global variable"}, function() {
        return private_var;
    });
})();

//in an object
instance_prviate = (function() {
    return method({private_var : "A truly private variable"}, function() {
        return private_var;
    });
})();
This code obviously doesn't do anything useful, but you could create a whole struct that has internal variables and logic and only expose certain elements (such as a run function) and so on allowing you a truly separate system.* (since in GM you can always edit the code directly it is separate only in the sense that normal accessing patterns would be impossible).

The reason (I've been told) that you use IIFE in javascript is to hide variables away, essentially from other programmers, to keep your code clean. I would say that generally this is either unnecessary in GM (as most people using it are working in teams the size of 1 or only slightly larger) and when you need to do it, there are better ways (such as just putting it in an object and telling people not to touch it).

But it seems like this would allow you to create truly private variables that you can only interact with via specified functions outside of going in to modify the core code itself and might have some uses especially if you are creating assets for reuse (either for yourself or for others).
 
This technique is really neat. I use it all of the time in my day job. I wonder what the performance implications would be of using an IIFE. Has anyone tried it?
 
I tried it the way Sam Spade wrote it, but it did not work. However, with a tweak, I was able to get it working:

GML:
privateVar = method( { privateVar: "value" }, function() {
    return privateVar;
} )();
I tested it in a script asset, and if privateVar was a global variable in that script asset, it would return "value"
 
I tried it the way Sam Spade wrote it, but it did not work. However, with a tweak, I was able to get it working:

GML:
privateVar = method( { privateVar: "value" }, function() {
    return privateVar;
} )();
I tested it in a script asset, and if privateVar was a global variable in that script asset, it would return "value"
Were you assigning the method and the variable the same name when it didn’t work previously? A global variable kinda defeats the purpose of it being private.
 
Were you assigning the method and the variable the same name when it didn’t work previously? A global variable kinda defeats the purpose of it being private.
I assigned the variable as a global variable for ease of debugging, so I can easily see in the debugger if the value passed in matched what I put in the method. Though, I could have done so using a local or instance variable. So, I agree that having it global does defeat the purpose of being private.

Having privateVar and the struct member variable privateVar be different names would also be ideal.

Thanks for bringing that to my attention.
 
Top