Page 1 of 1

changing lua file as main thread

Posted: Sat Aug 31, 2024 9:45 pm
by elbrunzy
Hi

I realize setting up the structure of my game project menu/animation/game mechanism as three independent love2d executable and relying on os.execute to call each program have some drawback and trying to solve them I read something about running all the .lua file inside a single love thread.

Reading about thread system of love2d I wonder if I could have the main thread reload itself using a new .lua file. The main menu program would be called main.lua and would eventually ask the main love thread to restart itself using a new anim.lua or game.lua file.

Maybe another method could be to have all three thread running together but having them paused and started with the window passed to each of them. But I dont need any inter process since all logic in each program is coded to exchange data using the game save file that is in the same love.filesystem via a shared identity, it is read at startup of each program. Maybe it would be easier to just restart using a different .lua file for main thread.

Thank you

Re: changing lua file as main thread

Posted: Sun Sep 01, 2024 10:59 am
by dusoft
I think you are overcomplicating here. What you are after are gamestates.

Check these libs (search for gamestate):
https://github.com/love2d-community/awesome-love2d

See e.g. how hump manages game states:
https://hump.readthedocs.io/en/latest/gamestate.html

Yes, you have separate files, but no filesystem calls are necessary, you just basically redeclare used functions.

Edit: Instead of love.filesystem and using external library, you can use require function: https://www.lua.org/pil/8.1.html
Just redeclare love.update, love.draw etc. functions in each file and you are all set, call require when ready to switch the game state.

That's also how my primitive state switcher used to work (do not use it, ugly, outdated etc.!): https://ambience.sk/love2d-a-state-switcher-class-lua/