Thursday, November 24, 2011

Lua for you'ah... Adding Lua scripting support to your .Net/WPF app or game

DotNetSlackers - 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.

  • 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

Introducing Lua and LuaInterface

Lua was created in 1993 in Brazil. As a small scripting language, Lua is designed to be embedded inside applications in order to provide flexible expansion and customization. The most famous applications based upon Lua should be the online game "World of Warcraft" and the AngryBirds game release on the iOS platform.

Lua scripts can be easily called in C/C++ code. Lua can in turn call functions written using C/C++, which makes Lua widely used in applications. Lua can not only be used as an extension script but can be used as an ordinary configuration file, instead of XML, .ini, and other file formats, easier to understand and maintain.

Download and rebuild LuaInterface

LuaInterface is a library for integration between the Lua language and Microsoft .NET platform's Common Language Runtime (CLR). Lua scripts can use it to instantiate CLR objects, access properties, call methods, and even handle events with Lua functions. As of this writing, the latest version of LuaInterface is 2.0.3 compiled against lower version of .NET.

To use LuaInterface in the latest .NET 4.0, we have to rebuild the source code project. To do this, we can download the source project from here running the following command at the MS-DOS command line:



In this article you learned the fundamental means of integrating Lua into C# based Console and WPF applications. Lua is a high-efficient embedded language based upon ANSI C. Lua offers many advanced features based upon the two components table and function; we cannot delve into all the interesting stuffs, but you can refer to other detailed Lua materials together with this tutorial to do further research.


That's just cool. I want to build in Lua support into my app's now... :)

(via @visualstudio10 - tweet)

No comments: