![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Rickers |
I am trying to write a way of displaying text which is able to pause (briefly) when certain characters are detected (e.g. pause for a second when you reach a full stop).
However, for some reason I cannot get the pauses to match up with the characters when it gets round to actually displaying text.
It seems that the get_total_character_count() is different from the actual length of the string given (with added bbcode), and somewhere in this mix the visible character’s variable is thrown off.
What this means is that when i add a string like “[center] This is a line of text, I think! And this is another poorly punctuated line, right?[/center]” instead of getting the pauses at the commas or question/exclamation marks it is at some offset.
If you see in the code below, it shouldn’t be so difficult, I pass a variable which i think will return the current character being displayed, and if that character is a piece of punctuation, pause the char_timer and wait until the pause_timer gives its timeout() signal.
However, for some reason it wont work consistently when bbcode is added like in the above example.
I have tried (and partially found) some solutions.
If I try to just use a regular text label with the following code, all the offsets are still off, despite there being no bbcode even added to the string. (this is something to do with get_total_characters in Label not including whitespace for some reason).
I can also use the dialogue[count].length()-8 or -9 to provide the offset needed to get the proper pauses, but this isn’t a great option as it won’t let me use bbcode as i’d fully like and i’d have to add the [center][/center] tags to every line of text which just seems kind of hacky and not a good practice to rely on when i’m having a non-coder provide some dialogue in future.
Here is the code now:
(forgive the shoddy code if its off by 1 in some way as I have tried to reconstruct here without all the spaghetti debugging stuff and its been through many alternations and iterations),
extends RichTextLabel
var dialogue
var count = 0
onready var char_timer = $char_timer
onready var pause_timer = $pause_timer
func _ready():
bbcode_text = dialogue[count]
visible_characters = 0
func _process(_delta):
rect_scale.x = abs(rect_scale.x)
func _on_char_timer_timeout():
if visible_characters < get_total_character_count():
var curr_character
if get_visible_characters() >=get_total_character_count():
curr_character = ""
else:
curr_character = dialogue[count][get_visible_characters()+8]
visible_characters += 1
pause(curr_character)
else:
count += 1
if count < dialogue.size():
visible_characters = 0
bbcode_text = dialogue[count]
else:
char_timer.stop()
func pause(character):
if character == ".":
char_timer.stop()
print("At this bit: "+character)
pause_timer.wait_time = 0.8
pause_timer.start()
yield(pause_timer,"timeout")
char_timer.start()
elif character == "!" or character == "?":
char_timer.stop()
print("At this bit: "+character)
pause_timer.wait_time = 1.2
pause_timer.start()
yield(pause_timer,"timeout")
char_timer.start()
elif character == ",":
char_timer.stop()
print("At this bit: "+character)
pause_timer.wait_time = 0.4
pause_timer.start()
yield(pause_timer,"timeout")
char_timer.start()
Have I found some weird bug or am I just missing something? Thanks.