TextLine in Godot – Complete Guide

Welcome to the world of Godot 4, where every line of text on your screen is not just a string but a canvas of creativity waiting to be unleashed. Whether you are a game developer, a UI designer, or just stepping into the realm of coding, understanding how to manage and manipulate text is essential. Text is often the unsung hero of games and applications, delivering critical information, narrative, and interaction cues to the user. Through this tutorial, you’ll discover the power of the TextLine class in Godot 4, learning to control the subtleties of text presentation and behavior in your projects. So let’s dive in and add some life to those lines!

What is the TextLine Class?

The TextLine class in Godot 4 is a sophisticated abstraction that offers developers granular control over a single line of text. By leveraging the capabilities of the TextServer, it creates a powerful interface for text manipulation.

What is it for?

TextLine is designed for instances where you need precise handling of text properties such as alignment, orientation, bidirectional support, and embedding objects within text. It allows for advanced layout adaptations, ideal for multilingual support and rich text applications.

Why Should I Learn It?

Understanding TextLine is key for any developer looking to present text that is not only functional but also aesthetically pleasing. The ability to control text to this degree opens up opportunities for highly polished game interfaces, dynamic in-game reading experiences, and sophisticated controls over how text appears and interacts with other UI elements. So, let’s learn how to harness the full potential of text in your Godot 4 projects!

CTA Small Image

Creating and Customizing a TextLine

First, let’s look at creating a basic TextLine in Godot 4. You’ll need to have a running Godot 4 project open and a script where to place the examples. The following snippet shows how to create a TextLine and set basic properties:

var text_line = TextLine.new()
text_line.set_text("Hello, GameDev!")

Now that we have our TextLine, we can also specify alignment. Here’s how you align your text to the right:


And if you want to give your text a color, you can do so easily:

text_line.set_color(Color(1, 1, 1, 1)) # This sets the text color to white.

Handling Multiple Lines and Wrapping

If you have a block of text that may span multiple lines, you’ll need to handle line wrapping and spacing between lines. Let’s create a TextParagraph to illustrate this:

var text_paragraph = TextParagraph.new()
text_paragraph.set_width(300) # Set the width for wrapping
text_paragraph.set_text("This is a very long paragraph that needs to be wrapped into multiple lines.")

With your paragraph set up, you can also adjust the wrap mode to control how text is split across lines:


For further customization, let’s set the indent of the first line:


Embedding Images and Rects

TextLine also allows for embedding images, rects, and other controls within the text flow, which is great for inline graphics or dynamic content. Here’s an example of embedding an image:

var image = preload("res://path_to_your_image.png")
text_line.add_image(image, Vector2(64, 64)) # Size of the image within the text

For rectangles, similar to images, you can embed them which can be useful for placeholders or dynamic content:

text_line.add_rect(Rect2(Vector2(0, 0), Vector2(100, 50)), false) # Adds a rectangle to the text line

Manipulating Text Orientation and Direction

You may want to change the orientation or direction of your text for certain UI elements or stylistic effects. To rotate your text, you can do:

text_line.set_orientation(90) # Rotates the text 90 degrees

And for setting the direction, especially useful in multilingual applications, you can specify different text directions:

text_line.set_base_direction(TextServer.DIRECTION_LTR) # Sets text direction to left-to-right

By mastering the use of the TextLine class, you’ll have a much more flexible and powerful tool at your disposal for creating sophisticated text layouts in your Godot 4 projects. In the next part, we’ll continue to explore additional functionality and examples to make sure you can fully leverage TextLine in your games and apps. Stay tuned!Continuing from where we left off, manipulating text using the TextLine class in Godot 4 can greatly enhance the interface of your game or application. Let’s dive deeper with more examples to showcase the versatility of this class.

Handling Bi-Directional Text
Bi-directional text handling is crucial for languages that read from right-to-left, like Arabic or Hebrew. Here’s how you can set this up:

text_line.set_base_direction(TextServer.DIRECTION_RTL) # Sets text direction to right-to-left

This ensures that users of all languages can experience the content as intended.

Using Underline and Strikethrough
Sometimes you may want to emphasize text or indicate that it should be ignored, without removing it. You can do this by using underline or strikethrough:

text_line.set_underline_color(Color(0, 0, 1, 1)) # Sets underline color to blue

text_line.set_strikethrough(true) # Enables strikethrough

These stylistic choices can also play a role in your game’s narration or UI feedback mechanisms.

Applying Shading to Text
Adding shading to your text creates depth and can improve readability on varying backgrounds:

text_line.set_shade_amount(0.5) # Adds shading at 50% opacity
text_line.set_shade_color(Color(0, 0, 0, 0.5)) # Sets the shade color to semi-transparent black

It’s a simple yet effective way to make sure your text stands out.

Adjusting Letter Spacing
For a finer control of how text is presented, adjusting the spacing between characters can be very impactful:

text_line.set_outline_color(Color(0, 0, 0, 1)) # Sets an outline color to solid black
text_line.set_letter_spacing(2) # Adds 2 pixels of space between each letter

This can help in creating stylistic titles or improving text legibility.

Animating Text
Animating text properties can make your UI dynamic and engaging. Here’s a simple way to animate the color of the TextLine:

var color_animation = Color(1, 0, 0, 1) # Target color for the animation is red
# Assumes you have a function setup to run a loop or frame-based animation elsewhere

