Help with Calling Method from dialogue system

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
DeidreReay
Posts: 93
Joined: Wed Jan 22, 2020 1:20 pm

Help with Calling Method from dialogue system

Post by DeidreReay »

So back again and not sure how to make this work correctly, or if just over thinking. I want to be able to call from dialogue system these methods to basically allow players a teleportation system, but not sure how to do this or if I need to with lua extensions. Any help would be appreciated.

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class ShellTeleportationSystem : MonoBehaviour
{
    public Transform ViernesLocation;
    public Transform PortNethreanLocation;
    public Transform EasternWatchLocation;
    public Transform PlayerTransform;

    private void Awake()
    {
        
    }

    void OnEnable()
    {
        // Register functions for Lua (if needed).
        Lua.RegisterFunction(nameof(DebugLog), this, SymbolExtensions.GetMethodInfo(() => DebugLog(string.Empty)));

    }

    void OnDisable()
    {
        // Unregister functions from Lua (if needed).
        Lua.UnregisterFunction(nameof(DebugLog));
    }

    public void DebugLog(string message)
    {
        Debug.Log(message);
    }

    public double AddOne(double value)
    {
        // Note: Lua always passes numbers as doubles.
        return value + 1;
    }

    public void TeleportViernes()
    {
        if (ViernesLocation != null && PlayerTransform != null)
        {
            PlayerTransform.position = ViernesLocation.position;
        }
    }

    public void TeleportPortNethrean()
    {
        if (PortNethreanLocation != null && PlayerTransform != null)
        {
            PlayerTransform.position = PortNethreanLocation.position;
        }
    }

    public void TeleportEasternWatch()
    {
        if (EasternWatchLocation != null && PlayerTransform != null)
        {
            PlayerTransform.position = EasternWatchLocation.position;
        }
    }
}
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: Help with Calling Method from dialogue system

Post by Tony Li »

Hi,

Try this version:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class ShellTeleportationSystem : MonoBehaviour
{
    public Transform ViernesLocation;
    public Transform PortNethreanLocation;
    public Transform EasternWatchLocation;
    public Transform PlayerTransform;

    [Tooltip("If this component is on your Dialogue Manager, leave this unticked so temporary Dialogue Managers don't unregister your functions.")]
    public bool unregisterOnDisable = false;

    void OnEnable()
    {
        Lua.RegisterFunction(nameof(TeleportViernes), this, SymbolExtensions.GetMethodInfo(() => TeleportViernes()));
        Lua.RegisterFunction(nameof(TeleportPortNethrean), this, SymbolExtensions.GetMethodInfo(() => TeleportPortNethrean()));
        Lua.RegisterFunction(nameof(TeleportEasternWatch), this, SymbolExtensions.GetMethodInfo(() => TeleportEasternWatch()));
    }

    void OnDisable()
    {
        if (unregisterOnDisable)
        {
            Lua.UnregisterFunction(nameof(TeleportViernes));
            Lua.UnregisterFunction(nameof(TeleportPortNethrean));
            Lua.UnregisterFunction(nameof(TeleportEasternWatch));
        }
    }

    public void TeleportViernes()
    {
        if (ViernesLocation != null && PlayerTransform != null)
        {
            PlayerTransform.position = ViernesLocation.position;
        }
    }

    public void TeleportPortNethrean()
    {
        if (PortNethreanLocation != null && PlayerTransform != null)
        {
            PlayerTransform.position = PortNethreanLocation.position;
        }
    }

    public void TeleportEasternWatch()
    {
        if (EasternWatchLocation != null && PlayerTransform != null)
        {
            PlayerTransform.position = EasternWatchLocation.position;
        }
    }
}
Then create a Custom Lua Function Info asset like this so you can select your functions from the Script field's "..." dropdown:

customLuaFunctionInfo.png
customLuaFunctionInfo.png (37.88 KiB) Viewed 47602 times

To create a Custom Lua Function Info asset, select menu item Assets > Create > Pixel Crushers > Dialogue System > Custom Lua Function Info.
Post Reply