Skip to content

Examples

Instance Component Example

In the example below, an instance is created for this component with some settings that might be useful for game development. It's often desired to have Lua automatically resume coroutines every frame in game development so functions like wait() are guaranteed to resume when expected if they are called inside of a Lua coroutine.

using bLua;
using UnityEngine;

public class MyBLuaComponent : MonoBehaviour
{
    private bLuaInstance instance;

    private void Awake()
    {
        bLuaSettings settings = new bLuaSettings()
        {
            features = bLuaSettings.SANDBOX_ALL,
            tickBehavior = bLuaSettings.TickBehavior.Manual, 
            coroutineBehaviour = bLuaSettings.CoroutineBehaviour.ResumeOnTick
        };

        instance = new bLuaInstance(settings);

        instance.OnPrint.AddListener(OnLuaPrint);
    }

    private void Start()
    {
        instance?.DoString(@"
            print('Hello world!')
        ");
    }

    private void Update()
    {
        instance?.ManualTick();
    }

    private void OnLuaPrint(bLuaValue[] args)
    {
        string log = "";
        foreach (bLuaValue arg in args)
        {
            log += arg.ToString();
        }
        Debug.Log(log);
    }
}


Global Instance Component Example

Below is a more advanced example that makes use of only one instance throughout all usages of MyBLuaComponent. Note that this example also uses the TickAtInterval setting instead of Manual, so the instance will automatically tick itself at the interval in the settings, meaning there is no need to call ManualTick every frame. This does mean that Lua ticks will not necessarily line up with Unity's Update call, so modify the example if you'd like the instance to tick on Unity's Update.

Another thing this example shows is how to keep code in separate "environments" (which are just Lua tables) so that code from one component doesn't interfere with code from another. You can also set up global functions and variables inside of the environment as shown below.

using bLua;

public static class MyGlobalBLua
{
    public static bLuaInstance instance;

    static MyGlobalBLua()
    {
        bLuaSettings settings = new bLuaSettings()
        {
            features = bLuaSettings.SANDBOX_ALL,
            tickBehavior = bLuaSettings.TickBehavior.TickAtInterval,
            coroutineBehaviour = bLuaSettings.CoroutineBehaviour.ResumeOnTick
        };

        instance = new bLuaInstance(settings);

        instance.OnPrint.AddListener(OnLuaPrint);
    }

    private static void OnLuaPrint(bLuaValue[] args)
    {
        string log = "";
        foreach (bLuaValue arg in args)
        {
            log += arg.ToString();
        }
        Debug.Log(log);
    }
}
using bLua;
using UnityEngine;

public class MyBLuaComponent : MonoBehaviour
{
    private bLuaValue environment;

    private void Awake()
    {
        environment = bLuaValue.CreateTable(MyGlobalBLua.instance);
        environment.Set("Vector3", new bLuaVector3Library()); // Vector3 helper function library
        environment.Set("GameObject", new bLuaGameObjectLibrary()); // GameObject helper function library
        environment.Set("gameObject", new bLuaGameObject(gameObject)); // This component's GameObject
    }

    private void Start()
    {
        MyGlobalBLua.instance?.DoString(@"
            print('Hello world!')
        ",
        null,
        environment);
    }
}