I’m having an issue where I’ve made a system that relies on dynamically getting the lengths of animation nodes in an animation tree. I’ve made some code that finds nodes based on a naming standard I’ve set, and then reads the value of their “current_length” variable.
An example would be something like this: start_anim_length = animation_tree["parameters/move_cycle/0/idle/current_length"]
My issue is that reading this in the scripts _ready() function always returns 0, even when I do await get_tree().process_frame, but it does return the correct value when I read it in the scripts _process() instead. So it seems the AnimationTree BlendTree root node only contains accurate values for its nodes after _ready() functions, but before _process() functions.
I’m looking for a way to circumvent this that doesn’t require me to have a bool and if check at the start of the _process function that makes sure init code is run only on the first _process() frame. It works, but it’s an ugly solution and I want a better one. Preferrably there’s some thing else I can await, something like await animation_tree.initialize_tree or something, so I can just delay the execution of the _ready() function until the AnimationTree is ready for it, the same way I tried to do with await get_tree().process_frame.
I also want to avoid a timer based await-approach, as it’s also ugly and kind of unreliable.
I’ve figured out that this is because AnimationTree bases its information on the previously processed frame, so for the values to be accurate I need at least one frame to have been processed.
Is there something similar to get_tree().process_frame that is triggered after _process, instead of before _process? Or some way to await next frame?
I actually just tried this on a whim, and it works. Came here to update the thread to say I’ve found a fix, but you beat me to it!
It does exactly what I need. I’d preferred if there was a “prettier” solution, but that’s mostly just aesthetics at this point, as I guess functionally this does the same thing as what I wanted to achieve with one await.
edit: oh wait, you’re not using state_machines, nevermind…
Well, another way would be to get the animation_node from the state_machine based on the node’s name, then you can get the name of the node’s animation, then the actual animation from your animation player and it’s length. I do something similar and it works for me in _ready, with no waiting:
var node: AnimationNode = state_machine.get_node(your_nodes_name)
var animation_name: String = node.animation
var animation: Animation = your_animation_player.get_animation(animation_name)
var length: float = animation.length