Attempt to index a nil value (that shouldn't be nil)

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
GhostedOnly
Prole
Posts: 1
Joined: Thu Mar 02, 2023 1:04 am

Attempt to index a nil value (that shouldn't be nil)

Post by GhostedOnly »

I'm making a game somewhat similar to Cell Machine by Sam Hogan.

I was making a move cell that moved in the direction it was facing, and made a script (below) that removes itself from the map, and sets the id of the tile in it's way to a move cell (which is 2). To do this, I just got the previous key and added 1 to it. According to LOVE/Lua, the next key in the table (also below), is nil (or at least that's what I understood).

Exception / Error Message:

Code: Select all

Error
main.lua:25: attempt to index a nil value
Traceback
[love "callbacks.lua"]:228: in function 'handler'
main.lua:25: in function 'Step'
main.lua:49: in function 'update'
[love "callbacks.lua"]:162: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'
Script:

Code: Select all

for k, v in pairs(map) do -- Prioritize Move Tiles first to prevent a lot of issues
    for k1, v1 in pairs(v) do
        if v1[1] == 2 then -- v1[1] is ID, v1[2] is DIRECTION. 1 is right, 2 is down, 3 is left, and so on.
            if v1[2] == 1 then
                v1[1] = 0
                v[k1+5][1] = 2
            end
        end
    end
end
Specific row in map table:

Code: Select all

local map = {
    {t(0),t(2),t(0),t(0),t(0),}
Help is appreciated. Attached is a .love file of game. If there is any specific information you need, I'll gladly tell you. Thanks.
Attachments
Durid.love
(3.71 KiB) Downloaded 58 times
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Attempt to index a nil value (that shouldn't be nil)

Post by Bigfoot71 »

Quite simply because each line of your table contains 25 values ​​and that arriving at the 22nd value (21 must be ignored by the following conditions) when you do:

Code: Select all

v[j+5][1]
You are trying to index v[27] as if it were an array when it is nil.

I haven't gone deep enough to understand the behavior that would be desired but you could check that the index is valid before, or calculate the indexes differently, also you could use ipairs as it is only numeric indexes, that could be easier to understand and sometimes avoid errors even if in your case it should be fine:

Code: Select all

local function Step()
    for i, v in ipairs(map) do -- Prioritize Move Tiles first to prevent a lot of issues
        for j, v1 in ipairs(v) do -- print(j)
            if v1[1] == 2 then
                if v1[2] == 1 then
                    v1[1] = 0
                    if v[j+5] then
                        v[j+5][1] = 2
                    else
                        -- do something ?? (or calculate the index differently)
                    end
                end
            end
        end
    end
end
If you have any other questions or have any other problems, please do not hesitate. ^^
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests