Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getWrappedFunction(f)
- -- this function will be called with handle, but we want to call it with raw reference
- return function(handle, ...) return f(handle.cppRef, ...) end
- end
- local memoizedEntityCppFuncs = {}
- function createHandle(e)
- local handle = {
- cppRef = e,
- isValid = true
- }
- local mt = {}
- mt.__index = function(handle, key)
- if not handle.isValid then
- error("Handle is invalid!", 2)
- end
- -- memoizing functions speeds this up a lot
- local memF = memoizedEntityCppFuncs[key]
- if not memF then
- memF = getWrappedFunction(Entity[key])
- memoizedEntityCppFuncs[key] = memF
- end
- return memF
- end
- setmetatable(handle, mt)
- return handle
- end
- function onEntityDeleted(handle)
- handle.isValid = false
- end
- function test(handle)
- print("Calling through handle:", handle:getName())
- end
- function test2(e)
- print("Calling through raw reference:", e:getName())
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement