Finish script when object if queue_free()d

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MaaaxiKing

I have an object and when it was hit and has 0 hp I call queue_free() on it. In its script there’s the _on_hit(damage) function. In this I increase the weapon’s damage for 5 seconds (yield(get_tree().create_timer(5), "timeout") and after the time is out, the damage will be decreased again. But if the object is queue_free()d, it cannot reset the damage. How should I solve this problem?

Hi! I’m not sure if im getting right the question, but can’t you decrease it manually before calling queue_free?

p7f | 2020-06-29 14:53

I can but I don’t want to do that. I don’t care if the object still exists or not, the damage should be decreased after 5 seconds, not before, not after.

MaaaxiKing | 2020-06-29 14:56

Can you be more descriptive? Is the object a monster? It sounds to me like you are changing the weapon’s damage from within a monster object’s script, which would make sense if the weapon belonged to the monster, but not if the weapon belongs to the player. Just trying to understand what you’re describing.

DDoop | 2020-06-29 15:17

I also don’t understand. So its like when you hit someone, you make the weapon’s damage higher for some time? In that case, the timer should be on the weapon, not on the object that got hit. You can start that timer on the weapon as soon as you increase the damage.

p7f | 2020-06-29 15:23

It’s not a monster, it’s a special stone that makes the pickaxe more “dangerous” when hit hahah.

MaaaxiKing | 2020-06-29 15:30

So i would do this way. Make a set_damage(damage) function in your weapon. Inside it you set the damage and start a timer. When the timer times out, set the damaga again to original value. Instead of using yield, i would probably use a Timer node in case you can hit multiple stones at the same time, and you dont want to the damage resets when the first timeout occured.

p7f | 2020-06-29 15:37

I concur, sounds like you are describing “feature envy” MaaaxIKing

DDoop | 2020-06-29 15:40

:bust_in_silhouette: Reply From: 1shevelov

You can use the signal “tree_exiting()” and reset the damage upon receiving it. But you can’t change property of already freed object because it exists no more.
Or maybe you should move this property in the parent object or Globals singleton and read the value when creating object and update when it changes.

I think he is asking other thing. As he said in the comments, he wants to hit a stone that temporary gives the character’s weapon more damage. After a time, he wants to reset that damage. He is reseting the damage of the weapon from the stone, but when the stone is cleared, it doesnt reset it anymore, ofcourse, because it was freed.
Thats why some of us suggested he should reset the damage from the weapon itself, instead of trying to reset it from an object that can be cleared.

p7f | 2020-06-30 12:55

p7f already helped me to solve my problem. I want the damage to reset after 5 seconds and not when the stone doesn’t exist anymore or at any other time. Just when 5 seconds are over. Thanks to p7f!

MaaaxiKing | 2020-06-30 13:29