Page 1 of 1

Stand-alone games as modules?

Posted: Thu Jan 03, 2019 12:30 am
by NobodysSon
A slightly belated Happy New Year! :awesome:

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.

Re: Stand-alone games as modules?

Posted: Thu Jan 03, 2019 12:54 am
by Snaker1
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".

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")
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").

Re: Stand-alone games as modules?

Posted: Thu Jan 03, 2019 3:49 am
by NobodysSon
Hey, that's an interesting method, snaker1! And it works like a charm!
Thanks!