I've been working around this problem for some time and would like to ask the masters of Gideros why it is that packages executed by Gideros at startup don't go into the package manager? This is causing my modules to be evaluated by the Lua interpreter twice which seems inefficient and forces me to either use excludefromexecution="1" for all files or to guard against double evaluation using a sentinel value. This can be very bad if you're doing one-time initialization of say, installing listeners and timers, which are then doubled since the package manager wasn't used.
Is there some reason why this would be needed? I'm still new to Lua so perhaps there's a simple answer I'm just not seeing, apologies in advance if so.
To see this in action here's an example, say you have 2 files, test.lua and main.lua. test.lua assumes it will only be evaluated once at startup or during the first require 'test.lua'. You'll see 2 different values for test printed out.
test.lua
local test = math.random()
print("test.lua loaded, value set to "..test)
return test |
main.lua
local test = require 'test' |
Comments
In Gideros you should use the scenes to divide logic of the app, and some other objects as game elements using as Gideros classes in Gideros OOP style, thus they can easily be executed on start.
If you are using some specific lua libraries that are based on lua modules, then yes you will have this problem and you should either exclude them from execution, which I think is a normal request for a 3rd party module added to the project, or create them the way, they will automatically create the global variable on start and you won't have to require them later (look at dataSaver https://github.com/ar2rsawseen/dataSaver/blob/master/dataSaver.lua).
I get that Gideros loads everything by default so all classes are ready once main.lua is invoked but I don't see why it would bypass the package manager. Seems it would be as as setting _G.package.loaded["Module"] = ... while Gideros is executing modules during startup.
Or is that impossible for Gideros to do?
Gideros executes all files, which are not set (Do not execute), including the files that contain modules
And then if you require the module again, of course this file gets executed again.
I don't think that Gideros changes any module behavior.
What would be the expected behavior in your opinion?
Thanks for your explanations.