Implemented Loading and Saving Player Spawn Transform in Open World

This commit is contained in:
Rafal Swierczek 2024-02-04 17:59:01 +00:00
parent a2e80dcdf6
commit ce4f968151
7 changed files with 62 additions and 15 deletions

View File

@ -9,10 +9,12 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="dfa3053d-1d51-4dad-9270-4c17e086f627" name="Changes" comment=""> <list default="true" id="dfa3053d-1d51-4dad-9270-4c17e086f627" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/.idea.EndlessVendetta/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.EndlessVendetta/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/.idea.EndlessVendetta/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.EndlessVendetta/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Content/Levels/TempOpenWorld.umap" beforeDir="false" afterPath="$PROJECT_DIR$/Content/Levels/TempOpenWorld.umap" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Content/StarterContent/Architecture/Floor_400x400.uasset" beforeDir="false" afterPath="$PROJECT_DIR$/Content/StarterContent/Architecture/Floor_400x400.uasset" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Content/StarterContent/Architecture/Floor_400x400.uasset" beforeDir="false" afterPath="$PROJECT_DIR$/Content/StarterContent/Architecture/Floor_400x400.uasset" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Source/EndlessVendetta/EndlessVendettaCharacter.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Source/EndlessVendetta/EndlessVendettaCharacter.cpp" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Content/StarterContent/Audio/Collapse01.uasset" beforeDir="false" afterPath="$PROJECT_DIR$/Content/StarterContent/Audio/Collapse01.uasset" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Source/EndlessVendetta/Characters/BountyHunterCharacter.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Source/EndlessVendetta/Characters/BountyHunterCharacter.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Source/EndlessVendetta/Characters/BountyHunterCharacter.h" beforeDir="false" afterPath="$PROJECT_DIR$/Source/EndlessVendetta/Characters/BountyHunterCharacter.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Source/EndlessVendetta/MainSaveGameClass.h" beforeDir="false" afterPath="$PROJECT_DIR$/Source/EndlessVendetta/MainSaveGameClass.h" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Source/EndlessVendetta/MainSaveGameClass.h" beforeDir="false" afterPath="$PROJECT_DIR$/Source/EndlessVendetta/MainSaveGameClass.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Source/EndlessVendetta/SpaceShip/LandingZone.h" beforeDir="false" afterPath="$PROJECT_DIR$/Source/EndlessVendetta/SpaceShip/LandingZone.h" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -129,7 +131,7 @@
<updated>1706900339638</updated> <updated>1706900339638</updated>
<workItem from="1706900342753" duration="2652000" /> <workItem from="1706900342753" duration="2652000" />
<workItem from="1706972179014" duration="11096000" /> <workItem from="1706972179014" duration="11096000" />
<workItem from="1707046474738" duration="2553000" /> <workItem from="1707046474738" duration="6163000" />
</task> </task>
<servers /> <servers />
</component> </component>

Binary file not shown.

View File

