Code: Select all
state = 'mainmenu'
Code: Select all
If state == 'mainmenu' then
--blah
end
Code: Select all
state = 'mainmenu'
Code: Select all
If state == 'mainmenu' then
--blah
end
Using if-else-ifs instead of a (reasonably decent) state manager has several drawbacks. The most important ones are derived from the fact that often you must make stateful decisions in several points. The typical example is when drawing and updating, but it's possible to have more.What is the difference between doing this and using a statemanager?
Would you mind providing some simple code examples or otherwise elaborating on this a little? I definitely grasp the desired result, but I'm having a little trouble figuring out the best way to actually implement this. Here's what I've come up with so far:Jasoco wrote:I can send tables back to the previous state by returning them through a states blur callback and in the previous state I place code in the focus callback to handle the commands I want to send. Basically like having a dialog that returns certain values.
Code: Select all
-- sub_menu_state.lua
function SubMenuState:makeSomeSelection()
local return_table = { selection_id = '11', selection_text = 'perform a cute twirl' }
self:getStateManager():pop(return_table)
end
Code: Select all
-- state_manager.lua
function StateManager:pop(return_table)
-- pop StateManager's top-most state.
table.remove(self.state_stack)
-- pass in the return payload returned by the previous state
self.state_stack[#self.state_stack]:focus(return_table)
end
Code: Select all
-- main_menu_state.lua
function MainMenuState:focus(return_table_from_sub_menu)
self:doSomethingWithTableReturnedByPrevState(return_table_from_sub_menu)
end
My library st8.lua can do this stuff: viewtopic.php?f=5&t=79943&hilit=state#p182150shru wrote:Would you mind providing some simple code examples or otherwise elaborating on this a little? I definitely grasp the desired result, but I'm having a little trouble figuring out the best way to actually implement this. Here's what I've come up with so far:Jasoco wrote:I can send tables back to the previous state by returning them through a states blur callback and in the previous state I place code in the focus callback to handle the commands I want to send. Basically like having a dialog that returns certain values.
Code: Select all
-- sub_menu_state.lua function SubMenuState:makeSomeSelection() local return_table = { selection_id = '11', selection_text = 'perform a cute twirl' } self:getStateManager():pop(return_table) end
Code: Select all
-- state_manager.lua function StateManager:pop(return_table) -- pop StateManager's top-most state. table.remove(self.state_stack) -- pass in the return payload returned by the previous state self.state_stack[#self.state_stack]:focus(return_table) end
Code: Select all
-- main_menu_state.lua function MainMenuState:focus(return_table_from_sub_menu) self:doSomethingWithTableReturnedByPrevState(return_table_from_sub_menu) end
Hopefully this makes sense to you. (There's definitely are some problems with this pseudocode, though. For example, since SubMenu doesn't actually know whether it's the topmost state in StateManager.)
Users browsing this forum: No registered users and 1 guest