Set lau before each conversation?

Announcements, support questions, and discussion for the Dialogue System.
TecD
Posts: 16
Joined: Thu Nov 26, 2020 3:53 pm

Set lau before each conversation?

Post by TecD »

Hi Tony,

I have a prefab that is instantiated x amount of times (e.g. NPC1, NPC2).

I am trying to set a lau variable on each NPC so that they remember their previous conversation answers. I thought maybe I could store the variable in code and then use the On Conversation Start event on Dialogue System Events script, however that doesn't seem to work. It seems like the variable I am conditionally testing the conversation on is always the response of the last conversation I had with the NPC.
conv1.PNG
conv1.PNG (17.8 KiB) Viewed 216 times
So each response above has a lau variable that I set on conversation start (I know the condition icon is not seen above, this is a mockup).

Should this work?
User avatar
Tony Li
Posts: 22166
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set lau before each conversation?

Post by Tony Li »

Hi,

Since you're instantiating NPCs, you probably can't predefine their variables or actor fields in the dialogue database.

Instead, use three features:
  • At runtime, configure a DialogueActor component on each NPC.
  • At runtime, set variables Variable["NPC1_Times"], Variable["NPC2_Times"], etc.
  • Use Variable["ConversantIndex"], which the Dialogue System automatically is set when the conversation starts.
For example, when you instantiate NPCs:

Code: Select all

for (int i = 1; i < 4; i++)
{
    // Instantiate NPC i:
    var npc = Instantiate(npcPrefab);
    npc.name = "NPC" + i;
    
    // Get or add its DialogueActor component:
    var dialogueActor = npc.GetComponent<DialogueActor>() ?? npc.gameObject.AddComponent<DialogueActor>();
    dialogueActor.actor = npc.name;
    
    // Initialize its NPC#_Times variable:
    DialogueLua.SetVariable(npc.name + "_Times", 0);
}
Then each of your dialogue entry nodes can check Variable[Variable["ConversantIndex"].."_Times"]:
  • Dialogue Text: "You have done this 1 time!"
  • Conditions: Variable[Variable["ConversantIndex"].."_Times"] == 0
  • Script: Variable[Variable["ConversantIndex"].."_Times"] = Variable[Variable["ConversantIndex"].."_Times"] + 1
  • Dialogue Text: "You have done this 2 times!"
  • Conditions: Variable[Variable["ConversantIndex"].."_Times"] == 1
  • Script: Variable[Variable["ConversantIndex"].."_Times"] = Variable[Variable["ConversantIndex"].."_Times"] + 1
  • Dialogue Text: "You have done this a lot!"
  • Conditions: Variable[Variable["ConversantIndex"].."_Times"] >= 2
  • Script: Variable[Variable["ConversantIndex"].."_Times"] = Variable[Variable["ConversantIndex"].."_Times"] + 1
TecD
Posts: 16
Joined: Thu Nov 26, 2020 3:53 pm

Re: Set lau before each conversation?

Post by TecD »

I am trying to implement your idea step by step, but at them moment I am stuck on:
Dialogue System: Conversation triggered on NPC_0 but skipping because no entries are currently valid.
NPC_0 is showing as the Actor in the dialogue actor script(however is not showing on the actual database)
User avatar
Tony Li
Posts: 22166
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set lau before each conversation?

Post by Tony Li »

Hi,

NPC_0 doesn't show in the database because it wasn't added to the database. The database is read-only at runtime. Instead, NPC_0 is added to the Lua environment.

Make sure the NPC is assigned to the Dialogue System Trigger's Conversation Conversant. If you're starting the conversation in script, pass it as the second transform:

Code: Select all

DialogueManager.StartConversation("Title", playerTransform, npc_0_Transform);
When you create the NPC, remember to use DialogueLua.SetVariable() to set its variable's value.

Check your Conditions. Make sure one of them will be true.

Temporarily set the Dialogue Manager's Other Settings > Debug Level to Info. This will let you see the Lua conditions it's running and whether each one is true ("Add Link") or false ("Block On False Link").
TecD
Posts: 16
Joined: Thu Nov 26, 2020 3:53 pm

Re: Set lau before each conversation?

Post by TecD »

No conversation issue was because variable was not being set. Thanks.

I am still stuck thou, not sure if I was supposed to take what you typed literally?
Variable[Variable["ConversantIndex"].."_Times"] == 0
The above is exactly what I have in my conditions.

On instantiate I have

Code: Select all

            obj.transform.name = "NPC" + i;
            var dialogueActor = obj.GetComponent<DialogueActor>();
            dialogueActor.actor = obj.name;
            DialogueLua.SetVariable(obj.name + "_Times", 0);
If I remove the condition the conversation does work, so its something to do with the condition?

Here is a few lines of the log:
conv2.PNG
conv2.PNG (21.97 KiB) Viewed 202 times
User avatar
Tony Li
Posts: 22166
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set lau before each conversation?

Post by Tony Li »

Unless your NPC name has spaces or other characters that aren't generally allowed in variable names, try this instead:

Variable[Variable["Conversant"].."_Times"] == 0

Let's say DialogueActor.actor is set to "NPC0".

When the conversation starts, Variable["Conversant"] will be set to "NPC0". So the whole condition will be:

Variable["NPC0_Times"] == 0
TecD
Posts: 16
Joined: Thu Nov 26, 2020 3:53 pm

Re: Set lau before each conversation?

Post by TecD »

Brilliant. Thanks again.
User avatar
Tony Li
Posts: 22166
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set lau before each conversation?

Post by Tony Li »

Glad to help!
TecD
Posts: 16
Joined: Thu Nov 26, 2020 3:53 pm

Re: Set lau before each conversation?

Post by TecD »

Actually, not 100% there, very close thou. I might need to do a partial work around if this cant be done through dialogue system.

As far as I can tell, you can't set a lau variable on the triggering of the conversation. It seems that you have to set the lau variable before you trigger the conversation. This is the only thing I am stuck on now. I have tried to use "conversation start", but that doesn't work.

e.g. check distance from object, set distance variable on conversation trigger, then have conversation use distance variable.
User avatar
Tony Li
Posts: 22166
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set lau before each conversation?

Post by Tony Li »

In your example, I thought you were setting it before starting the conversation.

You can set it in the conversation, but keep in mind that Conversations Evaluate Conditions One Extra Level Ahead. This means you may need to add a dummy node between <START> and the actual first nodes.
Post Reply