Returning bool parameters and creating a reference to Health in CanAttack

So real quick I’ve got a few questions. Bools are still catching me and I’m a little confused.

  1. Why do we put in the parameter of CombatTarget combatTarget to the bool, is there a reason for that rather than just getting the CombatTarget component inside of CanAttack or something?

  2. When we just type “return” followed by targetToTest != null && !targetToTest.isDead() is that basically just saying “return true if both statements are true”? Would it be okay to type out something like “if(targetToTest !=null && !targetToTest.isDead()) return true;”? Is that basically the same thing?

  3. What is the reason to create a reference to Health in CanAttack vs using the Health reference (target) at the top of the class? I tried that and my player just attacked automatically as soon as he was in range of a target, but I couldn’t parse through the logic. Would I be able to—like in the Health script—add a bool called canAttack at the top of the class and set up “CanAttack” the same way?

Could someone help? Thank you so much! Loving this course.

Exactly that. We need a reference to the thing we’re considering attacking.

That’s exactly correct, except that you’d need to have a return false after that.

return targetToTest!=null && !targetToTest.IsDead(); //one line and done.

vs

if(targetToTest!=null && !targetToTest.IsDead()) return true;
return false;

The target at the top of the class is the target we’re currently attacking. We also want to be able to test externally if we can attack a different target. For example, I might be in the process of bashing Rick with my Sword of Negating Silliness when I see Sam approaching. With CanAttack decoupled from the target at the top of the class (Rick), I can check to see if I can attack Sam.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms