Godot Version
4.4.1
Question
Hello guys,
I am trying to write my logic for dealing constant damage.
Here is the structure:
A hurtbox Component that detectes Area Entered/exited and emit an signal.
A attack Component that receives the signal and start/stop a timer to deal constant damage to the objects
I am using a pool system for the enemy so when enemy is “dead” he will be returened to the pool.
The problem I am having is that, if the enemy was killed before he exit the hurtbox detection, no exit signal will be emit and the damage timer won’t stop.
The method I am thinking to fix this is maybe to check for has_overlaping_areas in _process() and if its empty stop the timer.
But I am really not sure if this is the right approach, can anyone give me some advice on this? Or there is a better approach to write this type of constant damage logic.
Thank you very much
What if you emit a signal when the enemy is killed? You would need to connect the signal via code to the attack component.
yea, I have a enemy died signal. But I only used it for the enemypool to catch it and collect it.
I found a walkaround that
we can set timer.one_shot = true.
and each time it deals damage, check if there is overlapping area, if so. start the timer. It seems to work
1 Like
Hi!
What I find strange is that you’re using multiple signals for a feature that doesn’t look like it needs signals?
Of course you need to hook some functions to the native area_entered and area_exited signals, but then, why is the attack component receiving any signal from the hurtbox component, as it can just have a reference to it and get its variables?
Something like this:
1/ The hurtbox detects (area_entered) a component it must deal damage to.
2/ The hurtbox stores this component inside a list, so that it’s keeping track of all the components to damage over time.
3/ On area_exited, if the area is in the list, remove it, as it must not be damaged over time anymore.
This way, you know every frame what components are inside the hurtbox.
Then:
4/ Have a reference to the hurtbox component inside the attack component.
5/ In the process function of your attack component, loop through the list of targets in the hurtbox and apply damage to the objects inside.
I believe it’s better to just let the hurtbox component detect what is inside so that other component can then read that info, and not trigger any signal. Signals are a great feature but come with some cost: as you saw, you have to be careful removing them properly, which can be troublesome, and they make the code sometimes harder to read as when you’re emitting a signal, you can’t see directly what functions are listening to it and in what order.
A golden rule when using signals, is to make sure that the receiving functions can be called in any order regardless of what they do, as changing that order is hard. If your functions must have a specific order, then you actually don’t want a signal.
TL;DR: In your context, I’d suggest not using signals too much, they’re really cool but sometimes, simply having references to other components and caling functions directly is just better. 
Hi sixrobin,
Thanks for your reply.
I just start to use godot and am still trying to figuring things along the way.
Yes, I was using the method you mentioned before. Actually I don’t have an attack component before, I just maintain the target List and all the damage logic directly inside the hurbox Component.
But soon I realized that I might need more complicted damage logic. The combat system I am building is kind of like a vampire survivor type of game.
So my plan was to build every attack into different skill scene that can be attached to enemy/player scene. And then attach a hurtbox component and attackbox component to the skill scene.
At the skill script, I will define what type of skill it is, can be a single attack , constant damage attack and etc. So what I thought was, I don’t always need a target list. If the damage was just a single hit skill, I can just get the object entered the hurtbox and deal damage. And I can do that inside attack compoent with a if check.
If skill.type = single, then get entered hitbox and deal damage.
If skill.type = constant, then maintain a overlapping hitbox list, and deal damage periodically
This way I can use the components for every skill types.
The idea I have is that to have all the attack related logic inside the attack component, and just let the hurtbox do the detection. I am not sure if this is the correct approach because I can’t find many reference on the internet. But this is what I liked about Godot so far, it is simple to play around with different ideas.
That seems right to me. Any script you create should have only one responsibility, so if one is called “Attack”, it should handle the attack, and if one is called “Hurtbox”, it should handle detection. Then both of them can communicate, of course.
If you’re beginning (not sure if you’re beginning with game development in general or just Godot specifically), I believe the right approach would be the one that works fine and you find simple enough to work with/iterate on. There are a lot of ways to build an ability system such as Vampire Survivors’ one, many of them probably too complicated to begin with, so start simple, and remember you can always change you code structure later on if needed.
Thank you for your advice! 
1 Like