#My animation works in animation editor but doesn't work when I call it by a script
1 messages · Page 1 of 1 (latest)
Following these tips will increase your chance of getting good answers:
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?
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.
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.
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...? 🙂
second
Also I tried another animations and in the result I got only 4 animations which work good
look at line 18 vs line 25/28
can line 18 run without one of 25 or 28 running immediately after it?
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...)
oh I forgot to say that i tried to set autoplay for attack animation and it didnot work
not surprising
man I dont understand sorry(
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?
yes
ok. Can you play two things at the same time? (hint: from your experimentation: nope!)
no we cant
ok! So that's the bug. You can't do that.
wow
(and autoplay attack has the same problem; it gets wiped out immediately by one of 25/28 -- or other similar lines -- basically immediately)
ow, If I delete lines 25/28, it will be working?
(but since you deleted the lines that set the walk/idle animation... you won't do that anymore)
yeah
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?
oh
oh when I wrote var input := _get_input() this text became red
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 🙂
my brain is burning
good, means you're getting better at this! Just like lifting weights.
pause() or stop() both work iirc
https://docs.godotengine.org/en/stable/classes/class_animatedsprite2d.html will always have the right answer
Inherits: Node2D< CanvasItem< Node< Object Sprite node that contains multiple textures as frames to play for animation. Description: AnimatedSprite2D is similar to the Sprite2D node, excep...
(which you can get to from the code -- from autocomplete, hovering over a variable, etc)
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
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!)
https://docs.godotengine.org/en/latest/tutorials/scripting/pausing_games.html for the docs on that
Introduction: In most games it is desirable to, at some point, interrupt the game to do something else, such as taking a break or changing options. Implementing a fine-grained control for what can ...
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)
Inherits: MainLoop< Object Manages the game loop via a hierarchy of nodes. Description: As one of the most important classes, the SceneTree manages the hierarchy of nodes in a scene, as well as sce...
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