Running Lua
To run Lua code with bLua, you will need to create a bLuaInstance. You can set up your instance with settings and rules to your liking through the settings property, or create an instance without any settings.
bLuaSettings settings = new bLuaSettings()
{
features = bLuaSettings.SANDBOX_BASICMODDING,
tickBehaviour = bLuaSettings.TickBehaviour.AlwaysTick
};
// Settings is an optional parameter
bLuaInstance instance = new bLuaInstance(settings);
You only need one instance to run Lua code, it can be reused by multiple chunks of code as desired. You can run standard Lua code through the instance by passing it in as a string.
instance.DoString(@"
local myInteger = 100
myInteger = 50
print(myInteger)
");
You will notice that the print
function in Lua doesn't automatically send logs to Unity's editor log. You can hook this up yourself through an event on the instance.
instance.OnPrint.AddListener(LuaPrint);
void LuaPrint(bLuaValue[] parameters)
{
string log = "";
foreach (bLuaValue parameter in parameters)
{
log += parameter.ToString();
}
Debug.Log(log); // Log the print to Unity's log
}