[Fixed] Respon Not Working

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Fourius_Craft
Posts: 3
Joined: Fri Nov 24, 2023 11:46 am

[Fixed] Respon Not Working

Post by Fourius_Craft »

Hi, I'm currently learning how to create my own custom dialogue UI using the Unity Nova UI asset. Everything is going well, but there is a slight problem with the responses. When I click one of the response buttons, the current status of the response does not happen anything.

https://youtu.be/2sWBREl79cg

The button itself is not a problem, it seems like I'm missing something.

Custom UI script using IDialogueUI

Code: Select all


using UnityEngine;
using System;
using PixelCrushers.DialogueSystem;
using Nova;
using Unity.Mathematics;


public class HikmaUi : MonoBehaviour, IDialogueUI
{
    
    public GameObject dialouePanel;
    public TextBlock dialogueText;
    public TextBlock speakterName;
    public UIBlock2D actorPotraitPanel;
    public event EventHandler<SelectedResponseEventArgs> SelectedResponseHandler;
    public bool isItalic;
    public bool isPlay;
    public Transform dialoguePosition;
    public GameObject responPanel;
    public GameObject ButonExample;
    public GameObject rootRespon;
    
    public void Open()
    {
        dialouePanel.SetActive(true);
        
    }

    public void Close()
    {
        // Add your code here to do any cleanup at the end of a conversation -- for example,
        // deactivating GUI controls.
        transform.position = Vector3.one;
        isPlay = false;
        dialouePanel.SetActive(false);
    }

    public void ShowSubtitle(Subtitle subtitle)
    {
        if (isPlay == false)
        {
            transform.position = subtitle.speakerInfo.transform.position + new Vector3(0,2,0);
            isPlay = true;
        }
        
        actorPotraitPanel.SetImage(subtitle.GetSpeakerPortrait());
        //Debug.Log();
        speakterName.Text = subtitle.speakerInfo.Name;
        dialogueText.Text = subtitle.formattedText.text;
        
        // Add your code here to show a subtitle. Subtitles contain information such as the player 
        // type (PC or NPC), portrait texture, and the formatted text of the line.
    }

    public void HideSubtitle(Subtitle subtitle)
    {
       
    }

   

    public void ShowResponses(Subtitle subtitle, Response[] responses, float timeout)
    {
        Cursor.visible = true;
        Cursor.lockState = CursorLockMode.None;
        for (int i = 0; i < responses.Length; i++)
        {
            GameObject responDesu =  Instantiate(ButonExample, rootRespon.transform);
            HikmaRespon hikmaRespon = responDesu.GetComponent<HikmaRespon>();
            hikmaRespon.response = responses[i];
            hikmaRespon.text = responses[i].formattedText.text;
        }
        
        responPanel.SetActive(true);

        
        // Add your code here to show the player response menu. Populate the menu with the contents
        // of the responses array. When the player selects a response, call:
        //    SelectedResponseHandler(this, new SelectedResponseEventArgs(response));
        // where the argument "response" is the response that the player selected.
        // If (timeout > 0), auto-select a response when timeout seconds have passed.
    }

    public void HideResponses()
    {
        // Add your code here to hide the player response menu.
       
        
        responPanel.SetActive(false);
        
        Cursor.visible = true;
        Cursor.lockState = CursorLockMode.None;
    }

    public void ShowQTEIndicator(int index)
    {
        /// Add your code here to show the Quick Time Event (QTE) indicator specified by the given 
        /// index. If your project doesn't use QTEs, you can leave this method empty.
    }

    public void HideQTEIndicator(int index)
    {
        /// Add your code here to hide the Quick Time Event (QTE) indicator specified by the given 
        /// index. If your project doesn't use QTEs, you can leave this method empty.
    }

    public void ShowAlert(string message, float duration)
    {
        // Add your code here to show an alert message. Note that the dialogue system will not
        // call Open() or Close() for alerts, since alerts are meant to be shown outside of
        // conversations.
    }

    public void HideAlert()
    {
        // Add your code here to hide the alert message if it's showing.
    }

}





Script for responui

Code: Select all


using Nova;
using PixelCrushers.DialogueSystem;
using UnityEngine;
using UnityEngine.EventSystems;

public class HikmaRespon : MonoBehaviour
{
    // Start is called before the first frame update
    
    public virtual Response response { get; set; }
    public TextBlock labelText;
    
    public virtual string text
    {
        
        set
        {
            labelText.Text = value;
            
        }
    }

    public void SetRespon()
    {
        if (DialogueManager.instance.conversationController != null)
        {
            DialogueManager.instance.conversationController.SetCurrentResponse(response);
            Debug.LogWarning("Button press");
        }else
        {
            Debug.LogWarning("Null");
        }
        
    }
    
}


Last edited by Fourius_Craft on Sat Nov 25, 2023 11:33 am, edited 1 time in total.
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: Respon Not Working

Post by Tony Li »

Hi,

You're almost there. After calling SetCurrentResponse, invoke the SelectedReponseHandler event:

Code: Select all

DialogueManager.instance.conversationController.SetCurrentResponse(response);
SelectedResponseHandler?.Invoke(this, new SelectedResponseEventArgs(data as Response));
Fourius_Craft
Posts: 3
Joined: Fri Nov 24, 2023 11:46 am

Re: Respon Not Working

Post by Fourius_Craft »

Hi,

Thank you for your response. I invoke the SelectedResponseHandler that is in IDialogueUI? Can you give me an example? Why is it still not working for me?
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: Respon Not Working

Post by Tony Li »

I assume HikaRespon is on the response button? And when the player clicks the response button is calls SetRespon()? If so, then pass a reference to SelectedResponseHandler to HikaRespon so you can call it in SetRespon().
Fourius_Craft
Posts: 3
Joined: Fri Nov 24, 2023 11:46 am

Re: Respon Not Working

Post by Fourius_Craft »

thank you, it's working now. I still have some questions about this part of the code:

Code: Select all


public void ShowResponses(Subtitle subtitle, Response[] responses, float timeout)
    {
        Cursor.visible = true;
        Cursor.lockState = CursorLockMode.None;
        for (int i = 0; i < responses.Length; i++)
        {
            GameObject responDesu =  Instantiate(ButonExample, rootRespon.transform);
            HikmaRespon hikmaRespon = responDesu.GetComponent<HikmaRespon>();
            hikmaRespon.response = responses[i];
            hikmaRespon.text = responses[i].formattedText.text;
            hikmaRespon.SelectedResponseHandler = SelectedResponseHandler;
        }
        
        responPanel.SetActive(true);

        
    }


I'm wondering if the code is already optimal, because it keeps calling the getcomponen response every time it shows? Is there a solution?
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: Respon Not Working

Post by Tony Li »

You could cache it. But, even though the extra complexity is minimal, there's really no practical need. You should avoid GetComponent in methods such as Update(), but if you're only using it once to set up the response menu, it will have no performance impact. Every bit of extra complexity (such as caching the values) adds potential for bugs in your code. Keep your code as simple as possible.
mXispitlk
Posts: 2
Joined: Mon Dec 18, 2023 7:46 am

Re: [Fixed] Respon Not Working

Post by mXispitlk »

Could you pls provide your code or at least the hierarchy of your UI?

I am trying to implement also Nova UI but to be honest not too sure how to start and if I implemented the IDialogueUI.

It looks simple but probably needs some kind of structure to work properly or isn't it?
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: [Fixed] Respon Not Working

Post by Tony Li »

Hi,

Here are the basic steps:

1. Duplicate the file Plugins / Pixel Crushers / Dialogue System / Templates / Scripts / TemplateDialogueUI.cs. Move it into your own scripts folder and rename it -- for example, to something like NovaDialogueUI.cs.

2. Edit the script. At the top of the script, remove the lines that say "[REMOVE THIS LINE]".

3. Fill in the code where the comments indicate. At a minimum, you should add code to control Nova UI in the Open, ShowSubtitle, and ShowResponses methods. You'll probably also want to add code in the Close, HideSubtitle, and HideResponses methods. Get this working before looking at the Show/HideAlert methods. If you're not planning to use QTEs, you can leave the QTE methods untouched. I realize these instructions are a lot like this meme:

Image

so if someone has already filled in those methods for Nova, it would be nice if you can share with others.

As far as the hierarchy goes, I suggest looking at how the Basic Standard Dialogue UI prefab is structured. You probably won't need a hierarchy that complex, but it's good to understand the underlying ideas.
Post Reply