← Pixel Dreamer Studio

Type dialogue at the same letters per second on every line and every frame rate

Count time, not frames, and let RichTextLabel count the letters so BBCode tags do not hold up the end.

Two common ways to type out dialogue in a RichTextLabel change speed without telling you. Tweening visible_ratio from 0 to 1 over 2 seconds typed a 12 letter line at 6 letters a second and a 120 letter line at 60 in our Godot 4.7 test. Adding 1 to visible_characters every frame took 4 s at 30 fps and 0.83 s at 144 fps. Turning the time into a letter count typed 30 letters a second on every line at 30, 60 and 144 fps.

The code

Put the script on a RichTextLabel with BBCode Enabled on, and call show_line(text) for each line. Connect finished_typing to show your next-line arrow, and call skip() when the player clicks while it is still typing.

extends RichTextLabel
## Typewriter text at the same characters per second on every line and at any
## frame rate. BBCode tags are not counted, and finished_typing comes on time.

signal finished_typing

@export var chars_per_second := 30.0
var _t := 0.0
var _total := 0
var _typing := false


func show_line(line: String) -> void:
	text = line
	visible_characters = 0
	_t = 0.0
	_total = get_total_character_count()  # letters only, without BBCode tags
	_typing = true


func skip() -> void:  # e.g. on click: show the whole line now
	_t = _total / chars_per_second


func _process(delta: float) -> void:
	if not _typing:
		return
	_t += delta
	visible_characters = mini(int(_t * chars_per_second + 0.0001), _total)  # + 0.0001: summed deltas land just under whole numbers
	if visible_characters >= _total:
		_typing = false
		finished_typing.emit()

What we measured

Three lines: 12 letters, 120 letters, and a BBCode line of 54 characters where 28 are letters on screen. Each frame stepped with a fixed delta. Two runs with the same numbers.

Why it happens

visible_ratio is a share of the line, so a fixed time gives a fixed duration, not a fixed speed. Frames come faster on a faster screen. And text.length() counts every character of the BBCode tags, while visible_characters only counts what is drawn, which is what get_total_character_count() returns. Summed frame times also land a hair under whole numbers, which is why the script adds 0.0001 before int().

What to change

Change the scene behind the text: visual novel background crossfade.

Backgrounds for your visual novel: Asian Visual Novel Backgrounds has 12 scenes at 1920 x 1080 with the landmark on the left or right third, so the bottom middle stays clear for characters.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Asian Visual Novel Backgrounds has 12 scenes at 1920 x 1080 with the landmark on the left or right third, so the bottom middle stays clear for characters.

More how-tos: UI and HUD