12-02-2013, 01:09 AM
I've noticed a weird thing that plugin-writers should be aware of:
When MCServer gives you an object of any class, it means that MCServer owns it and MCServer may destroy it between calls to your plugin. I used the following code to store player coords (Vector3d class) in a table:
The solution to this is to make a copy for all instances that you'll be storing in the globals:
When MCServer gives you an object of any class, it means that MCServer owns it and MCServer may destroy it between calls to your plugin. I used the following code to store player coords (Vector3d class) in a table:
local PlayerPos = a_Player:GetPosition(); -- Get the player's position g_Maze.Center = PlayerPos; -- Store it in a global tableImagine my surprise when the player logged off the server and suddenly the g_Maze's Center's values were weird, coords like 10e-300 and such. Only then did I realise that MCServer has given me the player position in its own managed pointer and it has freed that pointer when the player logged off; but Lua still had references to that pointer. I was "lucky" enough that the server didn't crash, there was something else in the memory, it just wasn't a Vector3d anymore.
The solution to this is to make a copy for all instances that you'll be storing in the globals:
-- Get the player position and create a copy (through Vector3d's copy-constructor) local PlayerPos = Vector3d(a_Player:GetPosition()); -- Now this is a Lua-managed copy and only Lua can destroy that object, once it's not used. -- As long as it's stored in any table, it's safely existing. g_Maze.Center = PlayerPos;