Search code examples
lualua-tabledefold

Lua - attempt to index local 'lvlObj' (a userdata value)


I am a newbie programmer, just starting out with lua and Defold, and basically I have a table called objects, and later in the code I am looping through the table using the method pairs, and in that for loop I try to access the item and use it, but I get an error saying:

ERROR:SCRIPT: level/controller.script:57: attempt to index local 'lvlObj' (a userdata value)

Anyways I was wondering what this error derives from, and how do I fix it. (pipe_reset is a boolean variable, should have nothing to do with the error)

pipe_reset = false

local objects = {}

... later in the code

if pipe_reset then
    for k in pairs(objects) do
        local lvlObj = objects [k]
        lvlObj.delete()
        objects [k] = nil
    end 
    pipe_reset = false
end

Solution

  • You get this error because you try to index a non indexable userdata type.

    I have no idea of Defold but I searched its API reference for a delete() function. The only one I found was go.delete()

    As you don't provide sufficient information I can only assume that this is the function you want to use.

    Please refer to http://www.defold.com/ref/go/#go.delete%28%5Bid%5D%29 for details.

    delete is not a member of your object type but of the table go. So you most likely have to call go.delete() instead. go.delete() takes an optional id. There is also a function go.get_id().

    I guess you can do something like

    local id = go.get_id(myFancyObject)
    go.delete(id)
    

    or maybe your object alread is that id? so

    go.delete(myFancyObject)
    

    might work as well

    Maybe just try in your example:

    for _, id in objects do
      go.delete(id)
    end