Make sure to check out the demo. You may also use it as a starting point.
Simply copy the directory SceneManager
into your project, and use it like so:
--- main.lua
local SceneManager = require("SceneManager")
local MainMenuScene = require("scenes.MainMenu") -- assuming you have a `scenes` directory, with a `MainMenu` scene in it
local GameplayScene = require("scenes.Gameplay") -- assuming you have a `scenes` directory, with a `Gameplay` scene in it
love.load = function ()
-- register all your scenes here
SceneManager.Register(MainMenuScene)
SceneManager.Register(GameplayScene)
SceneManager.SwitchTo(MainMenuScene.name) -- switch to the default scene.
end
love.update = function (dt) SceneManager.Update(dt) end
love.draw = function () SceneManager.Draw() end
A Scene
ideally follows this contract:
local SceneManager = require("SceneManager")
local Transitions = SceneManager.Transitions
local GameplayScene = {
name = "Gameplay", -- required
transition_in = Transitions.FadeIn.New(), -- optional
transition_out = Transitions.FadeOut.New(), -- optional
}
GameplayScene.Enter = function (self) -- initialization
end
GameplayScene.Update = function (self, dt)
end
GameplayScene.Draw = function (self)
end
GameplayScene.Exit = function (self) -- cleanup
end
Enter
will be called every time you SwitchTo
into this Scene
, and Exit
will be called every time you SwitchTo
another scene (or to the same scene, in case you want to "reload" it, see GameplayScene).
In case you don't provide one or any of these functions, they will be set as __NOOP__
(empty function). The only required property is name
.
It is mandatory to Register
first all the Scenes
before Switch
ingTo
any of them. You may Register
a Scene
like so:
SceneManager.Register(GameplayScene)
Simply add the new Transition to the transitions directory, and then add it to the Transitions
table in init.lua, like so:
SceneManager.Transitions = {
FadeIn = require("SceneManager.transitions.FadeIn"),
FadeOut = require("SceneManager.transitions.FadeOut"),
DiagonalOut = require("SceneManager.transitions.DiagonalOut"),
MyTransition = require("SceneManager.transitions.MyTransition")
}
Note that the Transition
interface expects the following attributes and methods to be present:
New
, which accepts a config table, and returns a function, which returns a table. There are no required properties to be set during the constructor itself, but acompleted
(boolean) property needs to be set tofalse
andtrue
based on whatever criteria you define for that transition to have been completed, this is important becauseSceneManager
will not complete the scene switch unless it can evaluate that property as true at some point.
-- actual check happening inside of SceneManager.Update
if SceneManager._transition ~= nil then
if SceneManager._transition.completed then
-- execute logic for exiting from previous scene, and entering the new one.
end
end
SceneManager is licensed under the MIT LICENSE