Fixed Missing Ref when Quickly Switching Equipment

This commit is contained in:
Rafal Swierczek 2023-09-30 16:05:28 +01:00
parent 6cf1fc1869
commit f2215f05e4
5 changed files with 96 additions and 58 deletions

Binary file not shown.

View File

@ -131,25 +131,27 @@ void AEndlessVendettaCharacter::ToggleCombat()
//When 1 is pressed it calls EquipPrimary //When 1 is pressed it calls EquipPrimary
void AEndlessVendettaCharacter::EquipPrimary() void AEndlessVendettaCharacter::EquipPrimary()
{ {
if (IsValid(PrimaryWeapon))
{
PrimaryWeapon->Destroy();
PrimaryWeapon = nullptr;
bHasRifle = false;
return;
}
if (IsValid(SecondaryWeapon)) EquipSecondary();
// Marcel I respect you, like you and what not, please for the love of god don't touch these two guard clauses // Marcel I respect you, like you and what not, please for the love of god don't touch these two guard clauses
///////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (GadgetManager->IsReconEquipped() && !GadgetManager->TryToUnequipRecon()) return;///////////////////////// if (GadgetManager->IsReconEquipped() && !GadgetManager->TryToUnequipRecon()) return;/////////////////////////
if (GadgetManager->IsCombatEquipped() && !GadgetManager->TryToUnequipCombat()) return;/////////////////////// if (GadgetManager->IsCombatEquipped() && !GadgetManager->TryToUnequipCombat()) return;///////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////
//If primary weapon is not there but secondary weapon is it will call equipSecondary.
//EquipSecondary checks and sees that secondary is there so it will call to destroy itself
//Code goes back and sees primary weapon is not there anymore and spawns it in.
//Same thing for the EquipSecondary()
if (!IsValid(PrimaryWeapon))
{
if (IsValid(SecondaryWeapon)) EquipSecondary();
UE_LOG(LogTemp, Display, TEXT("Primary equipped"));
bHasRifle = true;
FActorSpawnParameters spawnParams; FActorSpawnParameters spawnParams;
spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true); FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true);
bHasRifle = true;
//Creating a new actor object called PrimaryWeapon that is based off primaryweaponClass //Creating a new actor object called PrimaryWeapon that is based off primaryweaponClass
AActor* PrimaryWeaponActor = GetWorld()->SpawnActor<AActor>(PrimaryWeaponClass, spawnParams); AActor* PrimaryWeaponActor = GetWorld()->SpawnActor<AActor>(PrimaryWeaponClass, spawnParams);
PrimaryWeaponActor->AttachToComponent(Mesh1P, AttachmentRules, FName("GripPoint")); PrimaryWeaponActor->AttachToComponent(Mesh1P, AttachmentRules, FName("GripPoint"));
@ -157,50 +159,92 @@ void AEndlessVendettaCharacter::EquipPrimary()
//We do this because we need to check if PrimaryWeapon is equipped and we want primaryweapon to be ABaseWeapon type and not a generic AActor //We do this because we need to check if PrimaryWeapon is equipped and we want primaryweapon to be ABaseWeapon type and not a generic AActor
PrimaryWeapon = Cast<ABaseWeaponClass>(PrimaryWeaponActor); PrimaryWeapon = Cast<ABaseWeaponClass>(PrimaryWeaponActor);
} //If primary weapon is not there but secondary weapon is it will call equipSecondary.
else if (IsValid(PrimaryWeapon)) //EquipSecondary checks and sees that secondary is there so it will call to destroy itself
{ //Code goes back and sees primary weapon is not there anymore and spawns it in.
PrimaryWeapon->Destroy(); //Same thing for the EquipSecondary()
bHasRifle = false;
}
// if (!IsValid(PrimaryWeapon))
// {
// if (IsValid(SecondaryWeapon)) EquipSecondary();
//
// UE_LOG(LogTemp, Display, TEXT("Primary equipped"));
// bHasRifle = true;
// FActorSpawnParameters spawnParams;
// spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
// FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true);
// //Creating a new actor object called PrimaryWeapon that is based off primaryweaponClass
// AActor* PrimaryWeaponActor = GetWorld()->SpawnActor<AActor>(PrimaryWeaponClass, spawnParams);
// PrimaryWeaponActor->AttachToComponent(Mesh1P, AttachmentRules, FName("GripPoint"));
// //Changing PrimaryWeaponActor to ABaseWeaponClass type instead of actor and storing it into PrimaryWeapon which is a ABaseClass Object
// //We do this because we need to check if PrimaryWeapon is equipped and we want primaryweapon to be ABaseWeapon type and not a generic AActor
// PrimaryWeapon = Cast<ABaseWeaponClass>(PrimaryWeaponActor);
//
// }
// else if (IsValid(PrimaryWeapon))
// {
// PrimaryWeapon->Destroy();
// bHasRifle = false;
// }
} }
void AEndlessVendettaCharacter::EquipSecondary() void AEndlessVendettaCharacter::EquipSecondary()
{ {
if (IsValid(SecondaryWeapon))
{
SecondaryWeapon->Destroy();
SecondaryWeapon = nullptr;
bHasRifle = false;
return;
}
if (IsValid(PrimaryWeapon)) EquipPrimary();
// Marcel I respect you, like you and what not, please for the love of god don't touch these two guard clauses // Marcel I respect you, like you and what not, please for the love of god don't touch these two guard clauses
///////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (GadgetManager->IsReconEquipped() && !GadgetManager->TryToUnequipRecon()) return;///////////////////////// if (GadgetManager->IsReconEquipped() && !GadgetManager->TryToUnequipRecon()) return;/////////////////////////
if (GadgetManager->IsCombatEquipped() && !GadgetManager->TryToUnequipCombat()) return;/////////////////////// if (GadgetManager->IsCombatEquipped() && !GadgetManager->TryToUnequipCombat()) return;///////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (!IsValid(SecondaryWeapon))
{
if (IsValid(PrimaryWeapon)) EquipPrimary();
UE_LOG(LogTemp, Display, TEXT("Secondary equipped"));
bHasRifle = true;
FActorSpawnParameters spawnParams; FActorSpawnParameters spawnParams;
spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true); FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true);
bHasRifle = true;
AActor* SecondaryWeaponActor = GetWorld()->SpawnActor<AActor>(SecondaryWeaponClass, spawnParams); AActor* SecondaryWeaponActor = GetWorld()->SpawnActor<AActor>(SecondaryWeaponClass, spawnParams);
SecondaryWeaponActor->AttachToComponent(Mesh1P, AttachmentRules, FName("GripPoint")); SecondaryWeaponActor->AttachToComponent(Mesh1P, AttachmentRules, FName("GripPoint"));
SecondaryWeapon = Cast<ABaseWeaponClass>(SecondaryWeaponActor); SecondaryWeapon = Cast<ABaseWeaponClass>(SecondaryWeaponActor);
}
else if (IsValid(SecondaryWeapon)) // if (!IsValid(SecondaryWeapon))
{ // {
SecondaryWeapon->Destroy(); // if (IsValid(PrimaryWeapon)) EquipPrimary();
bHasRifle = false; //
} // UE_LOG(LogTemp, Display, TEXT("Secondary equipped"));
// bHasRifle = true;
// FActorSpawnParameters spawnParams;
// spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
// FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true);
// AActor* SecondaryWeaponActor = GetWorld()->SpawnActor<AActor>(SecondaryWeaponClass, spawnParams);
// SecondaryWeaponActor->AttachToComponent(Mesh1P, AttachmentRules, FName("GripPoint"));
// SecondaryWeapon = Cast<ABaseWeaponClass>(SecondaryWeaponActor);
// }
// else if (IsValid(SecondaryWeapon))
// {
// SecondaryWeapon->Destroy();
// bHasRifle = false;
// }
} }
//Calls the fire function in the baseWeaponClass //Calls the fire function in the baseWeaponClass
void AEndlessVendettaCharacter::TapFireCaller() void AEndlessVendettaCharacter::TapFireCaller()
{ {
if (IsValid(PrimaryWeapon) || IsValid(SecondaryWeapon)) UE_LOG(LogTemp, Warning, TEXT("Tap Fire"));
{ // if (IsValid(PrimaryWeapon) || IsValid(SecondaryWeapon))
PrimaryWeaponClass.GetDefaultObject()->TapFire(); // {
} // PrimaryWeaponClass.GetDefaultObject()->TapFire();
// }
} }
//POTENTIAL PICKUP SYSTEM NEEDS CHANGING BUT YES //POTENTIAL PICKUP SYSTEM NEEDS CHANGING BUT YES

View File

@ -56,19 +56,13 @@ public:
bool TryToUnequipRecon() bool TryToUnequipRecon()
{ {
if (ReconGadget->Equipped) if (ReconGadget->Equipped) return ReconGadget->Unequip();
{
return ReconGadget->Unequip();
}
return false; return false;
} }
bool TryToUnequipCombat() bool TryToUnequipCombat()
{ {
if (CombatGadget->Equipped) if (CombatGadget->Equipped) return CombatGadget->Unequip();
{
return CombatGadget->Unequip();
}
return false; return false;
} }