Trigger custom method

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Passero
Posts: 32
Joined: Thu Jan 18, 2018 1:19 pm

Trigger custom method

Post by Passero »

I am trying out the dialogue system and one of the things i want to do is that an NPC gives an item to my player.
I'm not using the items in the dialogue system DB or a 3th party inventory system. I've got my own system.
So I was wondering how I could send a message from a conversation to my inventory system with a parameter (string and int) saying what item and how many it gives.

I looked at QuestLog.AddQuestStateObserver. Would be good if I can add a field where I add the items to be given upon completion but that would require me to add an observer for each quest. I don't know if I can do that easily...

Any other tips would be good.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trigger custom method

Post by Tony Li »

Hi,

Thanks for trying out the Dialogue System! Integration with other systems is one of the Dialogue System's strong points. Under the hood, the Dialogue System runs a Lua scripting environment. You can register your own C# methods with Lua. To do this:

1. Make a copy of the file Assets / Dialogue System / Scripts / Templates / TemplateCustomLua.cs and name it something else, such as MyLua.cs.

2. Edit MyLua.cs, and follow the instructions in the comments to register your C# method as a Lua function.

3. Add MyLua.cs to your Dialogue Manager.

4. Call your Lua function in the dialogue entry node's Script field, such as:
  • Dialogue Text: "It's dangerous to go alone! Take this."
  • Script: AddItem("Sword", 1)

Your MyLua.cs file might look something like this:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class MyLua : MonoBehaviour {

    void OnEnable() {
        Lua.RegisterFunction("AddItem", this, SymbolExtensions.GetMethodInfo(() => AddItem(string.Empty, (double)0)));
    }

    void OnDisable() {
        Lua.UnregisterFunction("AddItem");
    }

    public void AddItem(string itemName, double amount) {
        // Your code here to add item(s).
    }
}
Passero
Posts: 32
Joined: Thu Jan 18, 2018 1:19 pm

Re: Trigger custom method

Post by Passero »

Wow, great stuff!
Thanks for the quick reply.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trigger custom method

Post by Tony Li »

Glad to help!
namelesswc
Posts: 7
Joined: Wed Nov 22, 2017 9:01 am

Re: Trigger custom method

Post by namelesswc »

My idea was to create a scriptable object for that.

You create a prefab game object which has a script like this:

Code: Select all

public class ItemGiver : MonoBehaviour {
    public void GiveItem(string itemName) {
        // Your code.
    }
}
In every dialogue entry there is a event section. Click on the "+" on it and drag your prefab on it. Then select your method.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trigger custom method

Post by Tony Li »

That's a good idea, too. I really like UnityEvents for hooking up behavior in the editor. Unfortunately you can't hook it up to scene objects because the dialogue database exists independently of any specific scene. The prefab idea is a great solution to that restriction.
Post Reply