0 votes

Hi all,

I am loading data into a RichTextEdit like so:


(dialog[page] is simply text data from an array. It could contain something like "Hi I'm having a great day!".

I'm then hiding all characters so we can reveal them later:


Then later in a loop I'm doing this kind of thing to reveal one character at a time:


So it's really nice, it's linked to a timer timOut signal and prints out the text like a typewriter.

What I need though, is to know when that character is a SPACE. I can't for the life of me figure it out. I'm pretty new at all of this stuff so any advice would be appreciated.

Thanks so much...

in Engine by (828 points)

1 Answer

0 votes

You could do something like this:

var pos = get_visible_characters()
if dialog[page].length() > pos and dialog[page][pos] == ' ':
by (697 points)

You had me excited there for a quick minute... then:

Parser Error: Identifier not found: pos


Don't forget to declare the pos variable! If you don't like it that much, use the more straightforward approach:

if dialog[page].length() > get_visible_characters() and dialog[page][get_visible_characters()] == ' ':

Thank you.

OK so get_visible_characters() is the current character?

It does work and I am very much appreciative. Just trying to get my head around it. I would have thought get_visible_characters() would return an integer of how many characters their are.

Let me think out loud in public :-)

This appears to be what's happening:

If the length of the dialog string is greater than the length of the characters in the RichTextEdit AND the length of the dialog equals space, then print.

I am fully stumped on that one :-)

Am I misinterpreting get_visible_characters()?

I think you are missing something here. get_visible_characters() doesn't return the length of the characters. It returns the length of the characters that are visible. This is important! when you run the code, the function set_visible_characters(0) is making the RichTextLabel node show from character 0 to character 0 as visible. This means that even though the RichTextLabel node has the full string, it will show nothing. Now, everytime the timer is activated, it will increase the visible character length like this:


You are increasing the range of visible characters from 0 to n - 1 each time the timer activates. So, keeping this in mind I'll explain the code that I did:

if dialog[page].length() > get_visible_characters() and dialog[page][get_visible_characters()] == ' ':

The If statement here has 2 parts, one that checks that the visible characters quantity in the RichTextLabel isn't bigger that the dialogs string. Now, if you remove that part of the code and run it, you'll notice that Godot breaks when the text is completaly shown. This is because the visible characters don't represent the lenght of the dialog string, hence the value in the function might be bigger than the real size of that string, thus breaking in runtime.

So the first part of the if statement translates to If the current amount of characters visible in the RichTextLabel is lower than the length of the dialog[page] string. This prevents Godot from breaking when the text is finished appearing because string and array positions are accessed from 0 to length - 1, and functions like size() and get_visible_characters() return the length value, not length - 1.

The second part of the if statement just checks that the last character of the visible characters in the RichTextLabel is a space using the dialog string. To better understand this just remember that the RichTextLabel has his own copy of the string, and that is showing characters in a range from 0 to get_visible_characters() - 1. Because the strings are the same you can check if the last visible character is a space character in the dialog[page] string.

So the second part of the if statement translates to: If the last shown character in the RichTextLabel equals a space character

I think I wrote to much, tell me if it makes sense.

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 Frequently asked questions and 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 [email protected] with your username.