TextServerExtension in Godot – Complete Guide

Welcome to our tutorial on the TextServerExtension class in Godot 4! In the world of game development, the ability to handle text rendering and processing is a fundamental skill. Text rendering is not just about putting words on the screen; it’s about creating an immersive experience for players by providing clear, readable, and beautiful typography. With the latest Godot engine, developers have powerful tools at their disposal to take their game’s textual content to the next level. This tutorial will demystify the TextServerExtension class, explain its place within the Godot ecosystem, and provide actionable examples to integrate custom text solutions in your games.

What Is TextServerExtension?

TextServerExtension is a base class found in Godot 4 meant for creating custom text server implementations – think of it as the backbone for your game’s text rendering plugins. In simple terms, text servers are responsible for the processing and rendering of text within the Godot engine. Since Godot values customization and flexibility, the TextServerExtension class is crucial for developing advanced text functionalities that go beyond the default capabilities.

What Is It For?

The TextServerExtension is designed for extending Godot’s text rendering system. It’s the foundation upon which you can build plugins to manipulate text in various ways. This could include adding new fonts, implementing custom shaping and layout algorithms, or extending language support, especially for languages with complex typography such as Arabic or Hindi.

Why Should I Learn It?

Understanding the TextServerExtension is essential if you want to:

– Add specialized text features to your games not covered by the standard text server.
– Improve the performance of text rendering for large volumes of text or in performance-critical applications.
– Provide better support for languages with non-Latin alphabets and sophisticated typographic rules.
– Tailor the text rendering process to match your game’s unique visual style.

By learning how to utilize the TextServerExtension class effectively, you can unlock a high degree of text customization and give your games a professional edge. Let’s embark on this coding journey and harness the power of Godot’s text rendering capabilities!

CTA Small Image

Creating a Custom TextServer Extension

To start using your own TextServerExtension in Godot 4, you first need to create a new class that extends it. This class will declare the methods and properties that dictate how your custom text server behaves. Here’s an example of how you might set up a basic extended class:

extends TextServerExtension

func _init():
    # Constructor for your custom text server extension

func create():
    # Code to initialize a new text server

func free():
    # Code to handle the cleanup when your text server is no longer in use

This code snippet represents a template for your TextServerExtension. It includes placeholder functions where you would implement the logic for initializing and cleaning up your text server.

Registering the Custom TextServerExtension

Once you have created your custom TextServerExtension class, you need to register it with the engine. This allows Godot to recognize and utilize your TextServerExtension when the game is running.

func _enter_tree():
    # Register the text server extension when the node enters the scene tree

func _exit_tree():
    # Unregister the text server extension when the node exits the scene tree

In the above code, the _enter_tree() and _exit_tree() are lifecycle methods for a Godot Node that automatically register and unregister your TextServerExtension when it’s added to or removed from the scene tree.

Overriding TextServer Methods

To modify how text is handled, you need to override certain methods from the TextServerExtension class. For example, you can override the method responsible for shaping text (which adjusts the positioning of characters for display).

func shape_string(text: String) -> Array:
    # Custom shaping logic goes here
    # Return an array of shaped characters (glyphs)
    var shaped_text = []
    # Your logic to shape the text
    # ...

    return shaped_text

This is a simplified version of what the shaping function might look like. In practice, the shaping logic can be complex, especially for scripts that require contextual glyph substitutions.

Custom Rendering

Beyond shaping, you might also want to override rendering methods to handle the drawing of text. You can employ Godot’s built-in drawing methods or build a more sophisticated rendering system depending on the requirements of your game or application.

Here is an example of how you could override the draw method within your TextServerExtension class:

func draw_string(canvas_item: RID, pos: Vector2, text: String, modulate: Color, clip_w: float = -1):
    # Custom rendering logic
    # Drawing the text with the provided arguments
    VisualServer.canvas_item_add_text(canvas_item, pos, text, font, size, modulate, clip_w)

In this snippet, the draw_string method uses the VisualServer to draw text on a canvas item, which is highly customizable for whatever rendering needs you might have.

