Stand-alone games as modules?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
NobodysSon
Prole
Posts: 33
Joined: Tue Oct 30, 2012 10:37 pm

Stand-alone games as modules?

Post 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.
Snaker1
Prole
Posts: 9
Joined: Wed Dec 19, 2018 7:43 pm

Re: Stand-alone games as modules?

Post 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").
User avatar
NobodysSon
Prole
Posts: 33
Joined: Tue Oct 30, 2012 10:37 pm

Re: Stand-alone games as modules?

Post by NobodysSon »

Hey, that's an interesting method, snaker1! And it works like a charm!
Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 72 guests