Godot 4: How to enter unicode character into RichTextLabel via character code?

120 Views Asked by At

I'm using a RichTextLabel in Godot 4 and I'm trying to insert a special unicode character into it (I'm using fontawesome). I know the unicode index of said character. How do I add it to the label via that index?

I tried:

  • \uf6e3
  • \Uf6e3
  • u+f6e3
  • U+f6e3
  • [unicode]f6e3[/unicode]

All of the above just print the control sequence as-is, they don't print the unicode character I'm trying to refer to.

The only way that works is to literally copy the unicode character (as text) into my clipboard and pasting it directly into the label text in the editor. This works, but isn't very usable because the editor itself cannot display that character. I would much rather have the control sequence visibile in the editor, and the actual character in-game.

1

There are 1 best solutions below

0
Felix On

Unfortunately Godot cannot display this directly, I have written you a small workaroud script that replaces your text with the unicode characters in real time. I hope this helps you :)

@tool
extends RichTextLabel


@export_multiline var UnicodeText: String = "":
    set(value):
        var updated_value = replace_unicode_sequences(value)
        self.text = updated_value
        notify_property_list_changed()
    get:
        return text


func replace_unicode_sequences(value: String) -> String:
    var result = value
    var regex = RegEx.new()
    regex.compile("\\\\u([0-9a-fA-F]{4})")
    var matches = regex.search_all(value)
    for match in matches:
        
        
        var codepoint = int("0x" + match.get_string(1))
        var unicode_char = char(codepoint)
        result = result.replace(match.get_string(0), unicode_char)
    
    return result