How to add sounds to programmatically generated amimation

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

Hi there,

I’m building an animation that simulates the typing out of text on a computer terminal in our game. The animation of the text appearing works great, but I can’t figure out the syntax for adding the sound. I have a SamplePlayer called “player” with a sample loaded called “click_x” that I am trying to have play each time a char is shown in the animation. Basically the following function attempts to build the animation player with sound, for any text I might need to display, and return it at the end.

Here is my code:

func build_anim(start_char=0,end_char=10,time_step=.1):
	var typer_player = AnimationPlayer.new()
	add_child( typer_player )
	var typer_anim = Animation.new()
	typer_player.add_animation( "typer_anim", typer_anim )
	typer_player.set_current_animation( "typer_anim" )
	typer_anim.add_track( 0 )
	typer_anim.add_track( 1 )
	typer_anim.add_track( 2 )
	var time_needed = ((end_char - start_char) * time_step)
	print("total time for this animation: " + str(time_needed))
	typer_anim.set_length(time_needed)
	typer_anim.track_set_path( 0, "screen:visible_characters" )
	typer_anim.track_set_path( 1, "player:play/play()" )
	typer_anim.track_set_path( 2, "AM:playback/play" )
	# run the loop to build the animaton
	var n=start_char
	var t=0
	while (n < end_char):
		typer_anim.track_insert_key(0, t, n )
		typer_anim.track_insert_key(1, t,  1) # "click_x" | 
		typer_anim.track_insert_key(2, t, "click")
		#print("added key at time : " + str(t) + " with value: " + str(n)) 
		n = n+1
		t = t+time_step
	#typer_player.play( "typer_anim" ) 
	return typer_player

Thanks in advance for any assistance.

At least two things that might be problems.

The first parameter of add_track() is a type, not an index number. The type 0 is value, 1 is transforms, and 2 is method calls.

Also you probably don’t need the parenthesis for the player:play/play(). I think it would just be player:play/play.

avencherus | 2018-01-10 04:41

Thanks so much for the reply. That’s very helpful. Regarding “player:play:play()” – the open close parenthesis at the end was nuts, but I was just sort of trying everything,

A couple follow ups, if you have the time:

– If the index of a track is not established at the time of calling “add_track” how would I go about finding the correct index to use for a track once I am ready to call .track_set_path?

– Ideally, I’d just call a method and dispense with scripting the player (type 2 of add track, as you mention) to make the sound. What would the syntax be to simply call a method “makenoise()” which lives in an auto-loaded file called “global.gd”?

Thanks again for the quick reply. Feel free to just point me to another doc or resource if the answers to these questions are already online.

woody-leaderless | 2018-01-10 18:31

Quick update: I got this working, using the following code:

func build_anim(start_char=0,end_char=10,time_step=.1):
	var typer_player = AnimationPlayer.new()
	add_child( typer_player )
	var typer_anim = Animation.new()
	typer_player.add_animation( "typer_anim", typer_anim )
	typer_player.set_current_animation( "typer_anim" )
	typer_anim.add_track( 0 )
	typer_anim.add_track( 0 )
	var time_needed = ((end_char - start_char) * time_step)
	print("total time for this animation: " + str(time_needed))
	typer_anim.set_length(time_needed)
	typer_anim.track_set_path( 0, "screen:visible_characters" )
	typer_anim.track_set_path( 1, "player:play/play" )
	# run the loop to build the animaton
	var n=start_char
	var t=0
	while (n < end_char):
		typer_anim.track_insert_key(0, t, n )
		typer_anim.track_insert_key(1, t, "click_x")
		n = n+1
		t = t+time_step
	#typer_player.play( "typer_anim" ) 
	return typer_player

…questions above still are things I’m curious about, but a huge thanks to @avencherus for the help!

woody-leaderless | 2018-01-10 18:55

You’re welcome.

From the docs add_track() returns an integer. Animation — Godot Engine (stable) documentation in English

It doesn’t seem to say so, but I believe that is the track index.

avencherus | 2018-01-11 07:58