From 49b7055cb3f676c19dc5edd07fe72e2f5e2414a1 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 2 Jan 2012 12:48:25 +0200 Subject: [PATCH] Make add_entity return a reference to added entity (or nil) --- data/mods/default/init.lua | 2 +- src/scriptapi.cpp | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/data/mods/default/init.lua b/data/mods/default/init.lua index 47bdafd7..9c1ce4ef 100644 --- a/data/mods/default/init.lua +++ b/data/mods/default/init.lua @@ -152,7 +152,7 @@ -- ^ 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) diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 4dcb66c7..6b850b19 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -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) -- 2.30.2