#Perform an action when a specific AnimatedSprite2D frame is played.
38 messages · Page 1 of 1 (latest)
What you can do is extend AnimatedSprite2D to emit a signal:
extends AnimatedSprite2D
signal frame_change(frame: int)
## The frames that will trigger an event
@export var event_frames: Array[int]
func _on_animation_change():
if event_frames.has(frame):
frame_change.emit(frame)
Then on your animated sprite, the signals would then look like the image.
You could also use a dictionary if you want to filter by event types and use the same signal:
extends AnimatedSprite2D
signal frame_change(frame: int, event: String)
@export var event_frames: Dictionary
func _on_animation_change():
var event = event_frames.get(frame)
if event != null:
frame_change.emit(frame, event)
Thanks just trying to work it out, do I need to add a script to the AnimatedSprite2D? I already have a script on the parent CharacterBody2D node.
Ta, not familiar with dictionaries, probs should get on with that at some point
Yeah, either one of the scripts would go on the AnimatedSprite2D depending on which direction you take
Gotcha, let me play around thanks!
Then where ever you decide to listen, you can do this if you use a dictionary:
extends Node2D
func _on_animation_change(frame: int, event: String):
if event == 'event_two':
print('event_two')
Still trying method one. I created a built-in script on the AnimatedSprite2D node. and added the two signals as per the pic below.
This is the code in the AnimatedSprite2D script and also the signal connection. Is that correct? Source and target is the same, looks odd.
This is the code in the parent CharacterBody2D node called Dripguy
These are the errors....
You could have only one if you want and connect it in the code if you don't like it in the GUI:
func _ready():
frame_changed.connect(_on_animation_change)
Then you only need one in the GUI
Oh right, that thing we exported, like this? Not sure what to pupulate here, is this just all the frames in the animation?
Those are all the frames that you want to trigger an animation
So I tell it to trigger on frames 2 and 4.
Okay so I only want it to signal on one frame, the first one. So I have this, correct?
The animation frames are 0 based so the first frame is 0
Do I have this signal connected correctly?
second frame is 1 and so on
Yeah, that will notify itself that the animation changed, then in that function you will want to get the frame and emit to your character
I believe so, this is the full built-in script on the AnimatedSprite2D node
Whats the related section look like on the caracter node?
Just a simple print statement for now
Errors disappear when I comment out this bit on the AnimatedSprite2D node
After careful cross-referencing, I determined that I missed this bit in the brackets
Cool! The print statements now fire, so it should be clear sailing from here, thanks again @timid schooner !
If you don't care about the frame, in your character, you don't even have to emit it in your signal.
signal frame_change
...
frame_change.emit()
func _on_animation_change():
print('SUCCESS!!!!!!')
All depends on what you need
But, having it is useful when you need emit different things in the same animation so your listener know what it should do