3

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
David Wolters
  • 336
  • 2
  • 6
  • 18
  • please share what you put into objects table. you are trying to call a function that obviously does not exist in that userdata type. – Piglet Apr 13 '16 at 17:49

2 Answers2

2

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
Piglet
  • 27,501
  • 3
  • 20
  • 43
1

You can only use "x.y" if x is a table (it is equivalent to x["y"]), which apparently it isn't. If its supposed to be a table with a "delete" key, I would look at where that table is created, or see if there are any non-table values in objects. If it isn't, I would try to use table.remove() instead.

sowrd299
  • 149
  • 6
  • I tried to user objects.k in this case, but it gave me an error: level/controller.script:56: attempt to index field 'k' (a nil value) – David Wolters Apr 13 '16 at 17:07
  • objects.k is not equivalent to `objects[k]`, it is equivalent to `objects["k"]`, meaning it is only useful if you know you mapped something with the value `"k"`, and you want to access that specific thing. Honestly I wouldn't worry about dot notation at all in this context. – sowrd299 Apr 13 '16 at 17:40
  • he obviously tries to delete the userdata that is inside objects befor removing it from the table, so table.remove won't help him at all. – Piglet Apr 13 '16 at 17:51