Don’t know the real name for the functionallity in JavaScript, but it’s used like this e.g.
[javascript]
var myObj = {};
myObj.myVariable = (function()(
// … do stuff
return someObj;
}());
[/javascript]
the myObj.myVariable gets whatever value is returned without me ever having to call some function or initialize it myself.
Now to my question: Is it possible do achieve the same effect in Lua? So when I want to use a variable, its value comes from a function that’s “pre-run” and the variable already has its value.
I’m not sure if anonymous closures get garbage collected well. you’re basically creating a new function each time you call .healthBar i think. I don’t think that’s as optimized as a local setHealth function, but I couldn’t say for sure