Scenery - A dead simple Love2D SceneManager
Scenery - A dead simple Love2D SceneManager
Scenery is a dead simple Löve2D SceneManager. It can be found at https://github.com/paltze/scenery. Love2D does not have scene or state system and I loved it when I used to develop in https://phaser.io. And so I used a variation of this library for a long time when I used to develope in Löve. I know any experienced dev, with a bit of time, will be easily able to write it; but not everyone is experienced in this world. And everyone likes being lazy . I hope, in sharing this library, that it will be helpful to the lövely people who use Löve2D.
-
- Party member
- Posts: 356
- Joined: Wed Jul 03, 2013 4:06 am
Re: Scenery - A dead simple Love2D SceneManager
Not sure how yours work, but here's one I've been using on my own that I created awhile back.
Here's how it works, this would be a file named scenes.lua.
and you would do something like
In the same directory as you main.lua, create a folder named scenes. Every .lua file in this folder gets automatically loaded, no need to have a path or file name or set it. As long as you call Scenes:init(), it will autoload everything and get it ready to be called when you need to switch scenes.
Put scenes in the folder, scenes are just lua files, that look like this-
And that's it. Now, when you want to switch states you just call
Scenes:switch("newSceneName")
It's all stack based, so you can push onto the stack and have it return to the last state by popping it, using
Scenes:push("newSceneName")
and then when you're done
Scenes:pop()
goes to the last state loaded. Very handy for in game GUI's.
Hopefully this little snippet of code can be of use to you, and has stuff you can add to your own library.
Here's how it works, this would be a file named scenes.lua.
Code: Select all
return{
states={},
focus={},
action={switch=false, push=false, pop=false, newid=0},
init=function(self, start_state)
--gets all the game states from the game states directory
for i,v in ipairs(love.filesystem.getDirectoryItems("scenes")) do
if string.find(v, ".lua") then
self.states[string.gsub(v, ".lua", "")]=require("scenes." .. string.gsub(v, ".lua", ""))
end
end
if start_state then
self:push(start_state)
end
end,
push=function(self, state)
self.states[state]:init()
self.focus[#self.focus+1]=state
end,
pop=function(self)
local cfocus=self:currentFocus()
if #self.focus>1 then
if(self.states[cfocus].close~=nil) then
self.states[cfocus]:close()
end
self.focus[#self.focus]=nil
end
end,
switch=function(self, state)
for i,v in ipairs(self.focus) do
self.focus[i]=nil
end
self.focus={}
self:push(state)
end,
currentFocus=function(self)
return self.focus[#self.focus]
end,
update=function(self, dt)
self.states[self:currentFocus()]:update(dt)
end,
draw=function(self)
for i,v in pairs(self.focus) do
self.states[v]:draw()
end
end,
}
Code: Select all
Scenes=require "scenes"
function love.load(args)
Scenes:init("start") --or whatever your first state is called. This is the same name as the .lua file.
end
function love.update(dt)
Scenes:update(dt)
end
function love.draw()
Scenes:draw()
end
Put scenes in the folder, scenes are just lua files, that look like this-
Code: Select all
return {
init=function()
end,
update=function(dt)
end,
draw=function()
end,
}
Scenes:switch("newSceneName")
It's all stack based, so you can push onto the stack and have it return to the last state by popping it, using
Scenes:push("newSceneName")
and then when you're done
Scenes:pop()
goes to the last state loaded. Very handy for in game GUI's.
Hopefully this little snippet of code can be of use to you, and has stuff you can add to your own library.
Re: Scenery - A dead simple Love2D SceneManager
Your idea of automatically loading scenes is indeed excellent, but that forces the users to change their workflow and store their scenes in a particular folder. But I think I can find a way where they can choose whether to load automatically or not. Thank you for your valuable inputs.
-
- Party member
- Posts: 356
- Joined: Wed Jul 03, 2013 4:06 am
Re: Scenery - A dead simple Love2D SceneManager
Of course! I wasn't expecting you to use my code or not, but I figure some of what I've done might be helpful to you, and if that's the case, excellent. And if now, no big deal at all.
I'm glad it gave you some ideas, though. Yay for that
I'm glad it gave you some ideas, though. Yay for that
Who is online
Users browsing this forum: No registered users and 3 guests