Search code examples
luanullgame-enginelove2d

How does my quad in LOVE2D have a nil value?


I'm making a 2nd powerup in my game in LOVE2D what I expect it to do is to grow the paddle that it last collided with. However, it gave me an error:

Error

powerups/PaddleGrow.lua:64: bad argument #2 to 'draw' (Quad expected, got nil)


Traceback

[C]: in function 'draw'
powerups/PaddleGrow.lua:64: in function 'render'
main.lua:630: in function 'draw'
[C]: in function 'xpcall'

My quad as a nil value in the draw function, though I actually assigned it in a Util function:

--[[
    Simple function for making powerups.
]]
function GenerateQuadsPowerups(atlas)
    local x = 0
    local y = 0
    local counter = 1
    local quads = {}
    
    for i = 1, 3 do
        quads[counter] = love.graphics.newQuad(x, y, 8, 8, atlas:getDimensions())
    end
    
    return quads
end

My draw function has this frames and textures thingy in the Dependencies, and I added powerups as a sort. My frames is a table of 3 powerups but my 2nd powerup doesn't want to show up.

function PaddleGrow:render()
    if self.inPlay then
        love.graphics.draw(textures['powerups'], frames['powerups'][2], self.x, self.y)
    end
end

Why did it turn like this?


Solution

  • Your code seems to be assigning the same index of quads table.

    counter is defined once at the beginning of the function as 1, and you're doing a for loop that only assigns the new quad to 1, three times.

    Did you mean quads[i] = love.graphics.newQuad(x, y, 8, 8, atlas:getDimensions())?

    --[[
        Simple function for making powerups.
    ]]
    function GenerateQuadsPowerups(atlas)
        local x = 0
        local y = 0
        local quads = {}
        
        for i = 1, 3 do
            quads[i] = love.graphics.newQuad(x, y, 8, 8, atlas:getDimensions())
        end
        
        return quads
    end