User Data
Documentation is in ALPHA and is not as verbose as the beta or final release will be. Some minor details might be wrong.
To mark C# classes, properties, and functions as Lua user data, you simply need to add the bLuaUserData
attribute to the class containing the desired user data. This will make the class a new type of user data that Lua recognizes, and will allow Lua to access properties and functions on any instances of that class in Lua.
[bLuaUserData]
public class Character
{
public int health;
public void Damage(dmg)
{
health -= dmg;
}
}
To hide certain properties or functions from being marked as Lua user data, you can use the bLuaHidden
attribute.
[bLuaUserData]
public class Sword
{
public int damage;
[bLuaHidden]
public void DebugPrintDamage()
{
Debug.Log(damage);
}
}
A technique you might find particularly useful is making a class that is then set as a global variable in your Lua instance. This can be helpful for global functions and Lua taking that first step to affecting C# data.
[bLuaUserData]
public class CharacterFunctionLibrary
{
public Character NewCharacter()
{
return new Character();
}
}
// bLuaInstance inst = new bLuaInstance();
// inst.SetGlobal("CharacterLib", new CharacterFunctionLibrary());
// inst.DoString(@"
// local char = CharacterLib.NewCharacter()
// print(char.health)
// ");