#Perform an action when a specific AnimatedSprite2D frame is played.

38 messages · Page 1 of 1 (latest)

next silo
timid schooner
#

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)
next silo
#

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.

next silo
timid schooner
#

Yeah, either one of the scripts would go on the AnimatedSprite2D depending on which direction you take

next silo
#

Gotcha, let me play around thanks!

timid schooner
#

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')
next silo
#

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....

timid schooner
#

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

next silo
#

Oh right, that thing we exported, like this? Not sure what to pupulate here, is this just all the frames in the animation?

timid schooner
#

Those are all the frames that you want to trigger an animation

#

So I tell it to trigger on frames 2 and 4.

next silo
#

Okay so I only want it to signal on one frame, the first one. So I have this, correct?

timid schooner
#

The animation frames are 0 based so the first frame is 0

next silo
#

Do I have this signal connected correctly?

timid schooner
#

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

next silo
#

Is it like this?

#

Or like this?

#

Because neither work, throwing ~10 errors per second

timid schooner
#

second one

#

Are you extending AnimatedSprte2D in the script?

next silo
#

I believe so, this is the full built-in script on the AnimatedSprite2D node

timid schooner
#

Whats the related section look like on the caracter node?

next silo
#

Just a simple print statement for now

#

Errors disappear when I comment out this bit on the AnimatedSprite2D node

timid schooner
next silo
#

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 !

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