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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MarsOdin
Prole
Posts: 12
Joined: Tue Apr 02, 2019 11:46 pm

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

Post 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:
Attachments
004.love
(88.92 KiB) Downloaded 99 times
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

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

Post 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)
MarsOdin
Prole
Posts: 12
Joined: Tue Apr 02, 2019 11:46 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests