Edit DialogueDatabase Variables Through C#

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
almand
Posts: 2
Joined: Wed Sep 01, 2021 2:03 am

Edit DialogueDatabase Variables Through C#

Post by almand »

Hello,

My game is a dungeon crawler/open world that you collect items and then sell them at your shop. You set the price for these items, so if they are too high an NPC might try and haggle with you to get the price down. So what I need is to be able to set the item name that the NPC is trying to buy (string) and the price that they are looking to buy it for (float). The way I'm approaching it would look like this in the dialogue editor:

"This [var=itemName] is too much. I would take it for [var=lookingForPrice] though..."

But my question is:
How would I set those variables from a C# script that already holds that data?

I'm sorry if this question has been asked a few times before, but I've been searching for the answer for a while with no luck. Also let me know if there might be a better way to pass data INTO the dialogue database

Thank You!
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: Edit DialogueDatabase Variables Through C#

Post by Tony Li »

Hi,

Use the DialogueLua class. Example:

Code: Select all

using PixelCrushers.DialogueSystem; //<-- Add to top of script.
...
DialogueLua.SetVariable("itemName", "Magic Sword");
DialogueLua.SetVariable("lookingForPrice", 42);
Alternatively, you can write C# methods (e.g., GetItemName() and GetLookingForPrice()) and register them with Lua. Then you can use [lua(code)] in your Dialogue Text:

"This [lua(GetItemName())] is too much. I would take it for [lua(GetLookingForPrice())] though..."
almand
Posts: 2
Joined: Wed Sep 01, 2021 2:03 am

Re: Edit DialogueDatabase Variables Through C#

Post by almand »

Tony Li wrote: Wed Sep 01, 2021 8:42 am Hi,

Use the DialogueLua class. Example:

Code: Select all

using PixelCrushers.DialogueSystem; //<-- Add to top of script.
...
DialogueLua.SetVariable("itemName", "Magic Sword");
DialogueLua.SetVariable("lookingForPrice", 42);
Alternatively, you can write C# methods (e.g., GetItemName() and GetLookingForPrice()) and register them with Lua. Then you can use [lua(code)] in your Dialogue Text:

"This [lua(GetItemName())] is too much. I would take it for [lua(GetLookingForPrice())] though..."

Thank you for the quick response! My next question is a bit more of a complicated one and if it's too much to answer don't worry about it but...

Is there a way to run a method from a monobehaviour after choosing an option WITHOUT doing it through the scene specific event? Basically how it's going is this:

[AI Behaviour is through a C# script called AIShopping]
1.) Random NPC spawns and starts "shopping"
2.) If the NPC chooses to haggle the dialogue window will open
3.) NPC is able to pass off the itemName and lookingForPrice into variables
4.) Player chooses to accept or not
5.) Based on the answer AIShopping.accept(); or AIShopping.refuse();

I don't know how to get from step 4 to 5 since the AIShopping monobehaviour isn't something that can be pre-defined since it'll be on multiple NPCs and through different scenes. The thought process I'm thinking of is since that mono is already interacting with the database, is there a way for it to reference itself into the database so that it can know to use that mono when doing accept or refuse?

Please let me know if you need more details or if this problem is just too big to get quick help from, you've already been a big help so don't worry about it!

Thanks!
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: Edit DialogueDatabase Variables Through C#

Post by Tony Li »

Hi,

You can use a sequencer command. (More info: Sequences)

Let's assume the player will be the conversation's actor and the random NPC will be the conversation's conversant. Read Character GameObject Assignments to review how GameObjects are chosen to be the conversation's actor and conversant.

In dialogue entries spoken by the player, the player will be the entry's "speaker" and the NPC will be the entry's "listener". (In entries spoken by the NPC, the NPC will be the speaker.)

The code-less solution is to use the SendMessage() sequencer command to call accept() or refuse(). Example:
  • Dialogue Text: [Player] "Sounds like a good deal. I accept that offer."
  • Sequence: SendMessage(accept,,listener)
This uses Unity's SendMessage() to invoke the accept() method on the script(s) on the NPC.

If you don't want to use SendMessage(), you can write a custom sequencer command. The sequencer tutorials includes a tutorial on custom commands, and the Dialogue System provides a commented starter template named SequencerCommandTemplate.cs. Example:
  • Dialogue Text: [Player] "Sounds like a good deal. I accept that offer."
  • Sequence: AcceptOffer()

Code: Select all

public class SequencerCommandAcceptOffer : SequencerCommand
{
    void Awake()
    {
        var AIShopper = listener.GetComponent<AIShopper>();
        if (AIShopper != null) AIShopper.accept();
        Stop();
    }
}
In either case, when you're writing the conversation don't need to set any direct references to any GameObjects (as you would with a scene event), and the conversation will work with any NPC that has an AIShopper component.
Post Reply