Page 1 of 1

attempt to index local 'button' (a number value)

Posted: Fri Apr 03, 2020 12:25 pm
by MarsOdin
Error

main.lua:492: attempt to index local 'button' (a number value)


Traceback

main.lua:492: in function <main.lua:486>
[C]: in function 'xpcall'


After launching I can click anywhere on the screen and I get the error. It refers to:

for i,v in ipairs(button.activeId) do

The strange part is, that I use the exact same loop some lines above to draw the buttons on the title screen and it works.
I hope somebody can help me. Thanks!
Here is my .love:

Re: attempt to index local 'button' (a number value)

Posted: Fri Apr 03, 2020 4:54 pm
by Andlac028
It is because the button (as argument from function love.mousereleased) replaces your table called button (one variable cannot refer to two values). Try to rename one of that. There is an example to fix it:

Code: Select all

function love.mousereleased(x, y, pressedButton)
    if button == 4 then
        if gm.screen.i == 2 then
            handlerScreenTitle()
        end
    end
    for i,v in ipairs(button.activeId) do
        if button[v].isHighlight == true then
            if pressedButton == 1 then
                button[v].handler()
            end
        end
    end
    for i,v in ipairs(object.drawnObjectIds) do
        if object[v].isHighlight == true then
            if pressedButton == 1 and object[v].handlerL then
                object[v].handlerL()
            elseif pressedButton == 2 and object[v].handlerR then
                object[v].handlerR()
            end
        end
    end
end
(in the example above I renamed argument button and all its usecases of function love.mousereleased to pressedButton)

Re: attempt to index local 'button' (a number value)

Posted: Fri Apr 03, 2020 6:33 pm
by MarsOdin
I was going crazy and only because I wasn't taking into account that it was in a function with an argument that had the same name. I feel stupid, but I also learned something. Thank you so much!
Now it works.