How to tell when a space is being drawn into a RichTextEdit

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

Hi all,

I am loading data into a RichTextEdit like so:

set_bbcode(dialog[page])

(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:

set_visible_characters(0)

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

set_visible_characters(get_visible_characters()+1)

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…

:bust_in_silhouette: Reply From: quijipixel

You could do something like this:

var pos = get_visible_characters()
if dialog[page].length() > pos and dialog[page][pos] == ' ':
    print("Space!!!")

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

Parser Error: Identifier not found: pos

:slight_smile:

Robster | 2017-08-24 00:41

:open_mouth:
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()] == ' ':
    print("Space!!!")

quijipixel | 2017-08-24 00:46

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 :slight_smile:

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 :slight_smile:

Am I misinterpreting get_visible_characters()?

Robster | 2017-08-24 01:08

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:

set_visible_characters(get_visible_characters()+1)

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()] == ' ':
    print("Space!!!")

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.

quijipixel | 2017-08-24 18:51