Each of these examples represents a foundational building block for creating a full-featured, custom TextServerExtension in Godot 4. As you gain mastery over these basics, you’ll be well-prepared to tackle more advanced text rendering and processing tasks.Implementing a custom TextServerExtension in Godot 4 can be a challenging task, but it’s also incredibly rewarding, allowing for a high level of customization. Here are additional examples and explanations to further enhance your understanding of building a comprehensive TextServerExtension.

Handling Font Data

When dealing with fonts, you must override methods for adding and removing font resources within your custom TextServerExtension. You can manage the fonts as you see fit, possibly with a reference counting system to optimize memory usage.

var fonts = {}

func add_font_data(font_data: PackedByteArray):
    # Add the logic to add font data and assign an ID
    var font_id = str(len(fonts) + 1)
    fonts[font_id] = font_data
    return font_id

func remove_font_data(font_id: String):
    # Remove the font data from your fonts dictionary

In this example, we’re using a dictionary to store font data with unique IDs as the keys. The `add_font_data` method accepts a binary array representing the font data and assigns an ID to it. The `remove_font_data` method erases the font entry using the given ID.

Custom Font Fallback Mechanism

Characters not available in the primary font can be rendered using fallback fonts. You may override a method to manage fallback fonts in a custom text server.

func set_fallback(font_id: String, fallback: String):
    # Custom logic to set a fallback font for the specified primary font
    if fonts.has(font_id):
        fonts[font_id].fallback = fallback

In this snippet, we’re setting an optional property `fallback` on a font within the `fonts` dictionary, applying the logic for falling back to another font if the primary font is missing glyphs.

Language and Script Support

Since Godot aims to support game developers across the globe, supporting multiple languages and scripts is important. A custom TextServerExtension can override methods for script and language support.

func has_script_support(script_name: String) -> bool:
    # Return true if your text server supports the script
    return script_name in supported_scripts

func get_language_list(script_name: String) -> Array:
    # Return the list of languages supported for a given script
    foreach script in supported_scripts:
        if script.name == script_name:
            return script.languages_supported
    return []

These methods inform the Godot engine about your TexServerExtension’s capabilities in regard to different scripts and languages.

Custom Text Bidi Overrides

Bidirectional text (or BiDi) handling is a complex aspect of text rendering. For languages such as Hebrew and Arabic, you must override the corresponding methods to provide proper BiDi handling.

func reorder_bidi(text: String, bidi_override: int) -> String:
    # Implement your logic for text reordering according to BiDi rules
    var reordered_text = apply_bidi_algorithm(text, bidi_override)
    return reordered_text

This method would use an algorithm, which you would need to implement, to reorder the text characters for bidirectional scripts according to the Unicode Bidirectional Algorithm.

Handling Glyphs and Shaping

For languages with complex shaping rules, such as Devanagari or Tamil, handling the shaping of glyphs is essential.

func shape_glyphs(text: String) -> Array:
    # Apply complex shaping rules and return an array of shaped glyphs
    var shaped_glyphs = complex_shaping(text)
    return shaped_glyphs

Here, `complex_shaping` is a stand-in for your shaping logic, which would translate strings into an array of glyphs that are ready to be rendered on screen, taking into account the necessary ligature and contextual substitution rules.

Working with the TextServerExtension class provides profound control over the text rendering process. The possibilities are broad, and the examples given here offer a road map to creating a fully-functional custom text system tailored specifically for your game’s needs. It may require a deep dive into typography and text processing, but the payoff is a highly personalized and optimized text rendering system that can support any language or script your game requires.Continuing with our journey into extending Godot’s text capabilities, we dive deeper into more complex processes such as selection handling, cursor movement, and rendering colored text. These features are crucial for UI-heavy games or applications with text editors, chat systems, or dynamic in-game reading materials.

Handling Text Selection

To enable text selection in a custom TextServerExtension, you’ll want to provide methods that can define and retrieve selected ranges within the text. This involves keeping track of the selection start and end points and could look something like this:

var selection_start = 0
var selection_end = 0

func set_selection(start: int, end: int):
    # Logic to set selection range
    selection_start = start
    selection_end = end

