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.
- Tween
visible_ratioover 2 s: both lines done at 2.00 s, 6.0 and 60.0 letters a second. - +1 per frame: the 120 letter line in 4.00 s at 30 fps, 2.00 s at 60, 0.83 s at 144.
- +1 per frame with the end checked against
text.length()on the BBCode line: all 28 letters were on screen at 0.47 s, but the line counted as done only at 0.90 s, at 60 fps. - The script: 12 letters in 0.40 s and 120 in 4.00 s at all three frame rates, the BBCode line's 28 letters in 0.93 to 0.94 s, and finished_typing on the same frame as the last letter.
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
- Each speaker can have their own
chars_per_second. - For a blip sound per letter, compare
visible_characterswith last frame's value: on a slow frame it can go up by more than 1.
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
- A screen fade that finishes while the game is paused or slowed
- A hand of cards that always fits, without spreading out when it is small
- Edge markers that stay on the rim and point the true way
- A wrapped Label that is as wide as its text, up to a limit
- Big numbers with thousands separators, minus signs and all
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Blink or fade a tinted HUD element without losing its color
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Drag a box to select 3D units, in any direction
- Radar blips that turn with the player
- Pick the radial menu slot the mouse is pointing at
- Show a percent that never says 0% or 100% too early
- Place brush dots at the same spacing however fast the mouse moves
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total