A slightly belated Happy New Year!
I have several small puzzle games that I have written each consisting of a single main.lua file and a few image and sound resources. Is there an effective way to call them as modules? I am imagining having a main.lua file that displays a simple menu and puzzle1.lua, puzzle2.lua, etc., in the same folder, and the player being able to launch them from the menu.
I've played around a bit with require() without any luck. I also wondered if os.execute() might be an option? I welcome any thoughts.
Stand-alone games as modules?
Re: Stand-alone games as modules?
The Love-Example-Browser is doing something in that direction.
You can take a look the Code here:
https://github.com/love2d-community/LOV ... e-Browser/
It might be a bit more complicated for your needs but maybe you can mod it for your use.
If your need simpler, for my snake game I put the different game states like menu, the game, the options menu and so on in different "rooms".
Obviously, depending on what callbacks you are using you might need to modify the function.
You can just put every lua file in the directory "rooms" (or maybe call it "games") and then call it with switchroom("name_of_game").
You can take a look the Code here:
https://github.com/love2d-community/LOV ... e-Browser/
It might be a bit more complicated for your needs but maybe you can mod it for your use.
If your need simpler, for my snake game I put the different game states like menu, the game, the options menu and so on in different "rooms".
Code: Select all
function switchroom(room)
-- clear old state
love.update = nil
love.draw = nil
love.keypressed = nil
-- load new room
love.filesystem.load("rooms/"..room..".lua")()
-- run the new love.load function
love.load()
end
switchroom("load")
You can just put every lua file in the directory "rooms" (or maybe call it "games") and then call it with switchroom("name_of_game").
- NobodysSon
- Prole
- Posts: 33
- Joined: Tue Oct 30, 2012 10:37 pm
Re: Stand-alone games as modules?
Hey, that's an interesting method, snaker1! And it works like a charm!
Thanks!
Thanks!
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests