How to combine this code

Godot Version

4.7.2

Question

I wanted to combine both the animation tree and the state machine, and I wanted to do that without using Blendshape2d

but it got in the way and lost confidence and I don’t know how

	
	match State:
		State.idle: _state_idle()
		State.walk: _state_walk()
		State.jump: _state_jump()
		State.fall: _state_fall()
		State.land: _state_land(_delta)
		State.death: _state_death(_delta)
		
		
		# space for animation tree
		
		
		


func _state_idle() -> void:	
	velocity.x = move_toward(velocity.x, 0.0, speed)
	
	#state change
	var _direction: float = Input.get_axis("ui_left", "ui_right")
	if _direction != 0.0:
		_change_state(State.walk)
	elif Input.is_action_just_pressed("jump") and is_on_floor():
		_change_state(State.jump)
	elif not is_on_floor():
		_change_state(State.fall)
		
func _state_walk() -> void:
	var _direction: float = Input.get_axis("ui_left", "ui_right")
	velocity.x = _direction * speed 



	if _direction < 0.0:
		bodyvis.scale.x = -1.0
	elif _direction > 0.0:
		bodyvis.scale.x = 1.0

	#transitions
	if _direction == 0.0:
		_change_state(State.idle)
	elif  Input.is_action_just_pressed("jump") and is_on_floor():
		_change_state(State.jump)
	elif not is_on_floor():
		_change_state(State.fall)

func _state_jump() -> void:
	velocity.y = jump_velocity
	_change_state(State.fall)

func _state_fall() -> void:
	var _direction: float = Input.get_axis("ui_left", "ui_right")
	velocity.x = _direction * speed
	
	
	#sprite flip
	if _direction < 0.0:
		bodyvis.scale.x = -1.0
	elif _direction > 0.0:
		bodyvis.scale.x = 1.0
	
		
	if is_on_floor():
		_change_state(State.land)
		
		
		
func _state_land(delta: float) -> void:
	velocity.x = move_toward(velocity.x, 0.0, speed * 3.0)
	land_timer -= delta
	
	if land_timer <= 0.0:
		var _direction: float = Input.get_axis("ui_left", "ui_right")
		if _direction != 0.0:
			_change_state(State.walk)
		else:
			_change_state(State.idle)

func _state_death(delta: float) -> void:
	velocity.x = move_toward(velocity.x, 0.0, speed * 3.0)
	death_timer -= delta
	
	
	if death_timer <= 0.0:
		global_position = respawn_pos
		velocity = Vector2.ZERO
		_change_state(State.idle)

#helpers
func _change_state(new_state: State) -> void:
	#don't reenter the same state
	if new_state == current_State:
		return
	
	
	
	#entry actions
	match new_state:
		State.land:
			land_timer = land_duration
		State.death:
			death_timer = death_duration
		
		
	current_State = new_state


	

Where your code says this, insert a reference to your animation tree. I recommend giving it a Unique Identifier in the Scene tab.

Then within your _change_state() function call the animation you want.

The Godot docs say that something like this should do the tricks:

# <-- here use your unique idenfiier -- it's more future proof
var state_machine = $AnimationTree.get("parameters/playback")
# <- insert your animation states here, depending on your other state machine. 
state_machine.travel('walk')

The state-machine approach is a good fit when the states are discrete. I’d use one AnimationTree connected to the AnimationPlayer, make its root an AnimationNodeStateMachine, and put the individual clips or sub-state machines inside it. Then obtain the playback object once and call travel() from your gameplay state changes; avoid trying to drive the same AnimationPlayer directly at the same time. Keep transitions responsible for switching states, and use a Blend2 or BlendSpace1D only where you actually need continuous blending. Also make sure the tree is active and its anim_player path points at the correct node. A small diagram of the desired states first usually makes the code much simpler.