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
asked Apr 13, 2016 at 16:06
1
  • please share what you put into objects table. you are trying to call a function that obviously does not exist in that userdata type. Commented Apr 13, 2016 at 17:49

2 Answers 2

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
answered Apr 13, 2016 at 18:31
Sign up to request clarification or add additional context in comments.

Comments

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.

answered Apr 13, 2016 at 16:17

3 Comments

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)
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.
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.