编写函数来检查状态的机器当前状态 [Lua/Love2d]
Writing function to check state's machine current state [Lua/Love2d]
我正在使用 LÖVE2D 和 Lua 学习游戏开发,最近我一直在使用状态机 class。我自己没有编写 class 的代码,但我已经仔细阅读了代码,我想我几乎明白了,除了这个问题。
问题是,我正在尝试提示 class 当前状态,这样我就可以在 if 中使用它,但无论如何,我没弄对。
这里是class的相关代码:
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:is(stateName)
if self.current == self.states[stateName] then
-- this never executes
return true
end
return false
end
我已经尝试将 self.states[stateName]
更改为其他内容以对其进行测试,还尝试将内容打印到控制台以查看为什么比较永远不会正确。似乎 self.current
returns 指向 table 的指针,因此永远不会匹配比较运算符另一侧的任何内容。
感谢您的帮助!
self.current
设置为StateMachine:change
中self.states[stateName]
的return
值
function StateMachine:change(stateName, enterParams)
...
self.current = self.states[stateName]() -- note the () indicating the call
这意味着,除非 return 值为 self
,否则 self.current
将不等于它在 [= 中比较的函数或对象 self.states[stateName]
19=]
function StateMachine:is(stateName)
if self.current == self.states[stateName] then -- here we are comparing the function to the return value
我建议扩展您的状态对象以具有 :getName
函数,该函数将 return stateName
或将名称存储在您的 StateMachine
下的键下作为 currentStateName
.
我有完全相同的问题,我想补充一点——也许一些评论用我不明白的语言解释了这一点 :D——但在我创建的 getCurrentState 函数中我必须做这个:
function StateMachine:getCurrentState()
variable = self.currentStateName
return variable
其中 ofc 变量只是一些占位符。但我必须获取 self.currentStateName 指向的引用,否则比较总是失败。
我正在使用 LÖVE2D 和 Lua 学习游戏开发,最近我一直在使用状态机 class。我自己没有编写 class 的代码,但我已经仔细阅读了代码,我想我几乎明白了,除了这个问题。
问题是,我正在尝试提示 class 当前状态,这样我就可以在 if 中使用它,但无论如何,我没弄对。
这里是class的相关代码:
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:is(stateName)
if self.current == self.states[stateName] then
-- this never executes
return true
end
return false
end
我已经尝试将 self.states[stateName]
更改为其他内容以对其进行测试,还尝试将内容打印到控制台以查看为什么比较永远不会正确。似乎 self.current
returns 指向 table 的指针,因此永远不会匹配比较运算符另一侧的任何内容。
感谢您的帮助!
self.current
设置为StateMachine:change
self.states[stateName]
的return
值
function StateMachine:change(stateName, enterParams)
...
self.current = self.states[stateName]() -- note the () indicating the call
这意味着,除非 return 值为 self
,否则 self.current
将不等于它在 [= 中比较的函数或对象 self.states[stateName]
19=]
function StateMachine:is(stateName)
if self.current == self.states[stateName] then -- here we are comparing the function to the return value
我建议扩展您的状态对象以具有 :getName
函数,该函数将 return stateName
或将名称存储在您的 StateMachine
下的键下作为 currentStateName
.
我有完全相同的问题,我想补充一点——也许一些评论用我不明白的语言解释了这一点 :D——但在我创建的 getCurrentState 函数中我必须做这个:
function StateMachine:getCurrentState()
variable = self.currentStateName
return variable
其中 ofc 变量只是一些占位符。但我必须获取 self.currentStateName 指向的引用,否则比较总是失败。