From 8886dda9016a3d03b1d999b4471f17bb403b8945 Mon Sep 17 00:00:00 2001 From: Marcel Hara Date: Tue, 14 Nov 2023 17:00:58 +0000 Subject: [PATCH] Added Weapon Saving --- .../EndlessVendettaCharacter.cpp | 73 ++++++++++++++----- .../EndlessVendettaCharacter.h | 5 ++ 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/EndlessVendetta/Source/EndlessVendetta/EndlessVendettaCharacter.cpp b/EndlessVendetta/Source/EndlessVendetta/EndlessVendettaCharacter.cpp index 97bbe387..b8058c13 100644 --- a/EndlessVendetta/Source/EndlessVendetta/EndlessVendettaCharacter.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/EndlessVendettaCharacter.cpp @@ -274,13 +274,22 @@ void AEndlessVendettaCharacter::ToggleCombat() //When 1 is pressed it calls EquipPrimary void AEndlessVendettaCharacter::EquipPrimary() { + FActorSpawnParameters spawnParams; + spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; + FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true); + FDetachmentTransformRules DetatchRules(EDetachmentRule::KeepWorld, false); + if (bIsReloading) return; if (IsValid(PrimaryWeapon)) { - PrimaryWeapon->Destroy(); + PrimaryWeapon->DetachFromActor(DetatchRules); + PrimaryWeapon->SetActorLocation(FVector(0,0,0), false, nullptr, ETeleportType::None); + PrimaryWeapon->SetActorHiddenInGame(true); this->GetFirstPersonCameraComponent()->SetFieldOfView(90); + UE_LOG(LogTemp, Warning, TEXT("Primary Weapon Is Hidden: %hhd"), PrimaryWeapon->IsHidden()); PrimaryWeapon = nullptr; bHasRifle = false; + GLog->Log("Primary Weapon Put Away"); return; } @@ -289,30 +298,50 @@ void AEndlessVendettaCharacter::EquipPrimary() if (GadgetManager->IsValidReconGadget() && GadgetManager->IsReconEquipped() && !GadgetManager->TryToUnequipRecon()) return; if (GadgetManager->IsValidCombatGadget() && GadgetManager->IsCombatEquipped() && !GadgetManager->TryToUnequipCombat()) return; - - FActorSpawnParameters spawnParams; - spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; - FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true); - - bHasRifle = true; + //Creating a new actor object called PrimaryWeapon that is based off primaryweaponClass - AActor* PrimaryWeaponActor = GetWorld()->SpawnActor(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 + + if(!IsValid(PrimaryWeapon)) + { + bHasRifle = true; + if(!bIsPrimaryWeaponCreated) + { + PrimaryWeaponActor = GetWorld()->SpawnActor(PrimaryWeaponClass, spawnParams); + PrimaryWeaponActor->AttachToComponent(Mesh1P, AttachmentRules, FName("GripPoint")); + PrimaryWeapon = Cast(PrimaryWeaponActor); + PrimaryWeapon->SetActorHiddenInGame(false); + bIsPrimaryWeaponCreated = true; + } + //UE_LOG(LogTemp, Warning, TEXT("Primary Weapon Is Hidden: %hhd"), PrimaryWeapon->IsHidden()); + GLog->Log("Primary Weapon Equipped"); + } + PrimaryWeaponActor->AttachToComponent(Mesh1P, AttachmentRules, FName("GripPoint")); PrimaryWeapon = Cast(PrimaryWeaponActor); + PrimaryWeapon->SetActorHiddenInGame(false); GetWorldTimerManager().ClearTimer(PrimaryWeapon->reloadTimerHandle); } void AEndlessVendettaCharacter::EquipSecondary() { + FActorSpawnParameters spawnParams; + spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; + FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true); + FDetachmentTransformRules DetatchRules(EDetachmentRule::KeepWorld, false); + if (bIsReloading) return; if (IsValid(SecondaryWeapon)) { - SecondaryWeapon->Destroy(); + SecondaryWeapon->DetachFromActor(DetatchRules); + SecondaryWeapon->SetActorLocation(FVector(0,0,0), false, nullptr, ETeleportType::None); + SecondaryWeapon->SetActorHiddenInGame(true); this->GetFirstPersonCameraComponent()->SetFieldOfView(90); + UE_LOG(LogTemp, Warning, TEXT("Secondary Weapon Is Hidden: %hhd"), SecondaryWeapon->IsHidden()); SecondaryWeapon = nullptr; bHasRifle = false; + GLog->Log("Secondary Weapon Put Away"); return; } @@ -320,16 +349,24 @@ void AEndlessVendettaCharacter::EquipSecondary() if (GadgetManager->IsValidReconGadget() && GadgetManager->IsReconEquipped() && !GadgetManager->TryToUnequipRecon()) return; if (GadgetManager->IsValidCombatGadget() && GadgetManager->IsCombatEquipped() && !GadgetManager->TryToUnequipCombat()) return; - - - FActorSpawnParameters spawnParams; - spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; - FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true); - - bHasRifle = true; - AActor* SecondaryWeaponActor = GetWorld()->SpawnActor(SecondaryWeaponClass, spawnParams); + + if(!IsValid(SecondaryWeapon)) + { + bHasRifle = true; + if(!bIsSecondaryWeaponCreated) + { + SecondaryWeaponActor = GetWorld()->SpawnActor(SecondaryWeaponClass, spawnParams); + SecondaryWeaponActor->AttachToComponent(Mesh1P, AttachmentRules, FName("GripPoint")); + SecondaryWeapon = Cast(SecondaryWeaponActor); + SecondaryWeapon->SetActorHiddenInGame(false); + bIsSecondaryWeaponCreated = true; + } + //UE_LOG(LogTemp, Warning, TEXT("Secondary Weapon Is Hidden: %hhd"), SecondaryWeapon->IsHidden()); + GLog->Log("Secondary Weapon Equipped"); + } SecondaryWeaponActor->AttachToComponent(Mesh1P, AttachmentRules, FName("GripPoint")); SecondaryWeapon = Cast(SecondaryWeaponActor); + SecondaryWeapon->SetActorHiddenInGame(false); GetWorldTimerManager().ClearTimer(SecondaryWeapon->reloadTimerHandle); } diff --git a/EndlessVendetta/Source/EndlessVendetta/EndlessVendettaCharacter.h b/EndlessVendetta/Source/EndlessVendetta/EndlessVendettaCharacter.h index 258c5c21..c4b70eeb 100644 --- a/EndlessVendetta/Source/EndlessVendetta/EndlessVendettaCharacter.h +++ b/EndlessVendetta/Source/EndlessVendetta/EndlessVendettaCharacter.h @@ -81,6 +81,11 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Stats") float DefaultHealth = 100.0f; + AActor* PrimaryWeaponActor; + AActor* SecondaryWeaponActor; + bool bIsPrimaryWeaponCreated = false; + bool bIsSecondaryWeaponCreated = false; + protected: virtual void BeginPlay() override; virtual void Tick(float DeltaTime) override;