Page 1 of 1

Method to move to waypoint through custom Lua script

Posted: Mon Jun 21, 2021 8:20 pm
by xyztankman
Hey guys,

I am attempting to have a method run to have the character move to a waypoint in a cutscene by using the dialogue script field. I have two methods, LeaveRoom() and EnterRoom() in a navmesh script. Following the example from "How to Use Lua in Your C# Scripts" about registering functions, I had trouble thinking of how these would work with the custom lua script since they aren't using strings/bools/doubles.

Screenshot attached, how would I convert these to a bool/double/string so they can be run on command?

Code: Select all

public void EnterRoom(string enter)
    {
        if (!navMeshAgent.pathPending && navMeshAgent.remainingDistance < 0.1f)
        {
            navMeshAgent.destination = waypoints[0].position;
            animator.SetBool("isWalking", true);
            isMoving = true;
            waitToMove = false;
        }
    }

    public void LeaveRoom(string leave)
    {
        if (!navMeshAgent.pathPending && navMeshAgent.remainingDistance < 0.1f)
        {
                navMeshAgent.destination = waypoints[1].position;
                animator.SetBool("isWalking", true);
                isMoving = true;
                waitToMove = false;
        }
    }

    void OnEnable()
    {
        // Make the functions available to Lua: (Replace these lines with your own.)
        Lua.RegisterFunction("EnterRoom", this, SymbolExtensions.GetMethodInfo(() => EnterRoom(string.Empty)));
        Lua.RegisterFunction("LeaveRoom", this, SymbolExtensions.GetMethodInfo(() => LeaveRoom(string.Empty)));
    }

    void OnDisable()
    {
        // Remove the functions from Lua: (Replace these lines with your own.)
        Lua.UnregisterFunction("EnterRoom");
        Lua.UnregisterFunction("LeaveRoom");
            
    }

Re: Method to move to waypoint through custom Lua script

Posted: Mon Jun 21, 2021 9:21 pm
by Tony Li
Hi,

If you need to control when the methods run (e.g., enter room at the 1.5-second mark of the dialogue entry node, and leave room at the 4-second mark), I recommend writing custom sequencer commands instead of Lua functions. (Cutscene Sequence Tutorials)

In either case, you could pass the index of the waypoint (an int) and the name of GameObject to move (a string). If you write a sequencer command, you can specify 'speaker' or 'listener' to use the dialogue entry node's Actor or Conversant without having to specify its GameObject name.

Alternatively to all that, you could use a scene event.

Re: Method to move to waypoint through custom Lua script

Posted: Mon Jun 21, 2021 11:15 pm
by xyztankman
Thanks, scene events was what I was looking for!

Re: Method to move to waypoint through custom Lua script

Posted: Tue Jun 22, 2021 8:37 am
by Tony Li
Glad to help!