Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored asserts on getting objects and logging #293

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/Cloud9/Character/Cloud9AIController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void ACloud9AIController::OnMoveCompleted(FAIRequestID RequestID, const FPathFol
void ACloud9AIController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
CheckIsValid(InPawn, Error, "Pawn is invalid")
AssertOrVoid(InPawn, Error, "Pawn is invalid");
BaseLocation = InPawn->GetActorLocation();
}

Expand Down
21 changes: 10 additions & 11 deletions Source/Cloud9/Character/Cloud9Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,14 @@ float ACloud9Character::InternalTakePointDamage(

if (Settings->WeaponDebugDamageInfo)
{
log(Display,
"[Actor='%s'] Distance=%f RangeCoefficient=%f BoneName=%s Armored=%d DamageToHealth=%f DamageToArmor=%f",
*GetName(),
FMath::Sqrt(Distance),
RangeCoefficient,
*BoneName.ToString(),
HitInArmor,
DamageToHealth,
DamageToArmor);
ObjectDisplay(
"Distance=%f RangeCoefficient=%f BoneName=%s Armored=%d DamageToHealth=%f DamageToArmor=%f",
FMath::Sqrt(Distance),
RangeCoefficient,
*BoneName.ToString(),
HitInArmor,
DamageToHealth,
DamageToArmor);
}

FlinchModLarge = WeaponInfo->GetFlinchVelocityModifierLarge();
Expand Down Expand Up @@ -492,7 +491,7 @@ void ACloud9Character::OnConstruction(const FTransform& Transform)
if (not CameraTargetBoneName.IsNone())
{
let HeadBoneLocation = MyMesh->GetBoneLocation(CameraTargetBoneName, EBoneSpaces::WorldSpace);
log(Verbose, "Setup CameraBoom = %s", *HeadBoneLocation.ToString());
ObjectVerbose("Setup CameraBoom = %s", *HeadBoneLocation.ToString());
CameraBoom->SetWorldLocation(HeadBoneLocation);
}

Expand Down Expand Up @@ -523,7 +522,7 @@ void ACloud9Character::OnConstruction(const FTransform& Transform)
HitBox->SetCollisionProfileName(TRACE_HITBOX);
HitBox->CreationMethod = EComponentCreationMethod::UserConstructionScript;

log(Error, "Hitbox registered = %s [%f]", *HitBox->GetName(), HitBox->GetUnscaledCapsuleRadius());
ObjectVerbose("Hitbox registered = %s [%f]", *HitBox->GetName(), HitBox->GetUnscaledCapsuleRadius());
}
}
#endif
Expand Down
47 changes: 12 additions & 35 deletions Source/Cloud9/Character/Components/Cloud9AnimationComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,35 @@ UCloud9AnimationComponent::UCloud9AnimationComponent() {}
UAnimInstance* UCloud9AnimationComponent::GetAnimInstance() const
{
let Character = GetOwner<ACloud9Character>();

if (not IsValid(Character))
{
log(Error, "[Component='%s'] Character is invalid", *GetName());
return nullptr;
}
AssertOrReturn(IsValid(Character), nullptr, Error, "Character is invalid");

let Mesh = Character->GetMesh();

if (not IsValid(Mesh))
{
log(Error, "[Component='%s'] Mesh is invalid", *GetName());
return nullptr;
}
AssertOrReturn(IsValid(Mesh), nullptr, Error, "Mesh is invalid");

return Mesh->GetAnimInstance();
}

bool UCloud9AnimationComponent::PlayMontage(UAnimMontage* Montage, float StartTime, float Rate) const
{
if (not IsValid(Montage))
{
log(Error, "[Component='%s'] Montage is invalid", *GetName());
return false;
}
AssertOrReturn(IsValid(Montage), false, Error, "Montage is invalid");

let AnimInstance = GetAnimInstance();
AssertOrReturn(
IsValid(AnimInstance), false,
Error, "AnimInstance is invalid for montage '%s'",
*Montage->GetName());

if (not IsValid(AnimInstance))
{
log(Error, "[Component='%s'] AnimInstance is invalid for montage '%s'", *GetName(), *Montage->GetName());
return false;
}

if (not AnimInstance->Montage_Play(Montage, Rate, EMontagePlayReturnType::MontageLength, StartTime))
{
log(Error, "[Component='%s'] Can't play montage '%s'", *GetName(), *Montage->GetName());
return false;
}
AssertOrReturn(
AnimInstance->Montage_Play(Montage, Rate, EMontagePlayReturnType::MontageLength, StartTime), false,
Error, "Can't play montage '%s'", *Montage->GetName()
);

return true;
}

bool UCloud9AnimationComponent::IsAnyMontagePlaying() const
{
let AnimInstance = GetAnimInstance();

if (not IsValid(AnimInstance))
{
log(Error, "[Component='%s'] AnimInstance is invalid", *GetName());
return false;
}

AssertOrReturn(IsValid(AnimInstance), false, Error, "AnimInstance is invalid");
return AnimInstance->IsAnyMontagePlaying();
}
19 changes: 8 additions & 11 deletions Source/Cloud9/Character/Components/Cloud9EffectsComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ UCloud9CharacterEffectTrait* UCloud9EffectsComponent::AddEffect(
AppliedCanDamagedEffects.Add(Effect);
}

log(Verbose, "[Component='%s'] Apply effect class='%s' effect='%s' (%p) on owner='%s'",
*GetName(), *EffectClass->GetName(), *Effect->GetName(), Effect, *GetOwnerName());
ObjectVerbose(
"Apply effect class='%s' effect='%s' (%p) on owner='%s'",
*EffectClass->GetName(), *Effect->GetName(), Effect, *GetOwnerName());

