首页 > 解决方案 > Lua - 表不会从函数插入

问题描述

我有一个 Lua 函数,我在其中构建了一个值表并尝试将其添加到具有命名键的全局表中。

键名是从函数参数中提取的。基本上,它是一个文件名,我将它与有关文件的数据配对。

不幸的是,全局表总是返回零。这是我的代码:(如果您需要查看更多信息,请告诉我)

(评论部分是其他尝试,虽然很多尝试已经被删除)

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 类在名为 Bug 的角色类的 init() 函数中被调用。而Bug类是在StartState的init()函数中初始化的,它继承自BaseState,简单定义了dummy init()、enter()、update()等函数。

在 main.lua 中使用 StateMachine 类调用 StartState,它作为在 main.lua 中声明的全局表的值传递给 StateMachine。

gAnimationSets 在状态表之后和调用状态之前声明。

这是使用 Love2D 引擎。

对不起,我来这里寻求帮助,我已经花了好几个小时来处理这个问题。

编辑:更多测试。

尝试在索引 gTextures['buganimation'] 处打印 animationQuadArray 总是返回 nil。嗯?

这是 Main.lua 中的 gTextures

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')
}

尝试返回 gTextures['buganimation'] 会正常返回文件值。它不是空的。

我的大脑现在很煎熬,我什至不记得我为什么要来编辑这个。我不记得了。

Main.lua 中的全局表,所有其他功能都无法访问它。

print(gTextures['buganimation']) 在相关函数内工作。所以 gTextures 是绝对可以访问的。

表不为空。AnimationSetValues 不为空。

标签: luascopeinsertlua-tablelove2d

解决方案


我要添加第二个答案,因为两者在上下文中都是正确的。

我最终将 IDE 切换到 VS Code,现在原来的那个可以工作了。

我最初使用带有 Love2D 解释器的 Eclipse LDT,在那个环境中,我原来的答案是正确的,但在 VS Code 中,原来的答案也是正确的。

所以 Dimitry 是对的,它们是等价的,但是关于我的实际 Eclipse 设置的某些内容不允许该语法工作。

在我遇到另一个奇怪的解释器语法问题后,我切换到 VS Code,其中 goto 语法无法识别并给出持续错误。解释器认为 goto 是变量的名称。

所以我换了,现在两件事都解决了。我想我暂时不会使用 LDT。


推荐阅读