Yay! Toon Tanks complete, at least for now.
Thanks a lot, Stephen!
I’m afraid making a member variable public as seen in the video is not within best OOP practices though, or at least as I remember them. I can understand it’s a quick way to fix the issue at hand, but it breaks encapsulation. If you think about it, nothing stops the ATower class or any other class from modifying an inner variable of ATank without ATank knowing or having any control about it, which could lead to issues in the long run.
Anyway, maybe this is overkill or it’s me who is wrong, but thinking this might be of use for someone in the future or starting an interesting conversation about best coding practices, I’m leaving the relevant parts of this alternate implementation here:
Tank.h:
public:
// [Rest of public functions redacted for brevity]
bool IsAlive();
private:
//[...]
bool bAlive = true;
Tank.cpp
void ATank::HandleDestruction()
{
Super::HandleDestruction();
SetActorHiddenInGame(true);
SetActorTickEnabled(false);
bAlive = false; // this is ok as we are within the Tank class
}
bool ATank::IsAlive()
{
return bAlive;
}
Tower.cpp
void ATower::CheckFireCondition()
{
if (InFireRange() && Tank->IsAlive()) {
Fire();
}
}
What do you gain from this? Now no class but ATank itself can modify bActive but can check if the tank is still alive, and if the condition for the tank to be alive changes and does involve more than just a boolean value, you can modify the inner workings of the IsAlive() function inside ATank without having to also change other classes code.