Changing Dialogue Speed With C#

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
HawkX
Posts: 147
Joined: Mon Feb 27, 2017 1:50 pm
Location: Quebec
Contact:

Changing Dialogue Speed With C#

Post by HawkX »

Hi again! :)

I managed to do all the settings I wanted except for one...

I am trying to figure out how to get/set the value of the subtitlemanager.(chars per seconds) in C#...

I was able to create a public UnityTypeWriterEffect utwe -- and use that to get/set its attribute utwe.charactersPerSecond...

But creating a public DialogueManager doesnt give me access to any of its settings :P

EDIT : I think I just found it!
DialogueManager.DisplaySettings.subtitleSettings.subtitleCharsPerSecond = (float slider value);
does that seem right?

I am still curious to know if there any actual documentation anywhere that I missed on super specific stuff like that? or should I keep bugging you every time i have a question like this? :) I actually feel bad to bother you for every little details but you give such amazing answers every time! :)
User avatar
Tony Li
Posts: 22062
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing Dialogue Speed With C#

Post by Tony Li »

I'm here to help, and happy to do so!

You can access the Dialogue Manager's Display Settings using the static property DialogueManager.DisplaySettings. For example:

Code: Select all

DialogueManager.DisplaySettings.subtitleSettings.subtitleCharsPerSecond = 50;
The Dialogue Manager's Display Settings > Input Settings > Subtitle Chars Per Second only affects the '{{end}}' keyword value in sequences. For example:

Code: Select all

AnimatorPlay(MoveMouth);
AnimatorPlay(CloseMouth)@{{end}}
Separately, each typewriter effect component has its own Characters Per Second value that's independent of Display Settings.

Let's say you plan to keep all typewriter effects at the same speed. You could write two methods similar to these:

Code: Select all

int GetTypewriterSpeed() {
    var typewriter = FindObjectOfType<UnityUITypewriterEffect>();
    return (typewriter != null) ? typewriter.charactersPerSecond : 1;
}

void SetTypewriterSpeed(int charsPerSec) {
    // Make sure typewriter will always finish by {{end}}:
    charsPerSec = Mathf.Max(charsPerSec, DialogueManager.DisplaySettings.inputSettings.subtitleCharsPerSecond);
    
    // Set all typewriter components:
    foreach (var typewriter in FindObjectsOfType<UnityUITypewriterEffect>()) {
        typewriter.charactersPerSecond = charsPerSec;
    }
}
This way it should catch all of them, even if your scene has multiple dialogue UIs.

(BTW, back on the Unity forum thread, there's a post with an updated Unity UI Typewriter Effect. It adds better handling of auto-scrolling. If you don't use auto-scrolling, it doesn't include anything useful for you.)
User avatar
HawkX
Posts: 147
Joined: Mon Feb 27, 2017 1:50 pm
Location: Quebec
Contact:

Re: Changing Dialogue Speed With C#

Post by HawkX »

As always WOW... such an amazing answer! :)

Thanks for the info about new typewriter... but as you guessed, I only keep my dialogues in one window (no scrolling)... so that wouldn't be useful...
I also plan "for now at least" to only use my 1 Dialogue UI (so only 1 typewriter as well) but the info is good to know if I ever decide to change that.

When I analyse your code I realize how "noob" I still am... not sure if I wrote it before, but I am a designer and video editor first... I only started learning C# less than a year ago... so there is still a lot of the syntax I dont quite understand...
this being reference to that line here :
"return (typewriter != null) ? typewriter.charactersPerSecond : 1;"
I never used a ? or : in any of my programming yet... and i only use "return;" when I need to exit a method ;)
also never used method other than void... so there is a LOT of room for improvement on my side!

Here is my current final slider script :

Code: Select all

	public void SliderTextSpeed (float TSValue)
	{
			DialogueManager.DisplaySettings.subtitleSettings.subtitleCharsPerSecond = TSValue;
			dialogueTypeWriter.charactersPerSecond = TSValue;

			textAutoSpeed.text = (TSValue.ToString("N0") + " chars per second");
	}

If this looks "alright" then I guess I'm done with that ;)

Here is another slider I made for a "Wait Delay When In Auto Mode" based on what you gave me last week :

Code: Select all

	public void SliderWaitTime (float WTValue)
	{
			DialogueLua.SetVariable("finalDelay", WTValue);
			textWaitTime.text = (WTValue.ToString("N0") + " seconds");
	}
