How do you fix StateMachine.lua 20 attempt to method 'enter' (a nil value)

Viewed 10

I'm working on a Flappy Bird Remake and I've been having a problem with the state machine the error I been getting is

Error

StateMachine.lua:20: attempt to call method 'enter' (a nil value)

Traceback [love "callbacks.lua"]:228: in function 'handler'

StateMachine.lua:20: in function 'change' main.lua:76: in function 'load' [love "callbacks.lua"]:136: in function <[love "callbacks.lua"]:135> [C]: in function 'xpcall' [C]: in function 'xpcall'

Code of the State Machine

StateMachine = Class{}

function StateMachine:init(states)
    self.empty = {
        render = function() end,
        update = function() end,
        enter = function() end,
        exit = function() end
    }

    self.states = states or {}
    self.current = self.empty
end

function StateMachine:change(stateName, enterParams)
    assert(self.states[stateName])
    self.current:exit()
    self.current = self.states[stateName]()
    self.current:enter(enterParams)
end

function StateMachine:update(dt)
    self.current:update(dt)
end

function StateMachine:render()
    self.current:render()
end

Code in Main

 gStateMachine = StateMachine {
        ['title'] = function() return TitleScreenState() end,
        ['play'] = function() return PlayState() end,
        ['score'] = function() return ScoreState() end,
    
    }

    gStateMachine:change('title')
0 Answers
Related