Animating other properties, like underline or shade amount, would follow a similar process. You would creatively interpolate values over time to achieve your desired animation effect.

Rendering Text
You’ve customized your text, and now it’s time to render it. TextLine objects don’t render themselves directly; instead, you need to draw them within a _draw() function on a CanvasItem based node:

func _draw():
    text_line.draw(Offset2D, Color(1, 1, 1, 1)) # Draw the text line at a given offset with white color

Updating Text Content
And finally, changing the text content dynamically is as straightforward as:

text_line.set_text("New text content")

Remember to always update your visual representation after making changes to ensure users see the updated text.

As you can see, the TextLine class in Godot 4 provides you with a toolbox full of customization options for text. From animations to fine-tuning the presentation, these features are critical to weaving text seamlessly into your projects. Mastering these nuances can elevate the quality of your application or game’s user interface, leading to a more professional and polished end-product. Stay creative and keep exploring the depths of Godot 4’s text capabilities!As we delve deeper into the capabilities of the TextLine class in Godot 4, let’s explore how to interact with and conditionally style text. Conditional styling allows for dynamic changes to the text’s appearance based on certain conditions, such as user interaction or game events. This feature can significantly enhance the appeal and functionality of your application’s UI.

Interacting with Text
You may want your text to respond to user input or certain in-game conditions. Here’s an example of changing text color when a condition is met, such as a mouse hover:

func _on_Mouse_entered():
    text_line.set_color(Color(0, 1, 0, 1)) # Changes text color to green on mouse hover

func _on_Mouse_exited():
    text_line.set_color(Color(1, 1, 1, 1)) # Reverts text color to white when mouse is not over it

Remember to connect the mouse signals to the code above for it to work.

Conditional Formatting
More than just color changes, you might want to italicize or bold text based on player achievement or story progress:

# Check for an in-game condition
if player_has_reached_milestone():

This can be used to highlight important achievements or story elements that the player encounters.

Updating Text Position and Size
Sometimes you will need to reposition or resize your text according to gameplay dynamics or UI changes. This is how you dynamically update its position:

# Set a new position for the text
var new_position = Vector2(100, 200)

To resize the text, you would update the bounding size, which impacts text wrapping and layout:

# Update bounding size to a new width

Keep in mind that these methods affect layout, so always check your text visually after making changes.

Adding and Removing Custom Effects
The real fun begins when you start adding and removing custom effects from text. Let’s implement a simple shake effect that can be used for an immersive experience during intense moments in a game:

# Add an effect to shake the text
func add_shake_effect():
    var rng = RandomNumberGenerator.new()
    var shake_effect = TextEffect.new()
    shake_effect.start_offset = rng.randf_range(-10.0, 10.0)

And removing the effect once the intense moment passes is just as easy:

# Remove shake effect from the text
func remove_shake_effect():
    for effect in text_line.get_effects():

Working with custom effects can be complex, so it’s important to test the implementation and ensure that it behaves as expected.

Adjusting Text Transparency
Adjusting the transparency of text can be ideal for fade-in and fade-out animations. Here’s how you would make text semi-transparent:

# Change the alpha value of the text color to 0.5 for 50% transparency
var transparent_color = text_line.get_color()
transparent_color.a = 0.5

Animating this alpha value over time creates a smooth fade-in or fade-out effect.

Expanding on the versatility of the TextLine class within Godot 4, these examples showcase how text can be transformed and manipulated to engage users actively. Whether it’s shaking text during a dramatic moment in a game or re-styling text to reflect achievements, these tools provide the means to enhance storytelling and user interaction within your project. Remember to always thoroughly test your changes to ensure they have the desired effect and maintain readability. Keep experimenting and exploring the potential combinations to discover what works best for your specific application or game!

Continue Your Game Development Journey with Zenva

Diving into Godot 4 and mastering the TextLine class is just the beginning. If you’ve enjoyed this tutorial and are eager to expand your game development skills, our Godot Game Development Mini-Degree is the perfect next step. This comprehensive program will guide you through building various types of games from the ground up, employing both 2D and 3D assets and giving you hands-on experience with game mechanics across different genres. Whether you’re a beginner or looking to solidify your existing skills, our curriculum is designed to be flexible and adapt to your learning pace while delivering the practical know-how you need.

For those who are looking for an even broader spectrum of Godot content, we invite you to explore our full range of Godot courses. These resources are perfect for developing a strong portfolio and advancing your career in game development. At Zenva, we’re committed to providing you with the high-quality content needed to transition from beginner to professional confidently. Keep creating, keep coding, and make your game development dreams a reality with Zenva Academy.


Text manipulation in Godot 4 through the TextLine class is more than just a technical skill; it’s a form of art that brings your user interface and storytelling to life. As we’ve seen, the possibilities are as limitless as your creativity, from stylishly animated titles to intricately interactive narratives. Taking the time to master these details can be what sets your game apart in a crowded marketplace. So we encourage you to experiment, apply what you’ve learned, and watch as your games transform with each stroke of text.

Don’t stop here. Embrace the full suite of skills that game development has to offer by joining us at Zenva Academy. Our Godot Game Development Mini-Degree is designed to build upon what you’ve learned today and so much more. Whether you aspire to make your own games or contribute to exciting projects, we’re here to support your journey every step of the way. Turn your passion into achievement, and let’s create something incredible together!

Python Blog Image

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