Lua - 表不会从函数中插入

问题描述 投票:1回答:2

我有一个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扩展,它简单地定义了伪init(),enter(),update()等函数。

使用StateMachine类在main.lua中调用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不为空。

lua scope insert lua-table love2d
2个回答
0
投票

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

我最终将IDE切换到VS Code,现在原来的是。

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

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

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

所以我换了,现在两件事都修好了。我想我现在不会使用LDT。


-2
投票

解决方案:Lua语法。脑部疾病综合症

我写:

animationSetValues = {atlasarray = animationQuadArray, width = spriteWidthDivider, height = spriteHeightDivider}

应该:

animationSetValues = {['atlasfile']=atlasfile, ['atlasarray']=animationQuadArray, ['width']=spriteWidthDivider, ['height']=spriteHeightDivider}

编辑:我完全知道如何使用答案。这张贴在这里是为了保留我的答案,所以我可以在以后回家时编辑它,这正是我现在正在做的事情。为了档案目的,我会保留旧帖子。


原文:我解决了。我为没有立即发布解决方案而道歉。我的大脑融化成肉汁。

我明天会发布。只是想“回答”说不需要帮助。解决了它。

解决方案基本上是“哦,这只是其中一个Lua的东西”。精彩。我用这种语言玩得很开心 - 你可以用我的空白表达来判断。

从没有行结尾或括号的语言,但强制打印括号......呃。这节课完成后我会回到C#。

© www.soinside.com 2019 - 2024. All rights reserved.