func get_selection() -> Array:
    # Logic to get the current selection
    return [selection_start, selection_end]

The `set_selection` method updates the properties that track the range of selected text, while the `get_selection` returns the current selection boundaries.

Cursor Movement in Text

For applications that require text input, handling cursor movement is essential. In these cases, your custom text server should be able to advance the cursor by character or word.

func move_cursor_by_character(position: int, forward: bool) -> int:
    # Logic to move the cursor by one character
    var new_position = position + (forward ? 1 : -1)
    return new_position

func move_cursor_by_word(position: int, forward: bool) -> int:
    # Logic to move the cursor by one word
    var new_position = find_next_word_boundary(position, forward)
    return new_position

In these examples, `move_cursor_by_character` shifts the cursor one character at a time, while `move_cursor_by_word` finds the next word boundary and moves the cursor accordingly. Proper implementation would also include bounds checking to prevent the cursor from moving beyond the text length.

Rendering Colored Text

Sometimes you’ll want to add a splash of color to your text, whether it’s to highlight in-game items, differentiate players in a chat, or draw attention to critical game instructions. Here’s how one might override a method to support colored text rendering:

func draw_colored_string(canvas_item: RID, pos: Vector2, text: String, color_array: PoolColorArray, modulate: Color):
    # Logic to render each character with a specific color
    var offset = Vector2()
    for i in range(text.length()):
        var character = text.substr(i, 1)
        var color = (i < color_array.size()) ? color_array[i] : modulate
        offset += draw_character(canvas_item, pos + offset, character, color)

In this function, `draw_character` would be another method implemented to draw individual characters with the specified color and to return the advance (width) of the drawn character to offset the position for the next one.

Performance Considerations

When you’re extending the text rendering functionality, it’s important to consider the performance implications, especially when working with dynamic or large bodies of text. Here’s an example of how you might handle caching with your custom text server to avoid re-shaping or re-rendering text unnecessarily:

var shape_cache = {}

func shape_and_cache(text: String) -> Array:
    if shape_cache.has(text):
        return shape_cache[text]
    var shaped_text = shape_glyphs(text)
    shape_cache[text] = shaped_text
    return shaped_text

func draw_cached_text(canvas_item: RID, pos: Vector2, text: String, modulate: Color):
    var shaped_text = shape_and_cache(text)
    # Render the shaped text...

In this example, the `shape_and_cache` method checks if the shaped text already exists in the cache before performing the shaping process. It’s a basic form of memoization that saves the results of expensive function calls and reuses those results when the same inputs occur again.

Developing your own TextServerExtension is a balancing act between customization and efficiency. These examples provide stepping stones to achieving a customized text rendering pipeline, but keep in mind that each project may demand specific optimizations and error handling to ensure a smooth and immersive gaming experience.

Continuing Your Godot Journey

The world of Godot engine offers a vast landscape for developers to explore, and mastering TextServerExtension is just the beginning. To continue enhancing your Godot skills, we at Zenva offer a highly curated syllabus through our Godot Game Development Mini-Degree. This comprehensive collection spans multiple facets of game development, ensuring that whether you’re a newcomer or a veteran developer, there’s always something new to learn.

Our Godot courses cover a broad range of topics that will deepen your understanding of the engine’s capabilities and help you apply them to create engaging, cross-platform games. For an expansive selection of our tutorials, visit our Godot courses, which offer structured learning paths and actionable knowledge to bolster your game development career. With Zenva, empower your passion by learning at your own pace and build a portfolio that stands out. Happy coding!


As you can see, delving into custom text rendering with the TextServerExtension class in Godot 4 unlocks new possibilities for your game’s typography, allowing you to craft texts that not only convey information but also enhance the player’s immersive experience. Remember, the beauty of game development lies in the details, and something as fundamental as text can significantly influence the aesthetics and functionality of your game.

Whether you’re just embarking on your coding adventure or looking to refine your game development prowess, Zenva’s Godot Game Development Mini-Degree is your gateway to mastering not just text rendering, but a whole universe of game development techniques. Join us, and let’s turn your vision into a playable reality—one line of code at a time!

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.