Make add_entity return a reference to added entity (or nil)
authorPerttu Ahola <celeron55@gmail.com>
Mon, 2 Jan 2012 10:48:25 +0000 (12:48 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Mon, 2 Jan 2012 10:48:25 +0000 (12:48 +0200)
data/mods/default/init.lua
src/scriptapi.cpp

index 47bdafd71513e61372fecab4f9f41d46cec409d2..9c1ce4ef8e94f01c17895300b222120e9c057ce5 100644 (file)
 --   ^ Returns nil for unloaded area
 -- - get_node_light(pos, timeofday) -> 0...15 or nil
 --   ^ timeofday: nil = current time, 0 = night, 0.5 = day
--- - add_entity(pos, name)
+-- - add_entity(pos, name): Returns ObjectRef or nil if failed
 -- - add_item(pos, itemstring)
 -- - add_rat(pos)
 -- - add_firefly(pos)
index 4dcb66c772812445f4da700f0a359a12e0f944a1..6b850b19a0c3ced0635d3bd16f04729456241ce9 100644 (file)
@@ -2352,8 +2352,13 @@ private:
                const char *name = luaL_checkstring(L, 3);
                // Do it
                ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, "");
-               env->addActiveObject(obj);
-               return 0;
+               int objectid = env->addActiveObject(obj);
+               // If failed to add, return nothing (reads as nil)
+               if(objectid == 0)
+                       return 0;
+               // Return ObjectRef
+               objectref_get_or_create(L, obj);
+               return 1;
        }
 
        // EnvRef:add_item(pos, inventorystring)