How to get an animation into an add_child() AnimationPlayer

Godot Version

4.3

Question

im trying to make a custom resource pickup that i can easily change what kind of pickup it is just with an exported var, but to do this i need to learn how to add theoretical children to a node so that i can get an animated sprite and other useful things.

im kinda stuck on adding animations to the theoretical animationPlayer, can you save animations as files or do you have to manualy change the frames or something? as always, any help is apreciated, thanks!

this is as far as i’ve gotten in the pickup code:

extends Area2D
class_name MyPickup


@export var pickup_type = 0
# 0 = extra wall jump

var wall_jump_pickup_texture = preload("res://textures/wall jump pickup sheet.png")

func _init() -> void:
	collision_layer = 8
	collision_mask = 0
	var animation_player = AnimationPlayer.new()
	var animation_sprite = Sprite2D.new()
	var deactivation_timer = Timer.new()
	add_child(animation_player)
	add_child(animation_sprite)
	add_child(deactivation_timer)
	
	animation_player.root_node = animation_sprite
	
	if pickup_type == 0:
		animation_sprite.texture = wall_jump_pickup_texture
		animation_sprite.vframes = 2
		animation_sprite.hframes = 2

func _deactivate():
	collision_layer = 0

func _on_deactivation_timer_timeout() -> void:
	collision_layer = 8

Each AnimationPlayer has a dictionary filled with AnimationLibrary values, and each of those is a dictionary with Animation values. Since both Animation and AnimationLibrary are resources, you should be able to save them as .tres or .res files.

For getting them into or out of an AnimationPlayer, you should take at look at the functions it inherits from the AnimationMixer class.

1 Like

alright, thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.