[solved] add_entity returns userdata instead of ObjectRef

CybeatB
New member
 
Posts: 2
Joined: Sun Oct 09, 2016 00:56
GitHub: CybeatB

[solved] add_entity returns userdata instead of ObjectRef

by CybeatB » Sun Oct 09, 2016 14:13

I'm toying with an idea to create evolving 'cells', and I'm having some trouble mutating the children of my entities.

Here's the offending code snippet, from my init.lua:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local child = minetest.add_entity(pos, 'evolvingcells:cell');
if child ~= nil then
  minetest.log("Created a " .. type(child));
  child.dna = mutate(child.dna);
end


When this code runs, the fourth line causes a crash, with the error "attempt to index local 'child' (a userdata value)".
The log output also reports that 'child' is indeed a userdata value, rather than the ObjectRef (table) I expected it to be.

I looked around for this error to see whether I was making a simple mistake, and found a wiki page which mentions this error being caused by not correctly registering a class before using it, but I don't quite understand what I need to register or how to do so. The function containing this snippet is part of the prototype for my cell entity, so I don't believe that I can register the cell entity before defining this function.

Is there a way for me to get the ObjectRef (or LuaEntitySAO) of my newly-created 'child' entity, as I have attempted to do here?
Last edited by CybeatB on Mon Oct 10, 2016 00:12, edited 1 time in total.
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: add_entity returns userdata instead of ObjectRef

by kaeza » Sun Oct 09, 2016 23:56

The `ObjectRef` type is represented in Lua as a value of type "userdata". From the section 2.2 in the Lua manual:
The type userdata is provided to allow arbitrary C data to be stored in Lua variables. This type corresponds to a block of raw memory and has no pre-defined operations in Lua, except assignment and identity test. However, by using metatables, the programmer can define operations for userdata values [...]


Entities in Minetest contain a table associated with them to store Lua-level values. You can get this table with `obj:get_luaentity()`:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local child = minetest.add_entity(pos, 'evolvingcells:cell');
local e = child:get_luaentity()
e.dna = mutate(e.dna);
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

CybeatB
New member
 
Posts: 2
Joined: Sun Oct 09, 2016 00:56
GitHub: CybeatB

Re: add_entity returns userdata instead of ObjectRef

by CybeatB » Mon Oct 10, 2016 00:11

Yep, that fixed it. Thankyou!
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: [solved] add_entity returns userdata instead of ObjectRe

by kaeza » Mon Oct 10, 2016 00:40

I guess I should mention it for completeness sake, but given an "entity table" T, you can get back to the original object (userdata) with `T.object`:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
assert(e.object == child)
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 4 guests

cron