I'm working on my first "serious" (non-demo) project with love/lua, and I'm wondering if you guys have a preferred way of structuring your code. I'm new to lua and love, but not to programming, and I know from experience that after any project becomes sufficiently large, the code will be a mess unless you follow basic best practices. I couldn't find any wiki page of forum topics on that, so I decided to start one and give you my findings so far. Can you guys comment on my ideas below, and add your own to help me improve my skills?
After a few experiments, I decided to structure my code in different states, with the code for one give state being written each in one lua file. (I also try to hide most variables and functions as local as much as possible.) Each state implements load(), update(dt) and draw(), and the current state variable just calls the function.
My main.lua looks like this:
Code: Select all
require "menu"
local state = nil
function change_state(s)
state = s
state.load()
end
function love.load()
math.randomseed(os.time())
change_state(menu)
end
function love.draw() state.draw() end
function love.update(dt) state.update(dt) end
function love.keypressed(key)
if (key == "escape") then
if (love.event.quit) then
love.event.quit() -- 0.8
else
love.event.push("q") -- 0.7
end
else
state.keypressed(key)
end
end
Code: Select all
require "game"
require "credits"
require "instructions"
menu = {}
function menu.load()
-- init menu graphics here
end
function menu.draw()
-- menu draws here
end
function menu.update(dt)
-- menu animations here
end
function menu.keypressed(key)
if key =="g" then
change_state(game)
elseif key =="c" then
change_state(credits)
elseif key =="i" then
change_state(instructions)
end
end
Also, I noticed that I often need to switch between different "action modes" within a state. For instance, there's the initial animation of the menu screen, then the wait for a key to be pressed, then an animation to transition to a different state. Since the differences only need to be handled by the update(dt) function, I decided to have one update function for each mode, and switch between them as they finish.
Code: Select all
local update_fn
local function exit_menu_update(dt)
-- selected item jumps out and screen fades to black
-- at black, change_state(game)
end
local function wait_menu_update(dt)
-- here my menu items wiggle around their position
end
local function init_menu_update(dt)
-- here my menu items move into the screen to their position
-- when the init animation is finished: update_fn = wait_menu_update
end
function menu.update(dt) update_fn(dt) end
function menu.draw()
-- draw function is common for all update modes
end
function menu.keypressed(key)
if update_fn == wait_menu_update then
update_fn = exit_menu_update
end
end
Finally, I'm playing with the idea of using a class library. I like the ideas and the implementation of MiddleClass, and I'll probably try to use it soon.
What do you guys think? Did you come across any other useful recipes during your coding?
Thanks.