+1 vote

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

in Engine by (1,469 points)

1 Answer

+3 votes
Best answer

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))
by (1,590 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.