Hello everyone,
I’ve just released a script asset, which is an add-on for MoonSharp, a brand new Lua - Unity/C# bridging library developed by a friend of mine. It allows reading Lua data into Unity in a way that’s faster, easier and more intuitive than anything previously available, and works on both Unity and Unity Pro!
If you’ve ever wanted to use Lua to help build your game or add modding elements, it’s now easier than ever. I’m using this asset myself in a game under development, a Legend Of Grimrock kind-of oldschool RPG, which uses Lua for asset definitions and modding the same way LoG does.
Asset Store Link: Unity Asset Store - The Best Assets for Game Making
Description :
Lua Framework allows you to easily and automatically convert data defined in the Lua script language to .NET objects, and vice-versa. It works like XML or JSON readers, but instead of a markup language, you now have access to a powerful programming language to define your game or application’s logic. Like many top-selling games did, choosing Lua can greatly streamline the game design process and most importantly, allow easy-to-implement modding capacities.
Lua Framework is built on the power of MoonSharp, a modern and free .NET implementation of the Lua language. As opposed to previous .NET to Lua bridges such as LuaInterface, NLua or UniLua, MoonSharp provides a very intuitive user experience with fast learning curve, fast performance, is regularly updated, supports latest Lua 5.2, and supports Unity’s Mono implementation of the .NET language out of the box, on all Unity versions and licenses, including iOS. You can learn more about MoonSharp and download it at moonsharp.org.
For now, the Lua Framework has two main modules: LuaReader and LuaWriter. LuaReader automatically maps Lua variables and tables to .NET objects. LuaWriter creates a Lua script representation of .NET objects. Currently supported types are:
- Built-in types: bool, int, float, double, string, byte & decimal;
- Enums;
- Unity-specific structs: Color, Color32, Rect, Vector2, Vector3 & Vector4;
- Lua functions (closures)
- ANY custom classes with public properties;
- One-dimensional or two-dimensional arrays of any supported type;
- Generic Lists or Dictionaries of any supported type;
- Any possible nesting, for example List<Dictionary<string, Vector3>>.
The code for the Lua Framework is clean and professional, performance-optimized, with full intellisense support.
Manual:
You can download the complete manual to get a better understanding of how this asset works:
Lua Framework Manual V 1.0.pdf.
Example:
But as a quick example of what this can do, Lua Framework can map the following Lua code:
defineEnemy{
name = “Bandit”,
health = 50,
attackPower = 3.5,
}
defineEnemy{
name = “Bandit Leader”,
health = 125,
attackPower = 5,
}
To this C# class:
public class Enemy
{
public string name { get; set;}
public int health { get; set;}
public float attackPower { get; set;}
}
With one simple function call (called by the Lua defineEnemy
function, see manual for details):
Enemy
enemy =
LuaReader
.Read<
Enemy
>(luaTable);
Without the need of any further attributes, casting or manual parsing and reading of lua tables/data. The equivalent code with NLua would be:
Enemy enemy = new Enemy();
enemy.name = (string) luaTable[name];
enemy.health = (int) (double) luaTable[health];
enemy.attackPower = (float) (double) luaTable[attackPower];
Which is longer of course as Lua Framework takes care of all the castings, but also less flexible: if you decide to change your Enemy class in the future, by adding, renaming or removing properties, you need to change the parsing code. Also, the above will raise errors if your lua table happens to miss a property, requiring you to add a lot more code if you want some properties to be optional, or safety against user scripts for mods.
Questions, comments, feature requests? Don’t hesitate to post, I’ll be glad to answer and incorporate anything you need in future updates.