BBCode in Godot: Styling Text Like a Pro | Codabase

BBCode in Godot: Styling Text Like a Pro

7 Min Read

Godot Engine, a popular open-source game engine, provides a powerful tool for creating rich text with BBCode. BBCode (Bulletin Board Code) is a lightweight markup language used to format text in online forums, websites, and applications. In this article, we’ll dive deep into using BBCode in Godot, explore its features, and provide code examples to help you create engaging and visually appealing in-game text.

Using BBCode in Godot’s RichTextLabel

To use BBCode in Godot, you’ll need to work with the RichTextLabel node. RichTextLabel is a versatile UI node that supports rich text formatting using BBCode. To get started, add a RichTextLabel node to your scene and enable the “bbcode_enabled” property in the Inspector. Now, you can write your formatted text using BBCode syntax directly in the “text” property or via GDScript.


var rich_text_label = RichTextLabel.new()
rich_text_label.bbcode_enabled = true
rich_text_label.bbcode_text = "[b]Hello, world![/b]"

Basic BBCode Tags in Godot

Godot supports a variety of BBCode tags, allowing you to format text in multiple ways. Here are some basic BBCode tags and their effects:

  • Bold: Surround your text with [b] and [/b] tags to make it bold.
  • Italic: Use [i] and [/i] tags to italicize your text.
  • Underline: Add [u] and [/u] tags to underline your text.
  • Strikethrough: Use [s] and [/s] tags to apply a strikethrough effect.
  • Color: Change the text color with [color=#RRGGBB] and [/color] tags, where #RRGGBB is the desired color in hexadecimal format.
  • Font size: Adjust the font size using [size=N] and [/size] tags, where N is the desired font size.

Here’s an example of using these basic BBCode tags in Godot:


rich_text_label.bbcode_text = "[b]Bold[/b] [i]Italic[/i] [u]Underline[/u] [s]Strikethrough[/s] [color=#FF0000]Red[/color] [size=24]Large Text[/size]"

Advanced BBCode Features in Godot

Godot also supports more advanced BBCode features, such as custom fonts, images, and even animations. Let’s explore some of these advanced features:

  • Custom font: To use a custom font, add a DynamicFont resource to your project, then use the [font=res://path/to/font.tres] and [/font] tags to apply it to your text. Replace “res://path/to/font.tres” with the actual path to your font resource.
  • Images: Embed images in your text using [img=res://path/to/image.png] tag, replacing “res://path/to/image.png” with the path to your image file.
  • Animations: Add animations to your text using the [anim=AnimationPlayerName:AnimationName] tag, where “AnimationPlayerName” is the name of the AnimationPlayer node in your scene, and “AnimationName” is the name of the desired animation.
  • Links: Create clickable links using the [url=res://path/to/scene.tscn] and [/url] tags, replacing “res://path/to/scene.tscn” with the path to your target scene or resource.

Here’s an example demonstrating the use of these advanced BBCode features in Godot:


rich_text_label.bbcode_text = "[font=res://path/to/font.tres]Custom Font[/font] [img=res://path/to/image.png] [anim=MyAnimationPlayer:MyAnimation]Animated Text[/anim] [url=res://path/to/scene.tscn]Link to Scene[/url]"

Godot’s RichTextLabel node can emit a “meta_clicked” signal when a user clicks on a BBCode link. To handle these click events, connect the “meta_clicked” signal to a function in your script. The function will receive the clicked metadata as an argument, which can be used to take appropriate actions.


func _ready():
    rich_text_label.connect("meta_clicked", self, "_on_meta_clicked")

func _on_meta_clicked(meta):
    print("Clicked on:", meta)
    # Perform actions based on the clicked metadata

Creating Custom BBCode Tags in Godot

While Godot provides a wide range of BBCode tags out of the box, you may want to create custom tags to suit your specific needs. To create custom BBCode tags, you can extend the RichTextLabel class and override the “_process_custom_fx” function. This function will be called for each custom tag, allowing you to apply your custom effects.


class_name CustomRichTextLabel
extends RichTextLabel

func _process_custom_fx(char_fx):
    if char_fx.bbcode == "custom":
        # Apply your custom effect to the character
        char_fx.color = Color(0, 1, 0, 1)  # Example: Change the text color to green
        return true

    return false

With your custom RichTextLabel class, you can now use your custom BBCode tag in your project:


var custom_rich_text_label = CustomRichTextLabel.new()
custom_rich_text_label.bbcode_enabled = true
custom_rich_text_label.bbcode_text = "This is a [custom]custom effect[/custom] using BBCode in Godot."

In conclusion, BBCode in Godot offers an effective way to create rich text for your games. By leveraging the power of RichTextLabel and BBCode, you can create engaging text elements that enhance the player experience. To further expand your Godot skills, be sure to explore our article on mastering Godot Tilemaps for 2D game worlds.

Frequently Asked Questions

  1. How to change color with BBCode: To change the color of text using BBCode, you can use the “color” tag. Wrap the text you want to change the color of within the color tags, and specify the desired color using a standard color name or a hex code. Here’s an example:
    [color=red]This text is red[/color]
    [color=#008000]This text is green[/color]
  2. How do I use BBCode in RichTextLabel (Godot)?
    In Godot, you can use BBCode with the RichTextLabel node to style and format your text. First, make sure the “Bb Code” property of the RichTextLabel node is enabled in the Inspector. Then, you can set the text of the RichTextLabel node using BBCode, like this:

    var richTextLabel = $RichTextLabel
    richTextLabel.bbcode_text = "[color=red]This text is red[/color]"
    
    
  3. Typewriter effect that uses BBCode (Godot):
    To create a typewriter effect using BBCode in a RichTextLabel node in Godot, you can use the following GDScript code:

    
    extends RichTextLabel
    
    var text: String = "[color=red]This is a typewriter effect with BBCode[/color]"
    var timer: float = 0.1
    var index: int = 0
    
    func _ready():
        bbcode_enabled = true
        set_process(true)
    
    func _process(delta):
        if index <= len(text):
            timer -= delta
            if timer <= 0:
                bbcode_text = text.substr(0, index)
                index += 1
                timer = 0.1
    

    This code creates a typewriter effect by appending one character at a time to the RichTextLabel’s BBCode text.

  4. Any good Custom RichTextLabel BBCode effects out there?
    For custom BBCode effects in RichTextLabel, you might need to create your own custom node by extending the RichTextLabel node and implementing the desired functionality. You can check out the Godot forums, GitHub, or Reddit for user-created custom BBCode effects or ask the Godot community for recommendations. However, keep in mind that custom BBCode effects might not be widely available, as many developers tend to create their own solutions based on their specific needs.

Share this Article
Leave a comment