I'm attempting to have it so that the player switches between two different animation states using an animation tree upon a user input.
Currently I'm using an if statement that checks if the current animation tree node is equal to a string (the name of one of the nodes), traveling to the other node if they are, and traveling to the other if they aren't.
As it is, the transition works only when they aren't equal, so I tried printing whatever get_current_node()
returns to see what I get, but when I did that nothing showed up in the output, and me being new to this, I thought maybe things that return null return nothing when printed, so I simply printed null, and that printed out the word Null just fine.
I'm still very now to godot and have very limited coding experience, so I'm certain there's a better way to approach this, but I still don't understand why get_current_node()
isn't returning anything. I receive no errors in the editor at any point.
This is the code I'm running:
extends KinematicBody2D
export var ACCELERATION = 500
export var MAX_SPEED = 90
export var FRICTION = 350
export var ROLL_SPEED = 500
enum {
MOVE,
DASH
}
var state = MOVE
var velocity = Vector2.ZERO
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get("parameters/playback")
onready var animationPlayer = $AnimationPlayer
onready var currentAnimationState = animationState.get_current_node()
func _ready():
animationTree.active = true
func _physics_process(delta):
match state:
MOVE:
move_state(delta)
DASH:
dash_state(delta)
func move_state(delta):
#determine direction
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_vector = input_vector.normalized()
if input_vector != Vector2.ZERO:
animationTree.set("parameters/IdleBlue/blend_position", input_vector)
animationTree.set("parameters/IdleRed/blend_position", input_vector)
velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
else:
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
move()
if Input.is_action_just_pressed("dash"):
state = DASH
func dash_state(_delta):
#change color state
if currentAnimationState == "IdleRed":
animationState.travel("IdleBlue")
else:
animationState.travel("IdleRed")
state = MOVE
func move():
velocity = move_and_slide(velocity)