Mkay, im trying to set the speed of an animation to the value of the inputs get_action_strength. this is in a state machine, this being the code for the walk state. it works as expected, but trying to add the animation speed, and things start to fall down!

at the moment it gives an error... Attempt to call function 'get_move_direction' in base 'null instance' on a null instance. to this line...

var animation_speed: float = move.get_move_direction().x same error using get_parent().get_move_direction().x

...which means its a wrong node path doesnt it? Or spelling mistake etc. which is odd, as this is called succesfully later in the code.

Entering the state, the animation is played, so thats the place to start playing with animation speed I think.

Heres the code for the state....

extends State

var move: = get_parent()
var animation_speed: float = move.get_move_direction().x


# interface for the state
func  unhandled_input(event: InputEvent) -> void:
	get_parent().unhandled_input(event)


func physics_process(delta: float) -> void:
	var move: = get_parent()
	# transitions
	# 1 if input movement stops, go idle
	if owner.is_on_floor():
		if move.get_move_direction().x == 0.0:
			_state_machine.transition_to("Move/Idle")
	else:
		_state_machine.transition_to("Move/Air")
		
	if move.get_move_direction().x > 0.0:
		owner.get_node("AnimatedSprite").flip_h = false

	if move.get_move_direction().x < 0.0:
		owner.get_node("AnimatedSprite").flip_h = true	
	
	# delegate motion to the move state...
	move.physics_process(delta)


func enter(msg: Dictionary = {}) -> void:
	# pass the message to the parent move state...
	get_parent().enter(msg)

	print(animation_speed)
	owner.get_node("AnimationPlayer").play("Run").playback_speed(animation_speed)

	# owner.get_node("AnimationPlayer").play("Run")

	
func exit() -> void:
	get_parent().exit()

How do I go about setting the animation speed to the input strength? Im at head scratching stage!!

In lines 3,4 try:

onready var move = get_parent()
onready var animation_speed: float = move.get_move_direction().x

yeah, tried that, then it gives an error on the line

owner.get_node("AnimationPlayer").play("Run").playback_speed(animation_speed)

Invalid call. Nonexistent function 'playback_speed' in base 'Nil'.

Im not 100% sure that lines correct as it is...

That's better. One bug less :)

According to docs AnimationPlayer.play() doesn't return anything, so you can't call a method like that on a non existent object (hence base Nil error). On top of that playback_speed is not a method it's a property. Try:

var aplayer = owner.get_node("AnimationPlayer")
aplayer.play("Run")
aplayer.playback_speed = animation_speed

Yeah, wasnt sure that was right!

Thanks for the help, Its not crashing now. :)

As is, the animation freezes, as its setting the animation speed to zero, I think I only need to put the var animation_speed in its parent state "Move" where the input actions are being handled, and get the result from there. Its now only doing what its told to!

Guess I'd better buy a controller, bit difficult to do a final test on a keyboard as all animation speed results will be 1.0, but should work without!!!

a year later