Sorting response buttons depending on some value

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Adeoon
Posts: 33
Joined: Fri Dec 02, 2022 5:53 am

Sorting response buttons depending on some value

Post by Adeoon »

Hi, for my case I have 4 types of response buttons (dialogues buttons): Quest lore, side quest, call to action (trade, craft etc.), random normal talk. I want to order response buttons depending on that type, I don't know where should I code that, or maybe there is a way to do that without coding.
User avatar
Tony Li
Posts: 22140
Joined: Thu Jul 18, 2013 1:27 pm

Re: Sorting response buttons depending on some value

Post by Tony Li »

Hi,

One way to handle this is to set up your dialogue UI's response menu with button positions that are used for different types of responses. If you look at BioWare-style games, for example, their response wheels often put "nice" options in the lower right, "mean" options in the upper right, and skill-based options on the left. You can do the same with the Dialogue System. Set up your buttons, and assign them to the StandardUIResponseMenu component's Buttons list. Note their element numbers in the list (0, 1, 2, etc.). Then include [position=#] markup tags in your response text. If, for example, a response's Menu Text is "[position=2] Your mother was a hamster and your father smelt of elderberries!" then the Dialogue System will use button #2 in the Buttons list.

If that's not what you want to do -- if you instead want to use a straight list of buttons -- you can add a script with an OnConversationLine(Response[]) method to the Dialogue Manager. In this method, you can sort the Response[] array using whatever criteria you prefer.
Adeoon
Posts: 33
Joined: Fri Dec 02, 2022 5:53 am

Re: Sorting response buttons depending on some value

Post by Adeoon »

I can only see OnConversationResponseMenu(Response[]). I don't know if I understood correctly, I attached dialogue system events under my dialogues manager and there I can drag my method which will sort my responses? Also I am not sure, do that method should return array of responses?
User avatar
Tony Li
Posts: 22140
Joined: Thu Jul 18, 2013 1:27 pm

Re: Sorting response buttons depending on some value

Post by Tony Li »

Which approach do you want to take?

If you want to set up your buttons like this:

wheelPositions.png
wheelPositions.png (16.94 KiB) Viewed 483 times

Or like this;

listPositions.png
listPositions.png (5.5 KiB) Viewed 483 times
Then use the Buttons list as I described in the first paragraph of my reply. You don't need an OnConversationLine() method. Note that the second image tries to convey that you can tell the menu to hide buttons that aren't needed. The list will show 0, 2, and 3 in a straight list without spaces for the unused buttons.

If you don't want to, or can't, use [position=#], then you'll need to use OnConversationResponseMenu() [EDIT: had incorrectly typed OnConversationLine()]. Sort the array in place. You don't need a Dialogue System Events component. Just add a script with an OnConversationResponseMenu() method to the Dialogue Manager. Each Response object in the Response[] array points to its destinationEntry (the player response dialogue entry). You can use info in the destinationEntry to determine how to sort the array.
Adeoon
Posts: 33
Joined: Fri Dec 02, 2022 5:53 am

Re: Sorting response buttons depending on some value

Post by Adeoon »

I want second approach, can't use [position=#]. But I still don't know what you mean saying just use OnConversationLine. I've created script I have to attach it to some gameobject? What is dialogue manager, you mean dialogue controller (prefab)? And my script should inherit something to get that method or is it static like DialogueManager.OnConversationLine or what? I don't really know what you mean saying use OnConversationLine.
User avatar
Tony Li
Posts: 22140
Joined: Thu Jul 18, 2013 1:27 pm

Re: Sorting response buttons depending on some value

Post by Tony Li »

Sorry, I typo'd OnConversationLine -- I meant to type OnConversationResponseMenu.

Your scene will have a Dialogue Manager GameObject. You may have put the Dialogue Manager prefab in this scene, or the Dialogue Manager may have survived from a previous scene at runtime. (See the Quick Start tutorial for info about the Dialogue Manager.)

The Dialogue Manager GameObject will receive Dialogue System Script Messages such as OnConversationResponseMenu.

If you create a new script that contains an OnConversationResponseMenu method and add the script to the Dialogue Manager GameObject, the OnConversationResponseMenu method will run just before the Dialogue System shows the menu to the player.

Let's say, for example, that you have added a custom field named "Importance" to your dialogue entries. (See Templates for adding custom fields.) Then you can set the Importance field for each response dialogue entry. You can set Quest Lore entries' Importance to 1, Side Quest entries' Importance to 2, etc.

Then create a script with an OnConversationResponseMenu method that sorts the Response[] array by the Importance values. Example:

Code: Select all

using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;

public class SortResponsesByImportance : MonoBehaviour
{
    public void OnConversationResponseMenu(Response[] responses)
    {
        var list = new List<Response>(responses);
        list.Sort((a, b) => (Importance(a).CompareTo(Importance(b))));
        for (int i = 0; i < responses.Length; i++)
        {
            responses[i] = list[i];
        }
    }

    private int Importance(Response response)
    {
        return Field.LookupInt(response.destinationEntry.fields, "Importance");
    }
}
Adeoon
Posts: 33
Joined: Fri Dec 02, 2022 5:53 am

Re: Sorting response buttons depending on some value

Post by Adeoon »

Okay, It seems to work now. Thank you very much! :)
User avatar
Tony Li
Posts: 22140
Joined: Thu Jul 18, 2013 1:27 pm

Re: Sorting response buttons depending on some value

Post by Tony Li »

Glad to help!
Post Reply