Journal Entry 62 – Lua Integration

I ended up playing with my CNC machine longer than expected, so tonight will be a pretty quick blog post! I want to take a bit of time to chat about the Lua scripting language and how Summer Dawn (and LLF) interface to Lua. I use Lua as a scripting language to help enable the modding community, and to make it easy for anyone else to create content for the game. Summer Dawn uses NLua, which are bindings and a wrapper for using Lua within the .NET framework. It makes interfacing with Lua pretty straight forward. After acquiring your copy of NLua (which is under the permissive MIT license) you have to create a Lua environment.

var lua = new NLua.Lua();

Now we can execute Lua from either C# code or from a file. Here’s an example of a fibonacci calculation written in Lua, with some simple memoization to cache intermediate values. There are probably more efficient ways to do this, but it gets the job done.

lua.DoString(@"
cache = {}
 
function fibonacci (val)
  if (val < 1) then
    return 0
  elseif val < 3 then 
    return 1
  elseif (cache[val] ~= nil) then
    return cache[val]
  else
    cache[val] = fibonacci(val - 2) + fibonacci(val - 1)
    return cache[val]
  end
end");

We can then get a reference to this function and call it from C# code!

var scriptFunc = lua["fibonacci"] as NLua.LuaFunction;
 
for (int i = 1; i < 35; i++)
{
    double? result = (double?)scriptFunc.Call(i).First();
    Console.WriteLine("fibonacci({0}) = {1}", i, result);
}

As an aside, I use ‘First()’ here because Lua functions can return multiple items, so Call returns an array of objects. I only want the first object returned, so I am ussing the Linq extension method. I could have simply used Call(i)[0] for the same effect.

Okay, so we can now execute Lua code The next step is figuring out how to call C# code directly from Lua. Well, that’s pretty easy too. Here’s a simple class I wrote which has the ability to write to the Console:

public class ConsoleWriter
{
    public ConsoleWriter()
    {
    }
 
    public void Print(string text)
    {
        Console.WriteLine(text);
    }
}

We need to assign an instance of this class to a Lua variable. In this case, I will assign a new instance of ConsoleWriter to a Lua variable named instance. Next I will call the Print method on the instance variable, which will call the corresponding Print method in C# code.

lua["instance"] = new ConsoleWriter();
lua.DoString("instance:Print(\"Hello from Lua\")");
Here's a screenshot of the fibonacci sequence and printing to the console from Lua!
Here’s a screenshot of the fibonacci sequence and printing to the console from Lua!

The result is “Hello from Lua” printed to the Console. This is very powerful, as Lua code can execute C# code directly, and vice versa. This forms the basis for questing, NPC text and so on. I hope to have an example of a quest written in Lua soon. I am now working on transitioning from the current hard-coded quest system to the flexible and scriptable system that Lua enabled.

I hope to have more time tomorrow for a bigger blog post. I know I promised a water wheel, and that is in the works, I just couldn’t get it finished tonight. I also want to take a minute to talk about today’s reddit post and my new twitter followers. I want to thank you all for the positive feedback I’ve gotten. Here are a few of the common questions I am getting.

When is your game coming out?

I can’t promise a release date, but I’m hoping to release an alpha version in Q1 of 2015. No promises though! I want to have a solid feature set before releasing.

Can I stream your game once I get it?

Absolutely, you are free to stream the game and/or record and store your game sessions in any format or medium that you would like.

How much will the game cost?

The alpha is absolutely free. I wouldn’t feel right charging a fee for a game that isn’t complete.

What’s the best way to get new information about the game?

Follow me on Twitter! That’s where you’ll find out the latest information about LLF and my work.

Thanks again, and happy coding!

Giawa

Leave a Reply