简体   繁体   中英

Exposing C# objects to Lua scripting

I'm looking into adding scripting to my C# application. I've been debating between Lua and C# (through CSharpCodeProvider).

Regardless of which language I use, I need the script to be able to access/manipulate objects/arrays in my main application. With C# I should be able to expose my objects and interface functions without too many issues.

However, with Lua it seems like I'll only be able to access the application objects through exposed functions. I can't see how I could have a non-chunky interface to, for example, arrays. I'd either need Array1Set(index, value)/Array1Get(index) functions or ArraySet(array_no, index, value)/.... Is there an elegant way to implement this? I don't want to copy the arrays to the Lua machine, manipulate it, then pull it back into my application.

Thanks

You should take a look at the LuaInterface project, which supports full integration between Lua and .NET. Ask google for more information about LuaInterface to find lots of useful pages of discussion, samples, and ideas.

The general method of sharing objects between Lua and any application in any language is to define the __index() and __newindex() metamethods (and possibly others) of a userdata containing either the object instance itself (letting Lua's GC manage the object's lifetime) or a pointer to the instance (which requires careful cooperation with the GC). The metamethods allow Lua code to manipulate fields of the object as if they were stored in a Lua table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM