Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Announcements, support questions, and discussion for the Dialogue System.
xyztankman
Posts: 45
Joined: Mon Jun 21, 2021 7:48 pm

Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Post by xyztankman »

Hey Tony, I feel like I'm 90% on the way to having this typewriter effect working in script by buttons in my option menu but I'm missing something.

Is there a way to have the typewriterEffect for all dialogues/scenes changed from the the main menu scene and persist through all scenes? My Dialogue manager is already persistent and I was attempting to make this change, but it requires a public variable for typewriterEffect.

Currently I'm using overrides to show several dialogues one time for several scenes, and I was using an override for the main menu (just to display "this is the new dialogue speed").

It worked when I used an instance of a small dialogue tester but the dialogue tester persisted into scenes I did not want to have it in.
Attachments
Texxt Speed.PNG
Texxt Speed.PNG (85.73 KiB) Viewed 122871 times
User avatar
Tony Li
Posts: 21049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Post by Tony Li »

Hi,

You could try adding an OnConversationStart(Transform) method to your script. In this method, get a reference to the current dialogue UI (which may be a dialogue UI override) and set the speeds on its subtitle panels' typewriter effects. Something like:

Code: Select all

private float typewriterSpeed = 90;
private StandardDialogueUI currentUI;

private void OnConversationStart(Transform actor)
{
    // When a conversation starts, record the dialogue UI it's using and set its typewriter speeds:
    currentUI = DialogueManager.instance.activeConversation.conversationView.dialogueUI as StandardDialogueUI;
    SetTypewriterSpeed(typewriterSpeed);
}

private void SetTypewriterSpeed(float speed)
{
    typewriterSpeed = speed;
    foreach (var subtitlePanel in currentUI.conversationUIElements.subtitlePanels)
    {
        subtitlePanel.SetTypewriterSpeed(speed);
    }
}

public void SetFastSpeed()
{
    DialogueManager.displaySettings.subtitleSettings.subtitleCharsPerSecond = 120;
    DialogueManager.displaySettings.subtitleSettings.minSubtitleSeconds = 5;
    SetTypewriterSpeed(120);
}
//... etc for remaining methods.
xyztankman
Posts: 45
Joined: Mon Jun 21, 2021 7:48 pm

Re: Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Post by xyztankman »

Thanks again Tony! That's working on this scene, but I just realized when I select "new game" that I'm using DialogueManager.ResetDatabase to clear the game. Is that the best method for starting a game over with default variables? Or maybe I should be doing them individually instead of a full wipe?
User avatar
Tony Li
Posts: 21049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Post by Tony Li »

Hi,

I think that's an unrelated question, correct?

In any case, if you're not using the save system, DialogueManager.ResetDatabase() is the way to reset variables. If you're using the save system, call SaveSystem.ResetGameState() instead.
xyztankman
Posts: 45
Joined: Mon Jun 21, 2021 7:48 pm

Re: Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Post by xyztankman »

I was thinking that the ResetDatabase() was also clearing the typewriter settings because there wasn't a change on my end but it may be something in one of my other scenes that is causing the issue. I'll check to make sure since I'm doing a bit of refactoring over old mess that I've been putting together.

So for ResetGameState(), will that wipe only the current save? I have been using the save system but just want to make sure.
User avatar
Tony Li
Posts: 21049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Post by Tony Li »

That's correct. SaveSystem.ResetGameState() will only clear the save system's current memory. It won't touch saved game files. To delete a saved game file, use SaveSystem.DeleteSavedGameInSlot(#).
xyztankman
Posts: 45
Joined: Mon Jun 21, 2021 7:48 pm

Re: Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Post by xyztankman »

Tony Li wrote: Fri Jan 05, 2024 10:42 pm Hi,

You could try adding an OnConversationStart(Transform) method to your script. In this method, get a reference to the current dialogue UI (which may be a dialogue UI override) and set the speeds on its subtitle panels' typewriter effects. Something like:

Code: Select all

private float typewriterSpeed = 90;
private StandardDialogueUI currentUI;

private void OnConversationStart(Transform actor)
{
    // When a conversation starts, record the dialogue UI it's using and set its typewriter speeds:
    currentUI = DialogueManager.instance.activeConversation.conversationView.dialogueUI as StandardDialogueUI;
    SetTypewriterSpeed(typewriterSpeed);
}

private void SetTypewriterSpeed(float speed)
{
    typewriterSpeed = speed;
    foreach (var subtitlePanel in currentUI.conversationUIElements.subtitlePanels)
    {
        subtitlePanel.SetTypewriterSpeed(speed);
    }
}

public void SetFastSpeed()
{
    DialogueManager.displaySettings.subtitleSettings.subtitleCharsPerSecond = 120;
    DialogueManager.displaySettings.subtitleSettings.minSubtitleSeconds = 5;
    SetTypewriterSpeed(120);
}
//... etc for remaining methods.
Hey Tony, coming back to this after doing some other work on my project.

So I used the code above but am getting the following error when not setting a direct "currentUI". It worked when I set it in public to the override but it did not propagate to other scenes.

Code: Select all

NullReferenceException: Object reference not set to an instance of an object
SettingManager.SetTypewriterSpeed (System.Single speed) (at Assets/Scripts/MenuScripts/SettingManager.cs:585)
SettingManager.DialogueSetSlowSpeed () (at Assets/Scripts/MenuScripts/SettingManager.cs:600)
Should I be using something like

Code: Select all

gameObject.GetComponent();
to find the currentUI?
User avatar
Tony Li
Posts: 21049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Post by Tony Li »

Hi,

Is it getting the NullReferenceException in the OnConversationStart() method?

For brevity and readability, my example code omits error checking. In your own code, you'll probably want to check that currentUI isn't null.
xyztankman
Posts: 45
Joined: Mon Jun 21, 2021 7:48 pm

Re: Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Post by xyztankman »

Tony Li wrote: Wed Jan 10, 2024 10:29 am Hi,

Is it getting the NullReferenceException in the OnConversationStart() method?

For brevity and readability, my example code omits error checking. In your own code, you'll probably want to check that currentUI isn't null.
It seems to be in the SetTypewriterSpeed, but I noticed in the code that OnConversationStart() method is unused
Attachments
Texxt Speed2.PNG
Texxt Speed2.PNG (49.26 KiB) Viewed 122129 times
User avatar
Tony Li
Posts: 21049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set Dialogue System text speed/typerwriter effect via functions for all dialogues

Post by Tony Li »

The Dialogue System uses Unity to call OnConversationStart(Transform) through reflection, so to the code editor it may appear as if it won't be used. If you click on the error in the Console window, the bottom of the Console will show the stack trace, which is the order of method calls. In the error, is SetTypewriterSpeed being called from OnConversationStart?

To verify whether it's finding the currentUI, you could change it to:

Code: Select all

private void OnConversationStart(Transform actor)
{
    // When a conversation starts, record the dialogue UI it's using and set its typewriter speeds:
    currentUI = DialogueManager.instance.activeConversation.conversationView.dialogueUI as StandardDialogueUI;
    if (currentUI == null) Debug.Log("Could not find the dialogue UI to set currentUI.");
    SetTypewriterSpeed(typewriterSpeed);
}
Post Reply