#My animation works in animation editor but doesn't work when I call it by a script

1 messages · Page 1 of 1 (latest)

severe perch
#

I have another animation problem. I created an attack animation, and it works in the scene with my character. I wrote a script to play the attack animation when I press LMB, but it doesn't work!

south terraceBOT
#
How to ask for help

Following these tips will increase your chance of getting good answers:

Include Details

Helpers need to know as much as possible about your problem. Try answering the following questions:

  • What are you trying to do? (show your node setup / code)
  • What is the expected result?
  • What is happening instead? (include any error messages)
  • What have you tried so far?
Respond to Helpers

Helpers often ask you questions to better understand your problem. Not answering them or saying "it's not relevant" is not helpful. Even if it is not directly relevant, there is a high chance the answer will give more context for the people that are helping you.

Don't be demanding

Please don't expect people to immediately help you. We all do this in our freetime. It may take some time until someone who can help you will answer.

brisk tartan
#

show code that you think is misbehaving, or pictures of the scene tree where you've connected signals that they aren't getting sent, or...? 🙂

severe perch
#

second

severe perch
#

Also I tried another animations and in the result I got only 4 animations which work good

brisk tartan
#

look at line 18 vs line 25/28

#

can line 18 run without one of 25 or 28 running immediately after it?

severe perch
#

i dont know

#

may be no?

brisk tartan
#

another way to ask it is "under what conditions would 18 run and none of 25 nor 28 run after it"

#

(hint: there's an if/else guarding 25 & 28; if its condition is true then the if branch runs, and otherwise the else branch runs; as a result, the program would have to like crash or something to avoid reaching those lines...)

severe perch
#

oh I forgot to say that i tried to set autoplay for attack animation and it didnot work

brisk tartan
#

not surprising

brisk tartan
#

don't be sorry, but you'll keep having this bug until you do understand, which is why I'm explaining 🙂

#

are we both agreed: that you call play on 18 and then immediately call play again on one of line 25 or on line 28?

severe perch
#

yes

brisk tartan
#

ok. Can you play two things at the same time? (hint: from your experimentation: nope!)

severe perch
#

no we cant

brisk tartan
#

ok! So that's the bug. You can't do that.

severe perch
#

wow

brisk tartan
#

(and autoplay attack has the same problem; it gets wiped out immediately by one of 25/28 -- or other similar lines -- basically immediately)

severe perch
#

ow, If I delete lines 25/28, it will be working?

brisk tartan
#

you'll have a different bug

#

but this will work

severe perch
#

second

#

yeah, when I press lmb it works

brisk tartan
#

(but since you deleted the lines that set the walk/idle animation... you won't do that anymore)

severe perch
#

yeah

brisk tartan
#

so: you have to pick the animation you want to play, set that animation, and then stop running other animations.

#

I like a pattern called "early return" or "early exit" for that:

func _physics_process(_delta: float):
  var input := _get_input()
  _set_velocity(input)
  _set_animation(input)
  _i_dunno_other_stuff()

func _set_velocity(input: Vector2):
  velocity = speed * input
  move_and_slide()
  # Could do stuff handling collisions or is_on_wall or w/e here.

func _set_animation(input: Vector2):
  if Input.is_action_just_pressed("LMB"):
    $Sprite.play("bangbang")
    return # THE MAGIC!
  $Sprite.flip_h = input.x < 0
  if not is_approximately_zero(input.x):
    $Sprite.play("run")
    return
  $Sprite.play("idle")
  # ... other stuff?   
severe perch
#

oh

severe perch
brisk tartan
#

I also didn't define _i_dunno_other_stuff for you. Some filling in of methods required; that's just the shape of an example that uses "early exit" (see the comment "THE MAGIC", which stops it from running other animations).
(that's probably func _get_input() -> Vector2: return Input.get_vector("ui_left", ..., "ui_down") but maybe you have your own actions, and you have to give all 4, etc etc 🙂

severe perch
#

my brain is burning

brisk tartan
#

good, means you're getting better at this! Just like lifting weights.

severe perch
#

second

#

how to stop playing animation?

brisk tartan
#

pause() or stop() both work iirc

#

(which you can get to from the code -- from autocomplete, hovering over a variable, etc)

severe perch
brisk tartan
# severe perch for example, i just should write AnimationSprite2D.pause("BlaBlaBla")?

no; pause() doesn't take any arguments (and stop neither).
When you call pause(), whatever animation is currently running will be paused (and stop()/stopped; resetting to 0 iiuc).
And also, you'd need a variable named exactly AnimationSprite2D; elsewhere in your code you use $AnimatedSprite or w/e (which is "my child named AnimatedSprite), so you could use that kind of spelling here if you wanted

severe perch
#

oh

#

and how to stop all animations for 1 sec?

brisk tartan
#

There's better and worse ways of doing it. The easiest is get_tree().paused = true

#

(but you'll have to unpause, and pausing has... effects... on the whole system!)

severe perch
#

oh

#

can I use While?

brisk tartan
#

yes-ish. But basically no. The problem is stop all animations for 1 sec

#

if you want to stop all animations, you need all animations to pay attention to the thing that's stopping you

#

(and so you need something very powerful, thus: scary and tricky)

#

but if you want one object to pause, you can just call pause

#

and if you want this code not to run, you can just stop running this code

#

and if you want something to run in one second, you can
get_tree().create_timer(1.0).timeout.connect(_thing_to_do_in_one_second)

#

so between those you can probably craft a solution? 🙂

#

(the docs on create_timer are pretty good imo)

severe perch
#

Oh I remember that when I started making my first game with a youtube guide I had no problem with animations... and I had an attack animation which worked without errors

#

And now I realized that the main problem in this case is not only that I don’t know how to code, but that I’m Russian and use Google Translate to understand you

brisk tartan
#

haha

#

sorry!

#

I am hard enough for native speakers to understand, my apologies to you for you trying to get it through a robot!

severe perch
#

its okey bro)

#

I have the best degree at school english lol