0 votes

I've done a fair amount of research on the scripting in this engine and have taught myself a whole bunch of different things.I am currently making my first program which is a simple platformer where a man hops around as well as runs around. For this project I made a series of pixel animations, with four specific animations, a Left idle (as in facing left), a Right idle, a Left walk cycle, and a right walk cycle. I have each of these animations keyed up and working in the animation player. I can run each one, but I now have to figure out a way to play each one. I made a basic code which I thought would work.
var movespeed = 150
var anim
func _process(delta):
.
anim = get
node("AnimationPlayer")
.
.
.
if Input.iskeypressed(68): #68 = D
setaxisvelocity(Vector2(move_speed,0))
anim.play("Right Walk")

It's a simple enough code, it handles walking and playing an animation at the same time. However, I didn't realize that this code refreshes multiple times per second. So I looked up a few different solutions and figured out, the easiest way to go about play the animation is to only play something when the animation changes.

'Ray Koopa' - " As you check the input in fixedprocess, you call anim_player.play() several times a frame, which always seems to restart the animation, and thus, keeps the very first frame of the animation visible all the time.

As soon as you release the key, anim_player.play() stops resetting the animation back to start, and it can actually proceed to play the following frames.

A simple straight-forward solution would be to remember the last animation you played, and only call play() as soon as it changes."

This is great. The problem is I don't know how to do this. I've been able to figure things out thus far but I'm stumped on how to do this. I know what I want to do, and how to do it, but I don't know what codes to use. If anybody can help it would be fantastic.

If there is a decent resource to use for GDscripting (as in being taught the script not just the information Godot provides) it would also be very much appreciated. That way I don't run into any more cases like this.

in Engine by (15 points)

2 Answers

+1 vote
Best answer

Do not restart your "walk" animation, when it is already running!
The AnimationPlayer's get_current_animation() function returns the name of the current animation.

(...)
if(anim.get_current_animation() != "Right Walk"):
    anim.play("Right Walk")
(...)
by (672 points)
selected by

Thankyou! This is exactly what I was looking for! I see now that I needed to check if the animation was already running. By making a new var "lr_check" I was also able to program a way of checking which side the character was facing.
So my new code is:

if Input.iskeypressed(68): #68 = D
setaxisvelocity(Vector2(movespeed,0))
lr
check = true
if(anim.getcurrentanimation() != "Right Walk"):
anim.play("Right Walk")
anim.queue("Idle Right")
if lrcheck == true and !Input.iskeypressed(68):
if (anim.get
current_animation() != "Idle Right"):
anim.play("Idle Right")

and there you go works like a dream! I can use this for program jumping animations later. Thanks for giving me that starter code. :D

0 votes

It's hard to discern exactly what your question is...

It sounds like you're not familiar with if statements and flags.

You'll want to test the animation player to see if it's playing, before replaying. There is a method on AnimationPlayer that will tell you this: http://docs.godotengine.org/en/stable/classes/class_animationplayer.html?class-animationplayer-is-playing#class-animationplayer-is-playing

And the stop method takes an optional boolean to decide whether or not to reset the animation to beginning. It's true by default, but if you set stop(false) it will not reset.

http://docs.godotengine.org/en/stable/classes/class_animationplayer.html?class-animationplayer-is-playing#class-animationplayer-stop

by (5,232 points)

Thankyou for the resources. I get that I was a little un-clear in my question. Your resources did help somewhat though and I was able to see, and think of other scripts I could write.

Thanks!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.