Page 1 of 1

Trigger custom method

Posted: Thu Jan 18, 2018 1:41 pm
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.

Re: Trigger custom method

Posted: Thu Jan 18, 2018 2:04 pm
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).
    }
}

Re: Trigger custom method

Posted: Thu Jan 18, 2018 2:28 pm
by Passero
Wow, great stuff!
Thanks for the quick reply.

Re: Trigger custom method

Posted: Thu Jan 18, 2018 3:09 pm
by Tony Li
Glad to help!

Re: Trigger custom method

Posted: Fri Jan 19, 2018 5:03 pm
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.

Re: Trigger custom method

Posted: Fri Jan 19, 2018 6:07 pm
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.