Page 1 of 1

Integrating Lua with C# using LuaInterface

#1 cokacola  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 23-July 10

Posted 24 July 2011 - 10:47 PM

Note: This tutorial assumes you have basic knowledge of C#, such as variables, referencing and methods!
Download LuaInterface
Hello, today I will be posting this tutorial on integrating Lua with C# using the LuaInterface library(including adding custom Lua functions).
So, to get this library, first visit this page: http://luaforge.net/...s/luainterface/.
Now, on this page, scroll down a little and you should see "Latest File Releases".
Under this will be a download link for LuaInterface, click the link and you will be redirected to a page where the item you chose to download is highlighted.
Click the filename for it and the file will download.
Posted Image
(In the image above, you can see I drew a box around the filename).
Once the file has finished, extract the 2 DLL files inside(one of them will be LuaInterface.dll and the other will be something like Lua51.dll) to a folder on your hard-drive(they must be in the same folder, as referencing LuaInterface also copies the other DLL to your output directory).
Create a new project and Reference LuaInterface
Okay, so now that we've downloaded LuaInterface, its time to do something with it.
First off, create a new C# Console program and name it what ever you want.
Now, for this tutorial, I will assume you know how to reference libraries in C#, so go ahead and reference LuaInterface.dll, the other DLL should be copied to your output folder with it.
Okay, now at the top of your document, copy these using statements:
using System;
using LuaInterface;
using System.IO;
using System.Timers;

As you can see, I added LuaInterface in there, but I also added System.IO and System.Timers.
I included System.IO because we will be going over loading .lua script files.
I included System.Timers because I will be showing you a basic Lua timer function.
Running basic strings
Okay, now that we have it referenced, add this above your Main method:
private static Lua lua = new Lua();

The reason we put this outside the Main method is that we will be using it outside the Main method.
Now, we could set a basic Lua variable like this(this can go inside the Main method)
lua.DoString("myvar = 25")

This will set a myvar variable for this session to 25.
To access this variable, you can do this:
double myvar = (double)lua["myvar"];
(note: this has to be a double, an int throws an exception)
See, its not too hard.
Running files
Okay, now that I've showed you how to execute a Lua string, I will show you how to do the same thing, but with loading a file instead.
In your programs output folder(either Debug or Release, if its a new project than it will probably be Debug), create a new file called "script.lua", and put this inside it:
myvar = 25

Now, back to C#: Inside the Main method, put this:
lua.DoFile("script.lua");

This will load the file, and for the session, set MyVar to 25.
Now, we can use the variable like this:
double something = (double)lua["myvar"];
(Note: this has to be a double, an int throws an exception)
Just put that under the Lua.DoFile inside your main method, and it should work.
Now, under that code, we could put this:
Console.WriteLine(something);
Console.ReadKey(); //wait for key input to exit the program, so it doesn't quit before you can read the value

Load all Lua files from a script folder
Okay, now, I will be showing you a quick snippet to load all files ending with ".lua" in a script folder.
In your output directory, make a new folder called "scripts".
Now, you can put this code in your Main method to load and run all .lua files from the scripts folder:
foreach(string st in Directory.GetFiles("scripts\\","*.lua",SearchOption.AllDirectories)) {
	try {
		lua.DoFile(st);
	} catch(Exception err) {
		//This occures if there the file could not be loaded.
		//This happens mostly if there is an error in the lua script, so you might want to output it or something.
	}
}

Note: when including lua files from other lua files, you can't just use "script.lua" it must be "scripts/script.lua".
Adding custom Lua functions
Yes, here it is, the part you've all been waiting for( :bigsmile: ), adding new Lua functions!
Okay, now at the top of your Main method, add this line:
Program program = new Program();

This is required for when you register a Lua function.
Now, lets create a new method under Main called "outString":
public static void outString(string line) {
	Console.ForegroundColor = ConsoleColor.Yellow;
	Console.WriteLine(line);
	Console.ResetColor();
}

This will soon become a Lua function, and as you can see, it will output a line of text(in yellow), to the Console window of your program.
Now that we have that, lets get back to the Main method.
To register our method as a Lua function, we use this line:
lul.RegisterFunction("outString",program,program.GetType().GetMethod("outString"));

Now, in your Lua script, you can do this:
outString("I am in the console now!")

How cool is that?!
A timer function for you
I made a timer function, its simple, and doesn't work with arguments, but its cool anyway.
Here is the method:
public static void setTimer(string func,double ms) {
	System.Timers.Timer sttime = new System.Timers.Timer(ms);
	sttime.Start();
	sttime.Elapsed += delegate(object sender, ElapsedEventArgs e) {
		LuaFunction fnc = lul.GetFunction(func);
		fnc.Call();
		sttime.Stop();
	};
}

And here is the register line(for the main method)
lul.RegisterFunction("setTimer",program,program.GetType().GetMethod("setTimer"));



That concludes this tutorial, I hope this helps you guys out :)

Is This A Good Question/Topic? 1
  • +

Replies To: Integrating Lua with C# using LuaInterface

#2 petastream  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 20
  • Joined: 23-August 11

Posted 24 August 2011 - 04:10 PM

Very cool, I've had this as a back burner thought for the project that I am currently working on at my work. Thank you for the intro howto. :beta1:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1