Selecting nodes in a conversation randomly

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
nishant
Posts: 55
Joined: Mon Sep 21, 2015 3:16 pm
Location: Canada
Contact:

Selecting nodes in a conversation randomly

Post by nishant »

Hi ,
I have a set of dialogues that should play randomly if player chooses to talk to a particular NPC.
For eg assuming NPC Tom has conversation with dialogues 1,2,3 ; if player chooses to speak with Tom then sometimes Tom will start with dialogue 1 , 2 or 3 randomly. I can make different conversations all together and let the script trigger any of them based on Random.value but am looking for something in DialogueSystem that you do the same with nodes

Thanks.
Nishant
User avatar
Tony Li
Posts: 20700
Joined: Thu Jul 18, 2013 1:27 pm

Re: Selecting nodes in a conversation randomly

Post by Tony Li »

Hi Nishant,

In the START node, set the Script field to:

Code: Select all

randomValue = math.random(3)
Then create an empty child node. Set its Sequence to:

Code: Select all

None()
Then create three child nodes of that node. Set their Conditions fields to:

Code: Select all

randomValue == 1
et cetera for 2 and 3.

Your conversation will be structured like this:
  • [0] START - Script: randomValue=math.random(3)
    • [1] Sequence: None()
      • [2] Dialogue Text: "This is dialogue 1" - Conditions: randomValue==1
      • [3] Dialogue Text: "This is dialogue 2" - Conditions: randomValue==2
      • [4] Dialogue Text: "This is dialogue 3" - Conditions: randomValue==3
The empty node between START and your 3 dialogues is required.


Another way is to use the Dialogue System's RandomElement() Lua function:
  • [0] START - Script: randomValue=math.random(3)
    • [1] Dialogue Text: [lua(RandomElement("Hello|Hi|What's up?"))]
nishant
Posts: 55
Joined: Mon Sep 21, 2015 3:16 pm
Location: Canada
Contact:

Re: Selecting nodes in a conversation randomly

Post by nishant »

Perfect ... that solves it

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

Re: Selecting nodes in a conversation randomly

Post by Tony Li »

Someone had a question about the math.random(#) method described above. In his case, sometimes the Dialogue System wouldn't match any of the nodes' conditions, so it would end the conversation.

One possible cause is that the random value function:

randomValue = math.random(3)

generates a number that's higher than the number of responses.

One solution is to make sure randomValue is always set to a range that's covered by all the conditions. For example, math.random(3) will return a number 1, 2, or 3. So your follow-up nodes should have conditions "randomValue == 1" through "randomValue == 3".

The other solution, which avoids user error in the conditions, is to add a guaranteed fallback node. The conversation will use this node if none of the other nodes' conditions are true. To set up the fallback node, create an additional follow-up node with no conditions. Then click on the link arrow that leads to that node. In the Inspector, change the Priority dropdown to BelowNormal. So the conversation will be structured like this:
  • [0] START - Script: randomValue=math.random(3)
    • [1] Sequence: None()
      • [2] Dialogue Text: "This is dialogue 1" - Conditions: randomValue==1
      • [3] Dialogue Text: "This is dialogue 2" - Conditions: randomValue==2
      • [4] Dialogue Text: "This is dialogue 3" - Conditions: randomValue==3
      • [5] Dialogue Text: "This is guaranteed fallback dialogue." (No Conditions. Priority=BelowNormal)
How this works: When choosing a follow-up node, the Dialogue will check the conditions on all the normal nodes (i.e., the nodes with "randomValue == #" conditions). If a node's conditions are true, it will use the node. Otherwise, if none of the normal nodes' conditions are true, it will check the BelowNormal nodes. Since the BelowNormal node has no conditions, it will always be true, so in this case the Dialogue System will use it.
sunwooz
Posts: 8
Joined: Thu Jan 27, 2022 10:54 pm

Re: Selecting nodes in a conversation randomly

Post by sunwooz »

Is there a way to set different percentage chances for each possible node?

For example, I have two possible dialogue outcomes, but I want one to have an 80% chance and the other 20% chance.
User avatar
Tony Li
Posts: 20700
Joined: Thu Jul 18, 2013 1:27 pm

Re: Selecting nodes in a conversation randomly

Post by Tony Li »

You can do the same as above. Set a random value -- for example 1-100. Then branch based on that value:

  • [0] START - Script: randomValue=math.random(100)
    • [1] Sequence: None()
      • [2] Dialogue Text: "This line has a 20% chance to appear." - Conditions: randomValue > 80
      • [3] Dialogue Text: "This line has an 80% chance to appear."
sunwooz
Posts: 8
Joined: Thu Jan 27, 2022 10:54 pm

Re: Selecting nodes in a conversation randomly

Post by sunwooz »

Perfect, thank you!
User avatar
Tony Li
Posts: 20700
Joined: Thu Jul 18, 2013 1:27 pm

Re: Selecting nodes in a conversation randomly

Post by Tony Li »

Happy to help!
User avatar
DryreL
Posts: 29
Joined: Sat Dec 26, 2020 1:58 pm

Re: Selecting nodes in a conversation randomly

Post by DryreL »

Thank you!
Post Reply