@ -5,14 +5,10 @@
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "EnhancedInputComponent.h" #include "EnhancedInputComponent.h"
void ABountyHunterCharacter::SpawnMainBounty() void ABountyHunterCharacter::SpawnMainBounty(UEVGameInstance* GI)
{ {
UEVGameInstance* GI = Cast<UEVGameInstance>(GetGameInstance());
if (!IsValid(GI->MainSaveGameInstanceRef)) return;
CurrentMainBountyIndex = GI->MainSaveGameInstanceRef->LastMainBountyIndexInOpenWorld; CurrentMainBountyIndex = GI->MainSaveGameInstanceRef->LastMainBountyIndexInOpenWorld;
UE_LOG(LogTemp, Warning, TEXT("Loaded current bounty as %d"), CurrentMainBountyIndex);
if (GI->MainSaveGameInstanceRef->CurrentMainBountyIndexSave > CurrentMainBountyIndex) CompleteCurrentMainBounty(GI); if (GI->MainSaveGameInstanceRef->CurrentMainBountyIndexSave > CurrentMainBountyIndex) CompleteCurrentMainBounty(GI);
if (MainBountyClasses.IsEmpty() || MainBountyClasses.Num() <= CurrentMainBountyIndex|| !IsValid(MainBountyClasses[CurrentMainBountyIndex])) if (MainBountyClasses.IsEmpty() || MainBountyClasses.Num() <= CurrentMainBountyIndex|| !IsValid(MainBountyClasses[CurrentMainBountyIndex]))
@ -49,7 +45,17 @@ void ABountyHunterCharacter::AllBountiesCompleted()
void ABountyHunterCharacter::BeginPlay() void ABountyHunterCharacter::BeginPlay()
{ {
if (UGameplayStatics::DoesSaveGameExist("MainSave", 0)) SpawnMainBounty(); UEVGameInstance* GI = Cast<UEVGameInstance>(GetGameInstance());
if (IsValid(GI->MainSaveGameInstanceRef))
{
SpawnMainBounty(GI);
FTransform SpawnLoc = GI->MainSaveGameInstanceRef->PlayerOpenWorldSpawnTransformSave;
if (SpawnLoc.GetLocation() != FVector(0, 0, 0) && UGameplayStatics::GetCurrentLevelName(GetWorld()) == OpenWorldLevelName)
{
SetActorTransform(GI->MainSaveGameInstanceRef->PlayerOpenWorldSpawnTransformSave);
Cast<APlayerController>(GetController())->SetControlRotation(GI->MainSaveGameInstanceRef->PlayerOpenWorldSpawnTransformSave.GetRotation().Rotator());
}
}
Super::BeginPlay(); Super::BeginPlay();
} }
@ -94,3 +100,31 @@ void ABountyHunterCharacter::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
} }
void ABountyHunterCharacter::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
if (UGameplayStatics::GetCurrentLevelName(GetWorld()) != OpenWorldLevelName) return;
UEVGameInstance* GI = Cast<UEVGameInstance>(GetGameInstance());
if (!IsValid(GI->MainSaveGameInstanceRef)) return;
TArray<AActor*> LandingZones;
ALandingZone* ClosestLandingZone = nullptr;
double DistToClosestLandingZone;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ALandingZone::StaticClass(), LandingZones);
for (AActor* LZ_Actor : LandingZones)
{
ALandingZone* LZ = Cast<ALandingZone>(LZ_Actor);
double DistToLZ = FVector::Dist(LZ->GetExitTransform().GetLocation(), GetActorLocation());
if (!IsValid(ClosestLandingZone))
{
ClosestLandingZone = LZ;
DistToClosestLandingZone = DistToLZ;
continue;
}
ClosestLandingZone = DistToClosestLandingZone >= DistToLZ ? LZ : ClosestLandingZone;
DistToClosestLandingZone = DistToClosestLandingZone >= DistToLZ ? DistToLZ : DistToClosestLandingZone;
}
if (!IsValid(ClosestLandingZone)) return;
GI->MainSaveGameInstanceRef->PlayerOpenWorldSpawnTransformSave = ClosestLandingZone->GetExitTransform();
UGameplayStatics::SaveGameToSlot(GI->MainSaveGameInstanceRef, "MainSave", 0);
}

View File

@ -58,7 +58,7 @@ public:
// ------------------- METHODS --------------------------------- // ------------------- METHODS ---------------------------------
private: private:
// Spawns Current Main Bounty along with its Side Bounties, and spawns its appropriate CP's based on level // Spawns Current Main Bounty along with its Side Bounties, and spawns its appropriate CP's based on level
void SpawnMainBounty(); void SpawnMainBounty(UEVGameInstance* GI);
// Collects Reward for Current Main Bounty and Increments the Main Bounty Index // Collects Reward for Current Main Bounty and Increments the Main Bounty Index
void CompleteCurrentMainBounty(UEVGameInstance* GI); void CompleteCurrentMainBounty(UEVGameInstance* GI);
@ -78,6 +78,9 @@ protected:
// Called every frame // Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
// Called When Actor destroyed or Removed from Level
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
public: public:
// Used by Final Checkpoint to always load the Level set as the Open Level in here // Used by Final Checkpoint to always load the Level set as the Open Level in here
FString GetOpenWorldLevelName() FString GetOpenWorldLevelName()

View File

@ -28,4 +28,6 @@ public:
UPROPERTY() UPROPERTY()
TSubclassOf<ABaseWeaponClass> SecondaryWeaponClassSave; TSubclassOf<ABaseWeaponClass> SecondaryWeaponClassSave;
UPROPERTY()
FTransform PlayerOpenWorldSpawnTransformSave;
}; };

View File

@ -68,4 +68,10 @@ public:
// Updates all LZ's, then runs enter ship func on player passing the correct takeoff transform // Updates all LZ's, then runs enter ship func on player passing the correct takeoff transform
void Takeoff(); void Takeoff();
// Returns Exit Transform for LZ, Used for Save System for when Player Leaves Open World
FTransform GetExitTransform()
{
return LZ_ExitTransform;
}
}; };