Hi, I've been looking at libraries to help me with my game's internal architecture. This looks FANTASTIC for my purposes: simple to use, has enough features without being too convoluted to use (Polygamy looks very feature-rich but also too complicated for me).
Reading the code without testing it, it would appear that states' events are called in the order they're created, making addState() equivalent to a push() and destroyState() equivalent to a pop() in the call-order. If states ever need to be reordered, say because of some weird interactions with object draw order while running multiple states, you'd have to use these two functions to do so. To support this, I recommend making these changes in lovelyMoon.lua:
#141: This will return the removed state, as table.remove() returns removed tables, so if you need to store it as a local, you can. Prior to this, you'd first have to call lovelyMoon.getState(id) before calling lovelyMoon.destroyState(id).
Code: Select all
return table.remove(_slotState.states, index)
#76: If you call lovelyMoon.addState(<state_object>), it will push it to the stack *without* reinitializing the state.
Code: Select all
function lovelyMoon.addState(req, id)
local class
if type(req) == "table" then
class = req
if id then
class._id = id
end
else
class = require(req)
class._enabled = false
class._id = id
class:load()
end
table.insert(_slotState.states, class)
return class, state
end
Also, where does the variable `state` that is returned come from? It doesn't seem to do anything, and isn't set anywhere in this function.
Regarding lovelyMoon.event.update(dt), it appears that the line
followed by the checks for newState not being nil and if it has a .close and .load function, implies that writing a state object whose :update() returns *anything* will close and reload the state (if it can). I'm not sure what the purpose of this is, but it's an undocumented usage.
The usage for state:new() is also ambiguous because the code-as-written, with the examples given, appears to function around the idea of creating state classes and then modifying the single class when you require() it. I'm not sure what place that instances of the state classes has since addState() require()s the class's file and adds that to the stack, :new() doesn't do anything to add the instance to anything, states don't have any metatable stuff that calls any extant instances, and you can't add instances to the state stack without the addState() modifications I made.
Documentation in general on what the state functions do would be appreciated since this is the only link to this project that exists. Examples:
Documentation:
State objects:
Called whenever a state is created with lovelyMoon.addState(req, id).
Called whenever a state is removed with lovelyMoon.destroyState(id).
Called whenever a state is enabled with lovelyMoon.enableState(id).
Called whenever a state is disabled with lovelyMoon.disableState(id).