return Effect;
}
Expand All @@ -54,11 +55,7 @@ UCloud9CharacterEffectTrait* UCloud9EffectsComponent::AddEffect(

bool UCloud9EffectsComponent::RemoveEffect(UCloud9CharacterEffectTrait* Effect)
{
if (AppliedEffects.Remove(Effect) == 0)
{
log(Warning, "[Component='%s'] Effect '%s' not found", *GetName(), *Effect->GetName());
return false;
}
AssertOrReturn(AppliedEffects.Remove(Effect) != 0, false, Warning, "Effect '%s' not found", *Effect->GetName());

AppliedCanDamagedEffects.Remove(Effect);
AppliedCanTickEffects.Remove(Effect);
Expand All @@ -67,8 +64,8 @@ bool UCloud9EffectsComponent::RemoveEffect(UCloud9CharacterEffectTrait* Effect)

Effect->OnRemove();

log(Verbose, "[Component='%s'] Remove effect='%s' (%p) on owner='%s'",
*GetName(), *Effect->GetName(), Effect, *GetOwnerName());
ObjectVerbose("Remove effect='%s' (%p) on owner='%s'", *Effect->GetName(), Effect, *GetOwnerName());

return true;
}

Expand All @@ -82,10 +79,10 @@ void UCloud9EffectsComponent::RemoveAllEffects()
UCloud9HealthComponent* UCloud9EffectsComponent::GetHealthComponent() const
{
let Character = GetOwner<ACloud9Character>();
CheckIsValid(Character, Error, "Owner is invalid", nullptr);
AssertOrReturn(IsValid(Character), nullptr, Error, "Owner is invalid");

let HealthComponent = Character->GetHealthComponent();
CheckIsValid(HealthComponent, Error, "Owner HealthComponent is invalid", nullptr);
AssertOrReturn(HealthComponent, nullptr, Error, "Owner HealthComponent is invalid");

return HealthComponent;
}
Expand Down
60 changes: 13 additions & 47 deletions Source/Cloud9/Character/Components/Cloud9HealthComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,26 @@ void UCloud9HealthComponent::Initialize(FHealthConfig Config)

bool UCloud9HealthComponent::TakeHealthDamage(float Change)
{
if (Change >= 0.0f)
{
return ChangeHealth(Health - Change);
}

log(Warning, "Can't increase health using this method Change=%f", Change);
return false;
AssertOrReturn(Change >= 0.0f, false, Warning, "Can't increase health using this method Change=%f", Change);
return ChangeHealth(Health - Change);
}

bool UCloud9HealthComponent::TakeArmorDamage(float Change)
{
if (Change >= 0.0f)
{
return ChangeArmor(Armor - Change);
}

log(Warning, "Can't increase armor using this method Change=%f", Change);
return false;
AssertOrReturn(Change >= 0.0f, false, Warning, "Can't increase armor using this method Change=%f", Change);
return ChangeArmor(Armor - Change);
}

bool UCloud9HealthComponent::IncreaseHealth(float Change)
{
if (Change >= 0.0f)
{
return ChangeHealth(Health + Change);
}

log(Warning, "Can't decimante health using this method Change=%f", Change);
return false;
AssertOrReturn(Change >= 0.0f, false, Warning, "Can't decimante health using this method Change=%f", Change);
return ChangeHealth(Health + Change);
}

bool UCloud9HealthComponent::IncreaseArmor(float Change)
{
if (Change >= 0.0f)
{
return ChangeArmor(Health + Change);
}

log(Warning, "Can't decimante armor using this method Change=%f", Change);
return false;
AssertOrReturn(Change >= 0.0f, false, Warning, "Can't decimante armor using this method Change=%f", Change);
return ChangeArmor(Health + Change);
}

bool UCloud9HealthComponent::IsInvulnerable() const { return bIsInvulnerable; }
Expand All @@ -79,16 +59,11 @@ bool UCloud9HealthComponent::ChangeHealth(float NewHealth)

if (Health == 0.0f)
{
let Owner = GetOwner();

if (not IsValid(Owner))
{
log(Fatal, "[Component=%s] Owner isn't valid", *GetName());
return false;
}
let MyOwner = GetOwner();
AssertOrReturn(IsValid(MyOwner), false, Fatal, "Owner isn't valid");

bIsAlive = false;
OnCharacterDie.Broadcast(Owner);
OnCharacterDie.Broadcast(MyOwner);
}

return true;
Expand Down Expand Up @@ -118,12 +93,7 @@ void UCloud9HealthComponent::OnRegister()
Super::OnRegister();

let MyOwner = GetOwner();

if (not IsValid(MyOwner))
{
log(Error, "HealthComponent isn't valid");
return;
}
AssertOrVoid(IsValid(MyOwner), Error, "HealthComponent isn't valid");

// Register twice or delegate add twice (?)
MyOwner->OnTakePointDamage.AddUniqueDynamic(this, &UCloud9HealthComponent::OnTakePointDamage);
Expand All @@ -134,11 +104,7 @@ void UCloud9HealthComponent::AddAttackerScore(const AController* InstigatedBy) c
{
if (not bIsAlive)
{
if (not IsValid(InstigatedBy))
{
log(Error, "[Component = %s] InstigatedBy is invalid", *GetName());
return;
}
AssertOrVoid(IsValid(InstigatedBy), Error, "InstigatedBy is invalid");

// TODO: Make score component
if (let Character = Cast<ACloud9Character>(InstigatedBy->GetCharacter()); IsValid(Character))
Expand Down
Loading