Method to move to waypoint through custom Lua script

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
xyztankman
Posts: 54
Joined: Mon Jun 21, 2021 7:48 pm

Method to move to waypoint through custom Lua script

Post 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");
            
    }
Attachments
Navigation.JPG
Navigation.JPG (80.47 KiB) Viewed 580 times
User avatar
Tony Li
Posts: 21989
Joined: Thu Jul 18, 2013 1:27 pm

Re: Method to move to waypoint through custom Lua script

Post 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.
xyztankman
Posts: 54
Joined: Mon Jun 21, 2021 7:48 pm

Re: Method to move to waypoint through custom Lua script

Post by xyztankman »

Thanks, scene events was what I was looking for!
User avatar
Tony Li
Posts: 21989
Joined: Thu Jul 18, 2013 1:27 pm

Re: Method to move to waypoint through custom Lua script

Post by Tony Li »

Glad to help!
Post Reply