Has anyone tried adding scripts to Animationtree statemachine states?

Godot Version

4.6

Question

Hi,

I was messing about with state machines and discovered that i could add an additional AnimationTree with another AnimationPlayer with animations that are all call method tracks lasting 1 or 2 seconds. I decided to switch the AnimTree states and these in turn are used to control the original animation player …

The thing i was wondering about was the fact that the inspector shows that each state can also have its own script, so i added a script and it was just an empty file. The tooltip says its a refcounted and not a type of node, so i was just wondering how i could script the states, can they get parent objects or perhaps recieve signals from the animation player?


Ok so i went and added a script to a basic state, and the wrote the following …

func some_jazz():
	self.

and the intellisense thingy produced a load of functions, including ``connect()`and ``emit_signal()``

so it seems the states could be used to process game logic. The only problem is that they’re (they the states are) “calling up” to emit control signals so it would seem that the animationtree node and a big ``match`` block would be simpler.

Anyway, the next problem is to figure out how to connect the signals from other scripts, or how to acess the script functions of the animation state machine node from the animation tree.

Looks like it isnt a developed API but the note ner the process function suggests there could be some additions in the future.

I can still change states from the AnimationTree node using script, so i could load the states (which are all AnimationRootNode’s) with the function call animations to create a finite state machine, the animations call functions from the AnimationTree and the state machine owner has access to both the logic states and the animation states, which at the moment are not unified but they could be possibly by adding functions call tracks to the normal animations. All logic has to be in the AnimationTree class at the moment.

So i set up a simple animation tree state machine with only one state, a looping animation that does a function call every second and that’s it!

Then i made a CharacterBody2D thats just the Godot icon moving left and right, the system checks the time and sets a boolean var after a certain time period.


func stop_anim():
	print("stopped anim")
	animtree.set("parameters/conditions/pause",true)


func check():
	if vbool:
		stop_anim()

So, in anim_tree the sprite is moving, its stopped in the function `stop_anim` and in anim_tree2 theres a looping animation calling ``check`` every second waiting for the variable vbool to be true.