Page 1 of 1

Selecting nodes in a conversation randomly

Posted: Wed Nov 11, 2015 2:36 pm
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

Re: Selecting nodes in a conversation randomly

Posted: Wed Nov 11, 2015 10:25 pm
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?"))]

Re: Selecting nodes in a conversation randomly

Posted: Thu Nov 12, 2015 11:10 am
by nishant
Perfect ... that solves it

Thanks :)

Re: Selecting nodes in a conversation randomly

Posted: Tue Nov 07, 2017 9:52 am
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.

Re: Selecting nodes in a conversation randomly

Posted: Wed May 11, 2022 1:01 pm
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.

Re: Selecting nodes in a conversation randomly

Posted: Wed May 11, 2022 1:08 pm
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."

Re: Selecting nodes in a conversation randomly

Posted: Wed May 11, 2022 2:10 pm
by sunwooz
Perfect, thank you!

Re: Selecting nodes in a conversation randomly

Posted: Wed May 11, 2022 2:52 pm
by Tony Li
Happy to help!

Re: Selecting nodes in a conversation randomly

Posted: Fri Feb 24, 2023 3:31 pm
by DryreL
Thank you!