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
-
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– Piglet2016年04月13日 17:49:55 +00:00Commented Apr 13, 2016 at 17:49
2 Answers 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
Comments
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.
3 Comments
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.