Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Execution order of Lua files — Gideros Forum

Execution order of Lua files

Another noob question (I tried to define some macros for debugging and execute them before everything else executes). It works by having a file Constants.lua and declaring dependencies. Then I finally got the Games on Gideros book (awesome, btw) and learned about init.lua, and did some research.

Based on my understanding is that init.lua is loaded first, main.lua last. Also, each file is executed after it's loaded and parsed. So in the single lua file on deployment the init.lua code would be on top, and the main.lua code on the bottom. But that's not what I see (with the windows player locally as well as pushing to the android player from windows). What am I missing?

What I did: I created a file init.lua with a single line of code
print("init.lua")
I put similar lines in other files. The result is
Bushido Battle Started.
Uploading finished.
Constants.lua
init.lua
main.lua
init.lua doesn't depend on anything.
main.lua has to depend on SceneManager to work, so it does that. (I have Scenemanager.lua among my class files, not using the plugin)

Comments

  • hgy29hgy29 Maintainer
    Accepted Answer
    Yes, your expectations are correct, init.lua is always loaded first, and main.lua last. For other files, you need to set dependencies. I never tried to make init.lua depend on something else though, maybe that would make that something else run first.
    Also those rules about main.lua and init.lua only apply to top level files, if you put them in sub directories, they lose their specificities.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29 said:

    Also those rules about main.lua and init.lua only apply to top level files, if you put them in sub directories, they lose their specificities.

    Yay, forum to the rescue again! This is the part I missed. I had them inside assets/classes/
    If I move them to the top level directory, then init.lua gets run first, and main.lua gets run last without declaring any dependencies (as expected).

    A minor question: Right-click Refresh doesn't find main.lua and init.lua in the top directory. I have to manually link them "add existing file". Is this expected behavior?

    Also, it would be nice if it applies to files in subdirectories, too, so they don't clutter the top directory. Could that be a feature request?
  • hgy29hgy29 Maintainer
    you can put them in assets, since assets is top the directory used when you do 'refresh', isn't that enough ? Making it work for subdirectories too would mean possibly many main.lua and init.lua to handle that way...
  • Yes, you are correct. Gideros Studio finds them in assets, too. I think that is enough.

    I think changing it would also likely break a lot of existing projects.

    I submitted a pull request to the documentation. Let me know if this is helpful.
    https://github.com/gideros/gideros/pull/490
Sign In or Register to comment.