Projectile Damage

So I have a Character class that has a private variable int health in my Projectile class I have this:

if (resultNode->HasComponent<Character>())
{
    _Node = resultNode->GetComponent<Character>();
    _Node->setHealth(_Node->getHealth() - damage);
}

It works fine I just feel like my code is not efficient.

If it’s about writing it with less text:

auto character = resultNode->GetComponent<Character>();
if (character)
{
    _Node = character;
    _Node->setHealth(_Node->getHealth() - damage);
}
1 Like

@SirNate0 Looks a lot cleaner, and easier to read.

If you don’t need to keep the last Character you can shorten it even more, though at the cost of some readability

if (_Node = resultNode->GetComponent<Character>())
    _Node->setHealth(_Node->getHealth() - damage);
1 Like

I need to come up with a good method for and AI that has died. If the AI health hits zero play the death animation then remove the CrowdAgent from that node.

Is there a way to remove everything from the node, but not delete the model just yet. My ideas have not worked very well. I can get the AI to die then play the death animation, but the CrowdAgent keeps affecting the node.

A more generalized approach would be to use GetDerivedComponent<Character>(). This way components that derive from Character would also receive damage.

1 Like

The way I usually handle this is to have the dying AI object spawn a separate object to play the death animation, then mark itself for deletion. That way the death animation object has no residual components that are no longer relevant, such as crowd agents or physics objects. If you need to track anything from the AI (corpse data, ie for spells that consume corpses Diablo-clone-style) just pass that through a special component attached to the death object.

Won’t this have a strange affect though? Like a random spawning character model that plays the death animation, but the previous model instantly disappearing. Wouldn’t that be really noticeable though?

Not if they start from the same point in the animation. Both approaches are effectively the same, it’s just different based on the actual setup you have for your game - with your idea you remove some components and leave others, with JTippetts’ you create a new node and copy a few essentials (models/poses/etc) as needed (which could be nothing depending).

Regarding your request, can I ask why you want to leave the CrowdAgent until after death?

I don’t want to keep it after death. Problem I was having when my npc dies the crowd agent was still active.

Can you just remove or disable that component?

I tried disabling the agent, but that did not work.

Like to post I fix this. I have it were my AI class templates fix this issue.