Animation player not working, how fix?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Onulator

I’ve been looking into this for about a week or two now, and I still have no idea what’s going wrong. Basically, I’ve set up the animation player with 3 animations to activate on certain button presses in the process function, but when opening the debugger, it doesn’t work. And yet if I set it to loop and have it start up immediately in the ready function, it works properly. No matter what I do, what tutorial I look up, anything, I can’t get it to work, even though I’m pretty sure I’m using the correct code.

Am I just missing something or what?

extends KinematicBody2D

onready var P1_Animations = $P1_Animations

var isInCombo = false

var timeTillNextInput = 0.5
var time = 0

var currentAttack = 0
var previousAttack = 0
#-------------------------------------------------------------------------------
func _ready(): #Loads on startup
    time = timeTillNextInput
#-------------------------------------------------------------------------------
func _proccess(delta): #Game checks all of this every frame
    if(Input.is_action_just_pressed("P1_Attack1")):
	    if(currentAttack == 0):
		    P1_Animations.play("N-Attack1-1")
	    elif (currentAttack == 1):
		    P1_Animations.play("N-Attack1-2")
	
	    isInCombo = true
	    currentAttack += 1
    if(isInCombo):
	    time -= delta
	
	    if(time < 0):
		    time = timeTillNextInput
		    isInCombo = false
		    currentAttack = 0
		    P1_Animations.play("RESET")

    if Input.is_action_pressed("ui_right"):
	    $P1_Animations.play("N-Attack1-1")
    else:
	    $P1_Animations.stop()
#-------------------------------------------------------------------------------
:bust_in_silhouette: Reply From: Inces

You can’t force animationplayer to play in process(), because it starts animation from first frame once every process frame ! Your animationplayer works, but is frozen in frame 0. You need to design a way to activate play() once, just like ready() function is activated once

Any advice on how to do that? I’m very new to Godot and would need more experience to figure out how to do that on my own.

Onulator | 2022-04-15 17:44

:bust_in_silhouette: Reply From: Mohammad_Hosein

I think the problem is on your script attachment… looks like you attach the script to KinematicBody2D (beacause first line is extends KinematicBody2D)
so if the AnimationPlayer Node(which you name it P1_Animations) is not child node of KinematicBody2D then You can’t access to it by $P1_Animations and all of the P1_Animations.play('...') will not work.

:bust_in_silhouette: Reply From: angstyloop

This was a helpful thread

So I was having a similar Issue: Im calling my animations with custom functions in player script that plays the animations but my animations are not playing properly while character is moving.

The Problem: When holding movement key set to process, process is constantly resetting the animation every frame…

Solution: Add a Timer that delays the Animation reset…
so Once the key is held it plays animation then add a timer that only allows it to reset every 2-5 seconds or so

Haven’t tried this yet but I believe that is the solution.
Ill let ya know how it goes

Throwing my hat in the ring here:

You can just add a state flag to see if the timer is running, if it is don’t initialize, if it’s not running, set state flag to running and then initialize the timer.

I just wish it wasn’t so easy to break code lol