the animation sprite 2d wierdly restarts while playing it also dosent do that every time or not in the same way
########################## ATTACK FRONT RIGHT #################################
if Input.is_action_just_pressed("attack") and CD == false:
$Timer.start()
$atk.frame = 0
with_plays += 1
if $Timer.time_left > 0:
is_playing_front_R = true
else:
is_playing_front_R = false
if is_playing_front_R == true :
if not $atk.is_playing():
if with_plays == 1:
$atk.play("v1")
else:
$atk.play("v2")
$atk.show()
else:
$atk.hide()
$atk.frame = 0
$atk.stop()
###############################################################################
# #$atk is 25 frames per second and has 6 frames so should be the lenght of 0.24 seconds
# Timer is 0.24 sec
# cooldown timer is 0.6 sec
# CD is timer for cooldown
if $timer_cd.time_left > 0.1:
CD = true
else:
CD = false
I don’t see you anywhere starting the $timer_cd, but you’re checking against it at the end of the code snippet.
Try adding more print() statements throughout your code to understand what is exactly happening when at runtime.
In general, your code seems to be a little overcomplicated. I presume this is being ran in the _process(), while it could be handled in _unhandled_input() for input handling and by connecting to timeout signals of your timers to handle cooldowns.
You can also consider implementing a StateMachine before your code gets too crazy to handle.
yeah i forgot to include the line where i start the cooldown since i have script i incuded before i decided to make an separate line to start the cooldown
if Input.is_action_just_pressed("attack") and CD == false :
$timer_cd.start()
and yeah i also could have used the input functions i also have the timeout functions to stop timer when its done
been thinking about how can i use _unhandled_input() for this and i didint know about it so i had to watch few tutorials and i dont really understand how could it fix the problem with animation since from what i understand it whould only change the
Timers don’t need to be checked every frame - they have a timeout signal you can connect to and react only when the signal is emitted.
You also don’t need these bool variables like CD that you flip when the timer is started and finished, because you can just check Timer::is_stopped() instead.
Similarly to a Timer, AnimationPlayer also has signals you can connect to, like AnimationMixer::animation_finished. So you also don’t need to check every frame if the animation is playing or not, but rather react only when it’s finished playing.
These changes will make your code much easier to follow, and make it much more performant at the same time as you won’t be doing redundant checks every frame.
the issue was the animation being in 25 frames per second instead of 24 the animation in 25 takes 0.24 to finish and in 24 0.25 s to finish so am a bit confused
i still made the version of that code but just using func , _input(event): ; _on_timer_timeout(): ; _on_timer_cd_timeout():
so at least it should be a bit more optimized now
I believe there’s a signal for when an animation finishes that can be connected to the script like a timeout so you wouldn’t need to have a timer that also checks for the end of the animation.