Randomize PC responces

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
hellwalker
Posts: 112
Joined: Tue Jan 19, 2016 11:37 pm

Randomize PC responces

Post by hellwalker »

Hi,
So I have a system where I have ~10 player responses and I want to randomly show only 3.
Since I have only 3 response buttons setup in response panel, that's not a problem, but the dialogue always shows first three responses. Is there an easy way to randomize link priorities for responses?

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

Re: Randomize PC responces

Post by Tony Li »

There are a few ways you can do this.

The easiest way takes advantage of the fact that you've only set up 3 response buttons. If you shuffle the 10 responses first, the response menu will appear to randomly show 3 responses. It's actually provided all 10 responses, but since they're in a shuffled order, the first 3 are randomly chosen from the 10. To do this, add a script to the Dialogue Manager that has an OnConversationResponseMenu method:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;
public class ShuffleResponses : MonoBehaviour
{
    void OnConversationResponseMenu(Response[] responses)
    {
        if (responses.Length > 3) // If we have more than 3 responses, shuffle them.
        {
            int n = responses.Length; // Standard Fisher-Yates shuffle algorithm.
            for (int i = 0; i < n; i++)
            {
                int r = i + Random.Range(0, n - i);
                Response temp = responses[r];
                responses[r] = responses[i];
                responses[i] = temp;
            }
        }
    }
}

Another approach is to use the Conditions field on the responses. Briefly, you'll randomly choose 3 unique numbers x, y, and z. Then set the Conditions on the responses so that only 3 responses of the 10 will equal x, y, or z. If you want to use this approach and would like more details, just let me know.
hellwalker
Posts: 112
Joined: Tue Jan 19, 2016 11:37 pm

Re: Randomize PC responces

Post by hellwalker »

Thank you! I think the combination of the two would be best for me. I only need to shuffle responses in specific cases. So maybe if I tag responses or conversation with certain field and shuffle only conversations with that field? Would this work?
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Randomize PC responces

Post by Tony Li »

Yes, that would work. For example, you could add a Boolean field named "Shuffle" to the dialogue entry template, with a default value of false. When you want responses to be shuffled, on one or more of them set the field to true.

You could modify the OnConversationResponseMenu code above to first loop through all of the responses. If any of them have a Shuffle field that's set to true, shuffle the responses. Otherwise leave them as-is.
Post Reply