script: Fix add_entity returning unusable ref if object deleted in on_activate
authorsfan5 <sfan5@live.de>
Sun, 26 Apr 2020 18:29:16 +0000 (20:29 +0200)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Mon, 27 Apr 2020 04:58:34 +0000 (06:58 +0200)
src/script/lua_api/l_env.cpp

index 3fb58b8c8c9c44a2b7ea8fd4f1e51b8050e65d69..cabca124d35bc70f31107d1e3af19458b6566aa8 100644 (file)
@@ -589,19 +589,19 @@ int ModApiEnvMod::l_add_entity(lua_State *L)
 {
        GET_ENV_PTR;
 
-       // pos
        v3f pos = checkFloatPos(L, 1);
-       // content
        const char *name = luaL_checkstring(L, 2);
-       // staticdata
        const char *staticdata = luaL_optstring(L, 3, "");
-       // Do it
+
        ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, staticdata);
        int objectid = env->addActiveObject(obj);
        // If failed to add, return nothing (reads as nil)
        if(objectid == 0)
                return 0;
-       // Return ObjectRef
+
+       // If already deleted (can happen in on_activate), return nil
+       if (obj->isGone())
+               return 0;
        getScriptApiBase(L)->objectrefGetOrCreate(L, obj);
        return 1;
 }