However I'd like to know how to access that "finalDelay" variable (more precisely, how to GET its value) so I can set the slider.value manually to finalDelay value when the player enters the option menu the first time.
User avatar
Tony Li
Posts: 22062
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing Dialogue Speed With C#

Post by Tony Li »

HawkX wrote:I only started learning C# less than a year ago... so there is still a lot of the syntax I dont quite understand...
this being reference to that line here :
"return (typewriter != null) ? typewriter.charactersPerSecond : 1;"
I never used a ? or : in any of my programming yet...
Most programmers are inherently lazy, me certainly included. ;-) The '?' syntax is shorthand for:

Code: Select all

if (typewriter != null) {
    return typewriter.charactersPerSecond;
} else {
    return 1;
}
HawkX wrote:However I'd like to know how to access that "finalDelay" variable (more precisely, how to GET its value) so I can set the slider.value manually to finalDelay value when the player enters the option menu the first time.

Code: Select all

// Set slider:
slider.value = DialogueLua.GetVariable("finalDelay").AsFloat;
User avatar
HawkX
Posts: 147
Joined: Mon Feb 27, 2017 1:50 pm
Location: Quebec
Contact:

Re: Changing Dialogue Speed With C#

Post by HawkX »

Thank you again for taking the time to "educate" me ;)

and AMAZING! that was exactly what i needed!

Alright Options Menu DONE and working!
User avatar
Tony Li
Posts: 22062
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing Dialogue Speed With C#

Post by Tony Li »

:D
User avatar
HawkX
Posts: 147
Joined: Mon Feb 27, 2017 1:50 pm
Location: Quebec
Contact:

Re: Changing Dialogue Speed With C#

Post by HawkX »

Hey there! :)

A lot of the time I am getting an error like this :
NullReferenceException: Object reference not set to an instance of an object

When doing this :
FindObjectOfType<UnityUITypewriterEffect> ().charactersPerSecond = speedTextVar;

I am using that line in my public void OnConversationStart

I even added an invoke with 0.1f to make sure it had finished loading and appearing... but it still happen sometimes... and If i were to click "skip" faster than 0.1f it happens as well! :P

Would you have a better suggestion as to "how" to change this one? Since its not static i cannot simply change its value like i do for the dialoguemanager chars per seconds...
User avatar
Tony Li
Posts: 22062
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing Dialogue Speed With C#

Post by Tony Li »

Hi,

Try adding this public variable to your script:

Code: Select all

public UnityUITypewriterEffect typewriterEffect; 
And change your code from this:

Code: Select all

FindObjectOfType<UnityUITypewriterEffect> ().charactersPerSecond = speedTextVar; 
to:

Code: Select all

(if (typewriterEffect != null) typewriterEffect.charactersPerSecond = speedTextVar; 
Finally, in the inspector assign the typewriter effect to your script.

This guarantees that your script always has a good reference to the typewriter effect, and it's also faster because it won't have to find the typewriter effect every single time.
User avatar
HawkX
Posts: 147
Joined: Mon Feb 27, 2017 1:50 pm
Location: Quebec
Contact:

Re: Changing Dialogue Speed With C#

Post by HawkX »

whoa... i never got the notification email for this one!

and i have been so ultra busy with the demo that i never even though about this "bug" until today! lol...

in case you were wondering, the demo last Friday went amazingly well, we did not get funding as they are looking for existing property that already have an existing player base of many thousands every day... however, since they were so impressed with us, they told us we could launch on their platform as soon as June if we only add a little bit more content! :)


As for your last answer.... I do use public variables all the time... the problem being that in this case the OnDialogueStart script is in another scene... so i had to do a find... BUT I just remember you telling me i could have multiple ondialoguestart (specifically one on the manager directly), so i'll put one on there that does only that changes! :) Thanks for the idea!!

I will most likely be back in a couple of days for more questions! ;)
Last edited by HawkX on Mon Apr 10, 2017 10:31 am, edited 1 time in total.
User avatar
Tony Li
Posts: 22062
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing Dialogue Speed With C#

Post by Tony Li »

Sorry you didn't get the funding (it sounds like they were looking for an existing success where they could just jump on the back bumper and ride along for profit), but it's great to hear the demo went well! And, for a new property, an invitation to launch on a platform is a great opportunity!
Post Reply