Skip to content
TheModMaker edited this page Aug 13, 2012 · 2 revisions

First, to use different settings than default, create a ModMaker.Lua.LuaSettings object (see LuaSettings). Then pass the object to the ModMaker.Lua.Lua constructor. This will be the main handling object. This object handles registering types and delegates, hosting a LuaEnvironment, and holding the loaded chunks. Before a chunk is executed, any types and delegates must be registered. A chunk can be loaded before they are registered, but in order for the types and delegates to be accasible, they need to be registered before the chunk is executed.

To register types or delegates, use the overloads of Lua.Register. There are two major ones, Lua.Register(Type) and Lua.Register(Delegate) that respectivly register a type and a delegate. The delegate can be named something else, the type cannot. There are extension methods defined in the ModMaker.Lua.Extensions namespace that allows registering delegates without casting by providing overloads for any number of parameters up to 16. To use these, simply include a using ModMaker.Lua.Extensions statement in the file.

Example code:

Lua lua = new Lua();
lua.Register((Delegate)Foo);
lua.Register(typeof(T));

Once types and delegates have been registered, chunk(s) need to be loaded. A short-cut to loading and executing a chunk is Lua.DoFile and Lua.DoText which load and execute respectivly a file and plaintext. The former accepts a string path or an open Stream object. The other way to load a chunk is with Lua.Load and Lua.LoadText which operate the same way, except they return the loaded chunk. A reference to the chunk is maintained by the Lua object and can be retrieved using the indexer. To execute the chunk, call LuaChunk.Execute. The chunks can also be executed by calling Lua.Execute. One overload of Lua.Execute accepts a params array of arguments to pass and executes all the chunks in the order they were loaded; the other accepts a zero-based index and a params array of arguments and executes the respective chunk. Both return any results in an array.

Full example:

Lua lua = new Lua();
lua.Register(typeof(T));
LuaChunk chunk = lua.Load("file.lua");
chunk.Execute();

Next: Maintaining Visibility

Clone this wiki locally