-
[Question] private server - register data without adding it to the global table
Hello everyone,
I'm working with the Lua C API (specifically Lua 5.1), and I had a quick question:
Is there a way to register data from C++ — such as tables, functions, or libraries — in a way that doesn’t put them directly into the global table (_G), but still allows them to be accessed as if they were global within a script?
Basically, I’m looking to create an isolated namespace (like a custom environment) where I can expose C++ bindings without cluttering the global scope, while keeping access in Lua code as clean as possible — e.g., being able to call MyFunction() instead of _A.MyFunction().
Any tips, patterns, or best practices for this kind of sandboxed-but-global-style registration would be greatly appreciated!
Thanks in advance!
-
Established Member
Originally Posted by
Makkah
Hello everyone,
I'm working with the Lua C API (specifically Lua 5.1), and I had a quick question:
Is there a way to register data from C++ — such as tables, functions, or libraries — in a way that doesn’t put them directly into the global table (_G), but still allows them to be accessed as if they were global within a script?
Basically, I’m looking to create an isolated namespace (like a custom environment) where I can expose C++ bindings without cluttering the global scope, while keeping access in Lua code as clean as possible — e.g., being able to call MyFunction() instead of _A.MyFunction().
Any tips, patterns, or best practices for this kind of sandboxed-but-global-style registration would be greatly appreciated!
Thanks in advance!
setfenv can be your friend with a metatable to have __index and __newindex
-
Post Thanks / Like - 1 Thanks
Makkah (1 members gave Thanks to scizzydo for this useful post)
-
Thanks for the idea about metatables, it really helped! I realized you only need to register around 10 methods across all object types. Once you hook the UnitID to GUID function, any function that uses UnitID will now work with GUID.