使用 StateMachine 时 Love2D 中出现 Nil 值错误

发布于 2025-01-18 09:31:17 字数 2692 浏览 1 评论 0原文

我想使用“ self.player:changestate()”来称呼“实体:变化”,但该功能没有响应。我使用评论强调了问题,请仔细研究。 我尝试调试问题,但步道在“ self.player:changestate()”中消失了,idk为什么它不调用功能并给我零价值。

中键入不同文件(例如 - playstate.lua)

注意:我在使用调试器https://imgur.com/a/hmmnuph

-- PlayState.lua
-- Class library [https://github.com/vrld/hump/blob/master/class.lua][1]
PlayState = Class{}

function PlayState:init()
    self.background = math.random(3)

    self.player = Player({
        texture = 'green-alien',
        characterX = 10,
        characterY = 10,
        stateMachine = StateMachine {
            ['idle'] = function() return PlayerIdleState(self.player) end
        }
    })
    
    self.player:changeState('idle') --giving me error here
end
-- Player.lua
Player = Class{__includes = Entity}

function Player:init(def)
    Entity.init(self, def)
end

function Player:update(dt)
    Entity.update(self, dt)
end

function Player:render()
    Entity.render(self)
end


-- Entity.lua
Entity = Class{}

function Entity:init(def)
    self.characterX = def.characterX
    self.characterY = def.characterY
    self.texture = def.texture

    self.stateMachine = def.stateMachine -- [[Already checked by debugging here the argument 
                                            passed successfully refer screenshot]]

end

function Entity:changeState(state)
    self.stateMachine:change(state) -- The function that I want to call
end
-- StateMachine.lua
-- if everything goes well the value will end up here.
StateMachine = Class{}

function StateMachine:init(states)
    self.empty = {
        render = function() end,
        update = function() end,
        enter = function() end,
        exit = function() end
    }
    self.states = states or {} -- [name] -> [function that returns states]
    self.current = self.empty
end

function StateMachine:change(stateName, enterParams)
    assert(self.states[stateName]) -- state must exist!
    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

屏幕截图结果。

Error

src/states/game/PlayState.lua:15: attempt to call method 'changeState' (a nil value)


Traceback

src/states/game/PlayState.lua:15: in function 'init'
lib/class.lua:79: in function <lib/class.lua:77>
src/StateMachine.lua:17: in function 'change'
src/states/game/StartState.lua:13: in function 'update'
src/StateMachine.lua:22: in function 'update'
main.lua:45: in function 'update'
[C]: in function 'xpcall'

I want to call a "Entity:changeState" by using "self.player:changeState()" but the function is not responding. I've highlighted the problem using comment please go through it.
I tried debugging the problem but the trail got vanish in "self.player:changeState()", IDk why it's not calling the function and giving me nil value.

Note: I type my code in different files(eg - PlayState.lua)

ScreenShot result using Debugger https://imgur.com/a/HmmnuPh

-- PlayState.lua
-- Class library [https://github.com/vrld/hump/blob/master/class.lua][1]
PlayState = Class{}

function PlayState:init()
    self.background = math.random(3)

    self.player = Player({
        texture = 'green-alien',
        characterX = 10,
        characterY = 10,
        stateMachine = StateMachine {
            ['idle'] = function() return PlayerIdleState(self.player) end
        }
    })
    
    self.player:changeState('idle') --giving me error here
end
-- Player.lua
Player = Class{__includes = Entity}

function Player:init(def)
    Entity.init(self, def)
end

function Player:update(dt)
    Entity.update(self, dt)
end

function Player:render()
    Entity.render(self)
end


-- Entity.lua
Entity = Class{}

function Entity:init(def)
    self.characterX = def.characterX
    self.characterY = def.characterY
    self.texture = def.texture

    self.stateMachine = def.stateMachine -- [[Already checked by debugging here the argument 
                                            passed successfully refer screenshot]]

end

function Entity:changeState(state)
    self.stateMachine:change(state) -- The function that I want to call
end
-- StateMachine.lua
-- if everything goes well the value will end up here.
StateMachine = Class{}

function StateMachine:init(states)
    self.empty = {
        render = function() end,
        update = function() end,
        enter = function() end,
        exit = function() end
    }
    self.states = states or {} -- [name] -> [function that returns states]
    self.current = self.empty
end

function StateMachine:change(stateName, enterParams)
    assert(self.states[stateName]) -- state must exist!
    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

This is the error I got

Error

src/states/game/PlayState.lua:15: attempt to call method 'changeState' (a nil value)


Traceback

src/states/game/PlayState.lua:15: in function 'init'
lib/class.lua:79: in function <lib/class.lua:77>
src/StateMachine.lua:17: in function 'change'
src/states/game/StartState.lua:13: in function 'update'
src/StateMachine.lua:22: in function 'update'
main.lua:45: in function 'update'
[C]: in function 'xpcall'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高跟鞋的旋律 2025-01-25 09:31:17

我再次从头开始重写相同的代码,并且可以使用。也许问题在于字母的大写。

I've rewrite the same code again from scratch and it worked. Maybe the problem was in the capitalization of letters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文