samspade
Member
In messing around I learned that the following is valid code:
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).
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;
});
})();
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).