Randomize Response Positions

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Craftytroll
Posts: 2
Joined: Mon Jan 22, 2018 2:02 am

Randomize Response Positions

Post by Craftytroll »

Hello,

First time using the forum so bear with me!

I am trying to randomize the response menu when I display a set of dialogue options and can't seem to figure out how to do it.

I want to do this so that if the player has played through the game once he would not be able to select the dialog in the same position to get the correct response again.

Ex
First play through
1. Dialog#1
2.Dialog#2
3.Dialog#3

Second playthrough
First play through
1. Dialog#2
2.Dialog#1
3.Dialog#3

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

Re: Randomize Response Positions

Post by Tony Li »

Hi,

Welcome to the forum!

You can add this script to your Dialogue Manager. It will shuffle the response menu:

ShuffleResponses.cs

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

// Add this script to the Dialogue Manager or one of the conversation participants
// to shuffle the responses that will be shown in the player response menu.
public class ShuffleResponses : MonoBehaviour
{
    void OnConversationResponseMenu(Response[] responses)
    {
        for (int t = 0; t < responses.Length; t++)
        {
            var temp = responses[t];
            int r = Random.Range(t, responses.Length);
            responses[t] = responses[r];
            responses[r] = temp;
        }
    }
}
Here's an example scene that contains the script: QuizExample_2017-05-21.unitypackage

It also contains an example of randomly generating numbers for a math quiz.
Craftytroll
Posts: 2
Joined: Mon Jan 22, 2018 2:02 am

Re: Randomize Response Positions

Post by Craftytroll »

Thank you so much that worked great!
User avatar
Tony Li
Posts: 22061
Joined: Thu Jul 18, 2013 1:27 pm

Re: Randomize Response Positions

Post by Tony Li »

Great! Happy to help!
Post Reply