Hello dear Love2d community. Some time ago I've created a small states.lua lib for personal use and used that in every single of my project afterwards. Thought I might as well publish it separately on github and share with people in case they find it useful.
Here's the github link: https://github.com/Spellsweaver/States
I would be really happy if it turns out useful for someone besides me, or if someone provides useful feedback on the code.
How to use
In main.lua
states = require("states")
in love.load() call states.setup()
From now on, states library will redirect love2d callbacks from your main.lua to state files
Each state file should be located in "states/" directory and return a table of callbacks that correspond to love2d callbacks
If you want your callbacks to be state-independant, keep them in your main.lua. This way states library will not redirect them to states.
If you want a callback to have both state-dependant and state-independant part,
keep it in main.lua and call states.(callback name) within love 2d callback
To switch states (you should probably do this immediately after initialising) use states.switch(filename,params)
Filename is a name of your state file, while params is a table that will be caught by .open callback within according state file
Through params you can transfer data to your state files conveniently
My implementation of game states library
- Spellweaver
- Prole
- Posts: 8
- Joined: Fri May 08, 2020 8:42 pm
- JuanjoSalvador
- Prole
- Posts: 26
- Joined: Wed Nov 27, 2019 9:19 pm
- Location: Almeria, ES
- Contact:
Re: My implementation of game states library
Can you provide a code example?
itch.io profile - When I'm uploading my works and devblogs
GitHub - All my code is here!
Horchata - A set of Lua modules I wrote to make some task easier
GitHub - All my code is here!
Horchata - A set of Lua modules I wrote to make some task easier
Re: My implementation of game states library
The code looks interesting, thanks for sharing!
- Spellweaver
- Prole
- Posts: 8
- Joined: Fri May 08, 2020 8:42 pm
Re: My implementation of game states library
Sure. Sorry for delayed reply.
main.lua
Code: Select all
local states = require("states")
function love.load()
--some intialization happens
states.setup()
states.switch("ingame", {scale = 1})
end
Code: Select all
local scale
local ingame = {}
function ingame.open(params)
scale = params.scale
end
function ingame.draw()
--same as what would be in love.draw when you're in this state
end
return ingame
Re: My implementation of game states library
Nice, it looks similar to mine https://github.com/nekromoff/love-state-switcher
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Who is online
Users browsing this forum: No registered users and 3 guests