[PROBLEM] Coding an Animation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JCNegar

Hello there,

I’ve been trying to make a DialogSystem, I am trying to animate the text’s percent visible property through code and that’s what I came up with:

extends Node2D
onready var animationPlayer = $AnimationPlayer
onready var dialog = $DialogBox/Background/Text
var i = 0

func _process(delta):
	if Input.is_action_just_pressed("space"):
		dialog.text = "It was a big big world"

func animate_text():
	# Create the animation resource for the text
	var animation = Animation.new()
	var track_index = animation.add_track(Animation.TYPE_VALUE)
	animation.track_set_path(track_index, "DialogBox/Background/Text:percent_visible")
	animation.track_insert_key(track_index, 0, 0)
	animation.track_insert_key(track_index, 5, 1)
	# Attach the animation resource to the AnimationPlayer
	animationPlayer.add_animation("animate_text", animation)

I want to show the text progressively word by word, However, It doesn’t seem to work at all. Am I doing something wrong?

:bust_in_silhouette: Reply From: w411-3

First of all, it would help a lot more if you described in what way it doesn’t work. Is it doing anything at all?

Even though what you’re doing “sounds” like animation, I don’t think you actually want to use the animation player. Instead maybe your function should split the text it is going to animate by space (or whatever delimiter you want, maybe periods for whole sentences):

var split_text = text_to_show.split(" ")

and then just modify the actual dialog text periodically to reveal more and more of the text (set the text property to bigger chunks of the full text). Maybe based on time in the _process method or something. So don’t just set the dialog text property to the full text either, save it for later.

Thanks for the reply. I also figured to use set_visible_characters() with a timer and now it works fine.

JCNegar | 2022-03-23 17:02