Lua - table 不会从函数中插入
Lua - table won't insert from function
我有一个 Lua 函数,我在其中构建一个 table 值并尝试将其添加到具有命名键的全局 table。
键名是从函数参数中提取的。基本上,它是一个文件名,我将它与有关文件的数据配对。
不幸的是,全局 table 总是返回 nil。这是我的代码:(如果您需要查看更多,请告诉我)
(注释部分为其他尝试,虽然已经删除了很多尝试)
Animator = Class{}
function Animator:init(atlasfile, stringatlasfriendlyname, totalanimationstates, numberofframesperstate, booleanstatictilesize)
-- Define the Animator's operation mode. Either static tile size or variable.
if booleanstatictilesize ~= false then
self.isTileSizeStatic = true
else
self.isTileSizeStatic = false
end
-- Define the total animation states (walking left, walking right, up down, etc.)
-- And then the total frames per state.
self.numAnimationStates = totalanimationstates or 1
self.numAnimationFrames = numberofframesperstate or 2
-- Assign the actual atlas file and give it a programmer-friendly name.
self.atlasname = stringatlasfriendlyname or removeFileExtension(atlasfile, 'animation')
generateAnimationQuads(atlasfile, self.atlasname, self.numAnimationStates, self.numAnimationFrames)
end
function generateAnimationQuads(atlasfile, atlasfriendlyname, states, frames)
spriteWidthDivider = atlasfile:getWidth() / frames
spriteHeightDivider = atlasfile:getHeight() / states
animationQuadArray = generateQuads(atlasfile, spriteWidthDivider, spriteHeightDivider)
animationSetValues = {atlasarray = animationQuadArray, width = spriteWidthDivider, height = spriteHeightDivider}
--gAnimationSets[#gAnimationSets+1] = atlasfriendlyname
gAnimationSets[atlasfriendlyname] = animationSetValues
--table.insert(gAnimationSets, atlasfriendlyname)
end
注意:使用print(atlasfriendlyname) 和print(animationSetValues) 时,两者都不是空的或nil。它们都包含值。
由于某种原因,将密钥对分配给 gAnimationSets 的行不起作用。
gAnimationSets 在 main.lua 中的程序顶部定义了一次,使用
gAnimationSets = {}
Animator class 在名为 Bug 的角色 class 的 init() 函数期间被调用。 Bug class 在 StartState 的 init() 函数中初始化,该函数扩展自 BaseState,它简单地定义了 dummy init()、enter()、update() 等函数。
StartState 在 main.lua 中使用 StateMachine class 调用,它作为在 main.lua 中声明的全局 table 的值传递到 StateMachine。
gAnimationSets 在 table 状态之后和调用状态之前声明。
这是使用 Love2D 引擎。
抱歉,我是来这里寻求帮助的,我已经纠结了几个小时了。
编辑:更多测试。
尝试在索引 gTextures['buganimation'] 处打印 animationQuadArray 始终 returns nil。嗯?
这是 Main.lua
中的 gTexture
gTextures = {
['background'] = love.graphics.newImage('graphics/background.png'),
['main'] = love.graphics.newImage('graphics/breakout.png'),
['arrows'] = love.graphics.newImage('graphics/arrows.png'),
['hearts'] = love.graphics.newImage('graphics/hearts.png'),
['particle'] = love.graphics.newImage('graphics/particle.png'),
['buganimation'] = love.graphics.newImage('graphics/buganimation.png')
}
正在尝试 return gTextures['buganimation'] returns 正常的文件值。不是空的。
我现在脑子都炸了,我都想不起来我为什么来编辑这个了。我不记得了。
Main.lua中的全局table,所有其他函数都无法访问它。
print(gTextures['buganimation']) 在相关函数内部工作。所以 gTextures 是绝对可以访问的。
Table 不为空。 AnimationSetValues 不为空。
解决方案:Lua 语法。炸脑综合症
我写了:
animationSetValues = {atlasarray = animationQuadArray, width = spriteWidthDivider, height = spriteHeightDivider}
应该是:
animationSetValues = {['atlasfile']=atlasfile, ['atlasarray']=animationQuadArray, ['width']=spriteWidthDivider, ['height']=spriteHeightDivider}
编辑:我完全知道如何使用答案。这是 post 在这里编辑是为了保留我的答案位置,以便我稍后回家时可以对其进行编辑,这正是我现在正在做的事情。我会保留旧的 post 以供存档。
原文:
我解决了。对于现在没有 post 解决方案,我深表歉意。我的脑子都化成肉汁了。
我明天post。只是想 "answer" 说不需要帮助。解决了。
解决方案基本上是,"oh it's just one of those Lua things"。精彩的。我从这门语言中获得了很多乐趣 - 你可以从我茫然的表情中看出。
来自没有行尾或括号但强制打印括号的语言...呃。 class 完成后,我将回到 C#。
我添加了第二个答案,因为两者在上下文中都是正确的。
我最终将 IDE 切换到 VS Code,现在原来的可以用了。
我最初是使用带有 Love2D 解释器的 Eclipse LDT,在那个环境中,我的原始答案是正确的,但在 VS Code 中,原始答案也是正确的。
所以 Dimitry 是对的,它们是等价的,但我的实际 Eclipse 设置的某些方面不允许该语法起作用。
在解释器出现另一个奇怪的语法问题后,我切换到 VS Code,其中无法识别 goto 语法并给出了持续错误。解释器认为 goto 是一个变量的名称。
所以我切换了,现在两件事都解决了。我想我暂时不会使用 LDT。
我有一个 Lua 函数,我在其中构建一个 table 值并尝试将其添加到具有命名键的全局 table。
键名是从函数参数中提取的。基本上,它是一个文件名,我将它与有关文件的数据配对。
不幸的是,全局 table 总是返回 nil。这是我的代码:(如果您需要查看更多,请告诉我)
(注释部分为其他尝试,虽然已经删除了很多尝试)
Animator = Class{}
function Animator:init(atlasfile, stringatlasfriendlyname, totalanimationstates, numberofframesperstate, booleanstatictilesize)
-- Define the Animator's operation mode. Either static tile size or variable.
if booleanstatictilesize ~= false then
self.isTileSizeStatic = true
else
self.isTileSizeStatic = false
end
-- Define the total animation states (walking left, walking right, up down, etc.)
-- And then the total frames per state.
self.numAnimationStates = totalanimationstates or 1
self.numAnimationFrames = numberofframesperstate or 2
-- Assign the actual atlas file and give it a programmer-friendly name.
self.atlasname = stringatlasfriendlyname or removeFileExtension(atlasfile, 'animation')
generateAnimationQuads(atlasfile, self.atlasname, self.numAnimationStates, self.numAnimationFrames)
end
function generateAnimationQuads(atlasfile, atlasfriendlyname, states, frames)
spriteWidthDivider = atlasfile:getWidth() / frames
spriteHeightDivider = atlasfile:getHeight() / states
animationQuadArray = generateQuads(atlasfile, spriteWidthDivider, spriteHeightDivider)
animationSetValues = {atlasarray = animationQuadArray, width = spriteWidthDivider, height = spriteHeightDivider}
--gAnimationSets[#gAnimationSets+1] = atlasfriendlyname
gAnimationSets[atlasfriendlyname] = animationSetValues
--table.insert(gAnimationSets, atlasfriendlyname)
end
注意:使用print(atlasfriendlyname) 和print(animationSetValues) 时,两者都不是空的或nil。它们都包含值。
由于某种原因,将密钥对分配给 gAnimationSets 的行不起作用。
gAnimationSets 在 main.lua 中的程序顶部定义了一次,使用
gAnimationSets = {}
Animator class 在名为 Bug 的角色 class 的 init() 函数期间被调用。 Bug class 在 StartState 的 init() 函数中初始化,该函数扩展自 BaseState,它简单地定义了 dummy init()、enter()、update() 等函数。
StartState 在 main.lua 中使用 StateMachine class 调用,它作为在 main.lua 中声明的全局 table 的值传递到 StateMachine。
gAnimationSets 在 table 状态之后和调用状态之前声明。
这是使用 Love2D 引擎。
抱歉,我是来这里寻求帮助的,我已经纠结了几个小时了。
编辑:更多测试。
尝试在索引 gTextures['buganimation'] 处打印 animationQuadArray 始终 returns nil。嗯?
这是 Main.lua
中的 gTexturegTextures = {
['background'] = love.graphics.newImage('graphics/background.png'),
['main'] = love.graphics.newImage('graphics/breakout.png'),
['arrows'] = love.graphics.newImage('graphics/arrows.png'),
['hearts'] = love.graphics.newImage('graphics/hearts.png'),
['particle'] = love.graphics.newImage('graphics/particle.png'),
['buganimation'] = love.graphics.newImage('graphics/buganimation.png')
}
正在尝试 return gTextures['buganimation'] returns 正常的文件值。不是空的。
我现在脑子都炸了,我都想不起来我为什么来编辑这个了。我不记得了。
Main.lua中的全局table,所有其他函数都无法访问它。
print(gTextures['buganimation']) 在相关函数内部工作。所以 gTextures 是绝对可以访问的。
Table 不为空。 AnimationSetValues 不为空。
解决方案:Lua 语法。炸脑综合症
我写了:
animationSetValues = {atlasarray = animationQuadArray, width = spriteWidthDivider, height = spriteHeightDivider}
应该是:
animationSetValues = {['atlasfile']=atlasfile, ['atlasarray']=animationQuadArray, ['width']=spriteWidthDivider, ['height']=spriteHeightDivider}
编辑:我完全知道如何使用答案。这是 post 在这里编辑是为了保留我的答案位置,以便我稍后回家时可以对其进行编辑,这正是我现在正在做的事情。我会保留旧的 post 以供存档。
原文: 我解决了。对于现在没有 post 解决方案,我深表歉意。我的脑子都化成肉汁了。
我明天post。只是想 "answer" 说不需要帮助。解决了。
解决方案基本上是,"oh it's just one of those Lua things"。精彩的。我从这门语言中获得了很多乐趣 - 你可以从我茫然的表情中看出。
来自没有行尾或括号但强制打印括号的语言...呃。 class 完成后,我将回到 C#。
我添加了第二个答案,因为两者在上下文中都是正确的。
我最终将 IDE 切换到 VS Code,现在原来的可以用了。
我最初是使用带有 Love2D 解释器的 Eclipse LDT,在那个环境中,我的原始答案是正确的,但在 VS Code 中,原始答案也是正确的。
所以 Dimitry 是对的,它们是等价的,但我的实际 Eclipse 设置的某些方面不允许该语法起作用。
在解释器出现另一个奇怪的语法问题后,我切换到 VS Code,其中无法识别 goto 语法并给出了持续错误。解释器认为 goto 是一个变量的名称。
所以我切换了,现在两件事都解决了。我想我暂时不会使用 LDT。