Adding Lua scripting to your .Net app or game

Sign in to queue


You've heard of Lua? The scripting language used in many games, like World of Warcraft? Wouldn't it be kind of cool to integrate Lua scripting into your .Net application? I thought so too...

Integrate Lua into WPF Games

Lua is a world-famous scripting language. It is a lightweight multi-paradigm programming language designed as a scripting language with extensible semantics as a primary goal. In this article, we are to explore how to integrate Lua into C# applications, especially WPF games.

  • 1 Introduction
  • 2 Introducing Lua and LuaInterface
    • 2.1 Download and rebuild LuaInterface
  • 3 Build the Console Sample project - LuaForCSharpTest
    • 3.1 Sample 1 - access global variables
    • 3.2 Sample 2 - access functions and tables defined in Lua
    • 3.3 Sample 3 - the DoString method
    • 3.4 Sample 4 - calling of a function inside a table
    • 3.5 Sample 5 - calling of a global function that returns two values
    • 3.6 Sample 6 - Test the RegisterFunction method
    • 3.7 Sample 7 - Invoking other .lua files, .NET assembles and more…
  • 4 Integrate Lua into WPF Games
    • 4.1 Set up Lua script
    • 4.2 WPF C# programming
  • 5 Summary

The article covers downloading and compiling the latest version of the LuaInterface (the .Net Managed Assembly for Lua) and then begins walking you through using it.

From a simple console app;

Build the Console Sample project - LuaForCSharpTest

First off, create a new C# Console program and name it LuaForCSharpTest. For readers more clearly see the relations between the interested stuff, I create a folder LuaInterfaceStuff which holds the two file LuaInterface.dll and lua51.dll.

Now, add reference to LuaInterface.dll in the sample project, the other DLL should be automatically copied to the output folder with it.

As usual, let's add reference to the namespace defined in the assemble:

Let's next introduce the related tiny samples one by one.


To the more complex;

Sample 7 - Invoking other .lua files, .NET assembles and more…

The following sample is adapted from the online LuaInterface samples ( In most of the online samples, you would see Lua scripts inside C# application can easily interact with .NET libraries and related data structures. Now, let's take a look at a related example.

First, let's build up a Lua script file named Prompting.wlua which looks like this.


And finally discussing integrating it into your WPF app/game;

Integrate Lua into WPF Games

Embedded scripting language can give our application system more powerful flexibility and scalability. Take the world-famous network game World of Warcraft for example. This application used a bit more than 200K of Lua volume but gained a very fast computation speed. Nowadays, in Microsoft .NET platform, Lua can be widely applied to applications (such as WinForm, WebForm, WPF, etc.) which allow unsafe code. The good features of ease of use (no needing to consider the core module of complex logic, just using the simple script to complete the task can be specified), cross-platform (once compiled by the right script language can be free to migrate to other systems), and expansion of demand (in the function of the complement system and upgrade in the same play a pivotal role) makes Lua applicable over a wide variety of development areas.

Starting from this section, let's explore how to make WPF games interact with Lua.

First, start up Visual Studio 2010 and create a general WPF application named LuaInWPFGame based upon .NET 4.0.

Second, add a reference to LuaInterface in our WPF project. In our case, we use the afore-mentioned ready-made assemble LuaInterface.dll (together with the related file lua51.dll).

Next, let's take the popular chat scene in a WPF game as an example to introduce how to embed Lua inside a WPF application.


If you're thinking that adding scripting to your game/app is needed to take it to the next level, or just think it would be cool to include Lua in your app, this article will get your started down the road...


Here’s a few more links you might find interesting:

The Discussion

  • User profile image

    I would like to point out that at this time LuaInterface does not work with .NET 4.0 projects.

Add Your 2 Cents