I'm currently trying to dynamically create an animation and playing it to show text based on the length of the text and the set speed of characters per second.
What I'm trying to recreate in code is this animation:
So an animation with a property track on the label's property visible_characters
with update mode on continuous and interpolation mode on linear
The script behind the DialogBox node:
extends Control
export(String, MULTILINE) var Text = ""
export(int) var CharactersPerSecond = 100
func _ready():
$Panel/Label.set_text(Text)
print($Panel/Label.get_text())
createAnimation()
$AnimationPlayer.play("show-text")
print("is playing " + str($AnimationPlayer.is_playing()))
print("current animation " + $AnimationPlayer.current_animation)
func createAnimation():
var animationLength = Text.length() / (CharactersPerSecond as float)
print(animationLength)
var animation = $AnimationPlayer.get_animation("show-text")
animation.clear()
var trackIdx = animation.add_track(Animation.TYPE_VALUE)
animation.track_set_path(trackIdx, "Panel/Label:visible_characters")
animation.track_set_interpolation_type(trackIdx,Animation.INTERPOLATION_LINEAR)
animation.value_track_set_update_mode(trackIdx, Animation.UPDATE_CONTINUOUS)
animation.track_insert_key(trackIdx, 0, 0)
animation.track_insert_key(trackIdx, animationLength, Text.length())
For testing purposes the text is being set in the editor using the exported Text
variable and is some lorem ipsum.
What happens when I run the scene is that the Panel and Label get shown but no text is shown in the Label, it stays empty, but according the print statements the show-text
animation is playing
The printed data in the output window is:
** Debug Process Started **
Godot Engine v3.1.2.stable.mono.official - https://godotengine.org
OpenGL ES 3.0 Renderer: AMD Radeon R7 200 Series
label text: Magnam consequatur vel alias earum accusantium. Nobis voluptatem voluptatem quaerat adipisci voluptas. Numquam id error earum consectetur veniam. Quaerat quibusdam quas sunt alias et blanditiis corporis. Cupiditate rem ut natus est molestiae quidem. Magnam consequatur vel alias earum accusantium. Nobis voluptatem voluptatem quaerat adipisci voluptas. Numquam id error earum consectetur veniam. Quaerat quibusdam quas sunt alias et blanditiis
animationlength: 4.44
is playing: True
current animation: show-text
** Debug Process Stopped **
so the problem was that i still needed to set the length of the animation
just inserting the keys wasn't enough
adding the following line after animation.clear()
fixed it:
animation.set_length(animationLength)