#What's a good way to handle hit effect animations?

10 messages · Page 1 of 1 (latest)

vocal wigeon
#

Let's say I want to do more than just a flash on an enemy. If I have it so abilities have elements, and it chooses an animation to play based on the element, this is a snippet of what I have going on now:


func create_hit_effect(element):
    # Grab hit effect that's appropriate based on the element of the attack.
    var hit_effect_string = "hit_effect_" + str(element)
    var hit_effect_var = str_to_var(hit_effect_string)
    var hit_effect = get(hit_effect_string).instantiate()
    var hit_scene_dest = get_tree().current_scene
    hit_scene_dest.add_child(hit_effect)
    hit_effect.global_position = global_position```

The issue is that this is adding a node that has the explicit purpose of playing a hit effect animation and then being freed. If a scenario happens where 20~30 of these were to be spawned all at once, it results in a temporary but noticeable hit to performance if it's something that just so happens to be on the exact same frame (such as a group of enemies in the center of some kind of explosive/area of effect ability).

What's a better alternative to handle this sort of thing?
#

Essentially, I'm trying to find the optimal way to play an animation over another node, without having to have this be pre-existing within every node that could be hit. If it's truly just a node that plays an animation and is freed on its completion, then that's fine and means I'm probably just running into issues elsewhere, but I wanted to ask here just in case.

#

I mean, I COULD have it so every enemy has a hit effect animation player pre-existing within it that runs an animation on it if it takes a hit, but I'm wondering if this is better or worse than just creating a new node and destroying it.

An issue with this is impact location. If I fire an arrow at an enemy, I want the hit effect to play where the arrow first contacts the enemy. I don't want the animation to play in the center of the enemies sprite or something, as that would seem disjointed from the point of impact. So, what I've been doing is creating a hit animation node on the skill itself when it impacts an enemy.

tawny drift
#

if you have instances where this effect happens a lot contiounsly and at the same time, and you need to instantiate and delete it a lot, you should probably look into object pooling. basically you create hit_effect objects ahead of time and then just activate and deactivate them at a target position whenever needed and keep track or them. but you only instantiate them once at beggining of the game and dont delete them.

#

i dont know if thats what was your problem

#

but yeah if i understand correctly you can do something like have the enemy call an autoload that pools new hit effect with the current hit position of the enemy for example

vocal wigeon
plain horizon
#

Can't you just replay the hit animation or particle effect?

plain horizon
#

what I mean is that the the particle/animation is on the node that requires it

#

like i have a gun with a muzzle flash effect as a child node