Type character from ascii code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jospic
:warning: Old Version Published before Godot 3 was released.

Hi,
is there anyway to show a character from ascii code in a label string object?
Thanks in advance
-j

:bust_in_silhouette: Reply From: Bojidar Marinov

This question has two parts – getting a character from an ASCII code, and putting it into a text object.


For the first part, searching for ascii results in String::to_ascii and RawArray::get_string_from_ascii. Obviously, the first one won’t help, so we might as well use the second method. We just need a RawArray

var ascii_code = 56

var conversion_raw_array = RawArray()
conversion_raw_array.append(ascii_code)
# Or simply:
var conversion_raw_array = RawArray([ascii_code])

var character = conversion_raw_array.get_string_from_ascii()
# Or, shorter:
var character = RawArray([ascii_code]).get_string_from_ascii()

The next part is setting the label’s text. (Probably you know how to do it, but anyway…) Searching again, this time using Ctrl+f on the Label help page, for text, we eventually get to Label::set_text. It is pretty easy to use, and from the docs we learn that it has a single String parameter. Note that since RawArray::get_string_from_ascii returns String as well, we can just use its output from above.

get_node("path/to/the/Label").set_text(character)
# Or, if you have a non-string you want to display
get_node("path/to/the/Label").set_text(str(ascii_code))

Note that it might be more performant if you hardcode all ascii code → character mappings in a Dictionary, even though it feels hackish.

Bojidar Marinov | 2016-06-27 14:44