Issues with using a timer to pause text animation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By juneery

Hi! I’m making a dialogue box where I want text to appear slowly over time. I already managed to achieve this effect by using the following code on a Label:

func _physics_process(_delta):
    if visible_characters < get_total_character_count():
        visible_characters += 1

This works, but I also wanted the text animation to pause whenever it met a punctation mark. To do this, I attached an autostart oneshot Timer node to the Label and replaced the old code with this:

func _on_Timer_timeout():
    visible_characters += 1
    if text[visible_characters] in [".", ","]:
        $Timer.start(0.5)
        print('gotcha!') # test
    else:
        $Timer.start(0.05)

    if visible_characters == get_total_character_count():
        $Timer.queue_free()

My issue is that the if statement seems to “lag behind”… It only detects the punctuation a few letters after the actual punctuation mark. What’s the issue here? Am I missing something?

For anyone coming to this question later, changing the type of the Label to a RichTextLabel also seems to do the trick, since spaces are counted as characters in a RichTextLabel!

juneery | 2022-10-21 13:50

:bust_in_silhouette: Reply From: jgodfrey

Hmmm…

It seems that the visible_characters property of the Label control ignores space characters. Here’s some info:

Not sure why that’s the case. One way to handle it would be to count the spaces as they occur and modify your offsets accordingly. Something like:

var space_count = 0
func _on_Timer_timeout():
	visible_characters += 1
	if text[visible_characters] == ' ': space_count += 1
	if text[visible_characters + space_count - 1] in [".", ","]:
		$Timer.start(0.5)
		print('gotcha!') # test
	else:
		$Timer.start(0.05)

That is odd! Hope that gets fixed at some point. This suggestion seems to work, so thanks for the explanation!

juneery | 2022-10-21 13:44

On the surface, it seems odd to me, but… Thinking about it, I’d guess spaces are ignored to ensure a rhythmic cadence to the exposure of the next visible character. If spaces were considered, and “exposed” just like any visible character, it’d break the visual cadence because there would be a “delay” in the visuals when a space was “exposed”…

Not sure if that’s really why, but it seems plausible. That said, it’d be nice to turn that feature on/off via a property.

jgodfrey | 2022-10-21 13:54