© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•10mo ago•
23 replies
croizat

[NLua] How to provide the lua state with a dynamically accessible class

I have a
Service
Service
class that contains different services that relate to the game, like Player, WorldState, etc. I want to provide access this class to the lua state in a way where it is newly accessed each time it's called. So not just doing

using var lua = new Lua();
foreach (var p in typeof(Service).GetProperties())
  lua[p.Name] = p.GetValue(typeof(Service));
using var lua = new Lua();
foreach (var p in typeof(Service).GetProperties())
  lua[p.Name] = p.GetValue(typeof(Service));


because that will lead to the values being cached on load, and not accessed whenever they're called in a lua script. The goal is to ultimately be able to just call
Service.Player.Position
Service.Player.Position
or
Service.WorldState:GetMapName()
Service.WorldState:GetMapName()
(or formatted reasonably similarly) and fetch the value on call.

There are hundreds of functions and properties within the service class so I'm not interested in making wrappers for all of them either and registering them individually. This seems very straightforward to accomplish and that I'm missing someting obvious, but the lack of nlua documentation is getting to me.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

[NLua] How to call function which has <T> from lua?
C#CC# / help
3y ago
How to to dynamically provide T type of a generic method
C#CC# / help
15mo ago
NLua problem
C#CC# / help
3y ago
❔ NLua csharp hooks
C#CC# / help
4y ago