John Lardinois
John Lardinois

Reputation: 109

Lua - table won't insert from function

I have a Lua function where I build a table of value and attempt to add it to a global table with a named key.

The key name is pulled from the function arguments. Basically, it's a filename, and I'm pairing it up with data about the file.

Unfortunately, the global table always comes back nil. Here's my code: (let me know if you need to see more)

(Commented parts are other attempts, although many attempts have been deleted already)

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

Note: when using print(atlasfriendlyname) and print(animationSetValues), neither are empty or nil. They both contain values.

For some reason, the line(s) that assign the key pair to gAnimationSets does not work.

gAnimationSets is defined a single time at the top of the program in main.lua, using

gAnimationSets = {}

Animator class is called during the init() function of a character class called Bug. And the Bug class is initialized in the init() function of StartState, which extends from BaseState, which simply defines dummy init(), enter(), update() etc. functions.

StartState is invoked in main.lua using the StateMachine class, where it is passed into StateMachine as a value of a global table declared in main.lua.

gAnimationSets is declared after the table of states and before invoking the state.

This is using the Love2D engine.

Sorry that I came here for help, I've been picking away at this for hours.

Edit: more testing.

Trying to print the animationQuadArray at the index gTextures['buganimation'] always returns nil. Huh?

Here's gTextures in Main.lua

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

Attempting to return gTextures['buganimation'] returns a file value as normal. It's not empty.

My brain is so fried right now I can't even remember why I came to edit this. I can't remember.

Global table in Main.lua, all other functions can't access it.

print(gTextures['buganimation']) works inside the function in question. So gTextures is absolutely accessible.

Table isn't empty. AnimationSetValues is not empty.

Upvotes: 1

Views: 922

Answers (2)

John Lardinois
John Lardinois

Reputation: 109

I'm adding second answer because both are correct in context.

I ended up switching IDE's to VS Code and now the original one works.

I was originally using Eclipse LDT with a Love2D interpreter and in that environment, my original answer is correct, but in VS Code, the original is also correct.

So Dimitry was right, they are equivalent, but something about my actual Eclipse setup was not allowing that syntax to work.

I switched to VS Code after I had another strange syntax problem with the interpreter where goto syntax was not recognized and gave a persistent error. The interpreter thought goto was the name of a variable.

So I switched, and now both things are fixed. I guess I just won't use LDT for now.

Upvotes: 0

John Lardinois
John Lardinois

Reputation: 109

Solution: Lua syntax. Brain Fry Syndrome

I wrote:

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

Should be:

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

Edit: I'm fully aware of how to use answers. This was posted here to reserve my spot for an answer so I could edit it later when I returned back home, which is exactly what I'm doing right now. I'll keep the old post for archival purposes.


Original: I solved it. I apologize for not posting the solution right now. My brain is melted into gravy.

I will post it tomorrow. Just wanted to "answer" saying no need to help. Solved it.

Solution is basically, "oh it's just one of those Lua things". Wonderful. I'm having so much fun with this language - you can tell by my blank expression.

From the language without line endings or brackets, but forced print parentheses... ugh. I'm going back to C# when this class is done.

Upvotes: -2

Related Questions