This has been driving me nuts for over a week. Below are two snippets of Lua code from a robot player in the game Bitfighter (written in C++, using a variant of LuaWrapper for binding).
When I first start the Lua script, both work exactly as expected. But after some minutes of intense object creation and destruction, variant 2 stops working, and gives me the following error:
robot.lua:253: attempt to call missing or unknown method 'getEnergy' (a nil value)
To my eyes these should function identically. Who can explain the difference?
Notes: target is a (heavy) userdata representing a C++ object. getEnergy and getHealth are properly registered C++ functions. I can reproduce this differing behavior easily. This is Lua 5.1, using the luavec mod.
Variant 1 - always works
local mt = getmetatable(target)
local pow = mt.getEnergy(target) + mt.getHealth(target)
Variant 2 - starts failing after script has been running for an arbitrary amount of time
local pow = target:getEnergy() + target:getHealth()