diff --git a/doc/lua_api.md b/doc/lua_api.md index 747c523a4..cd7707343 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5249,6 +5249,13 @@ Methods where the engine will keep the map and the VM in sync automatically. * Note: this doesn't do what you think it does and is subject to removal. Don't use it! * `get_emerged_area()`: Returns actual emerged minimum and maximum positions. +* `close()`: Frees the data buffers associated with the VoxelManip object. + It will become empty. + * Since Lua's garbage collector is not aware of the potentially significant + memory behind a VoxelManip, frequent VoxelManip usage can cause the server to + run out of RAM. Therefore it's recommend to call this method once you're done + with the VoxelManip. + * (introduced in 5.13.0) `VoxelArea` ----------- diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index 5815d3bf3..09a9f9ffa 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -344,6 +344,19 @@ int LuaVoxelManip::l_get_emerged_area(lua_State *L) return 2; } +int LuaVoxelManip::l_close(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + LuaVoxelManip *o = checkObject(L, 1); + + if (o->is_mapgen_vm) + throw LuaError("Cannot dispose of mapgen VoxelManip object"); + o->vm->clear(); + + return 0; +} + LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm) : is_mapgen_vm(is_mg_vm), vm(mmvm) @@ -451,5 +464,6 @@ const luaL_Reg LuaVoxelManip::methods[] = { luamethod(LuaVoxelManip, set_param2_data), luamethod(LuaVoxelManip, was_modified), luamethod(LuaVoxelManip, get_emerged_area), + luamethod(LuaVoxelManip, close), {0,0} }; diff --git a/src/script/lua_api/l_vmanip.h b/src/script/lua_api/l_vmanip.h index 5ba1caffa..bfd83f56c 100644 --- a/src/script/lua_api/l_vmanip.h +++ b/src/script/lua_api/l_vmanip.h @@ -45,6 +45,8 @@ private: static int l_was_modified(lua_State *L); static int l_get_emerged_area(lua_State *L); + static int l_close(lua_State *L); + public: MMVManip *vm = nullptr;