-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveGameSubsystem.cpp
More file actions
118 lines (91 loc) · 3.31 KB
/
Copy pathSaveGameSubsystem.cpp
File metadata and controls
118 lines (91 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "SaveGameSubsystem.h"
#include "MySaveGame.h"
#include "SaveGameInterface.h"
#include "Kismet/GameplayStatics.h"
#include "Serialization/ObjectAndNameAsStringProxyArchive.h"
void USaveGameSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
// For testing purposes I'm setting the SaveSlotName and CurrentSaveGame values here.
SaveSlotName = TEXT("SaveGame01");
if (UGameplayStatics::DoesSaveGameExist(SaveSlotName, 0))
{
CurrentSaveGame = Cast<UMySaveGame>(UGameplayStatics::LoadGameFromSlot(SaveSlotName, 0));
}
else
{
CurrentSaveGame = CastChecked<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass()));
}
}
void USaveGameSubsystem::SaveGame()
{
// start with an empty file
CurrentSaveGame->SavedActors.Empty();
TArray<AActor*> SaveGameActors;
UGameplayStatics::GetAllActorsWithInterface(GetWorld(), USaveGameInterface::StaticClass(), SaveGameActors);
for (AActor* Actor : SaveGameActors)
{
FActorSaveData ActorData;
ActorData.ActorName = Actor->GetFName();
// set ActorTransform if you want ot save the location/rotation/scale of a physics actors or object
// this simple example won't be using actor transform data.
// ActorData.ActorTransform = Actor->GetActorTransform();
FMemoryWriter MyMemoryWriter(ActorData.ByteData);
FObjectAndNameAsStringProxyArchive Ar(MyMemoryWriter, true);
Ar.ArIsSaveGame = true;
Actor->Serialize(Ar);
CurrentSaveGame->SavedActors.Add(ActorData);
}
UGameplayStatics::SaveGameToSlot(CurrentSaveGame, SaveSlotName, 0);
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Save Game Successful"));
}
}
void USaveGameSubsystem::LoadGame()
{
if (UGameplayStatics::DoesSaveGameExist(SaveSlotName, 0))
{
CurrentSaveGame = Cast<UMySaveGame>(UGameplayStatics::LoadGameFromSlot(SaveSlotName, 0));
TArray<AActor*> SaveGameActors;
UGameplayStatics::GetAllActorsWithInterface(GetWorld(), USaveGameInterface::StaticClass(), SaveGameActors);
for (AActor* Actor : SaveGameActors)
{
for (FActorSaveData ActorData : CurrentSaveGame->SavedActors)
{
if (ActorData.ActorName == Actor->GetFName())
{
// set the actor trasform here if you set it in the save function
// Actor->SetActorTransform(ActorData.ActorTransform);
// We follow the same process as the save function but replace FMemoryWriter with FMemoryReader
// to get the binary back to Actor variable values
FMemoryReader MyMemoryReader(ActorData.ByteData);
FObjectAndNameAsStringProxyArchive Ar(MyMemoryReader, true);
Ar.ArIsSaveGame = true;
Actor->Serialize(Ar);
ISaveGameInterface::Execute_OnLoadGame(Actor);
break;
}
}
}
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Load Game Successful!"));
}
}
}
void USaveGameSubsystem::ResetData()
{
TArray<AActor*> SaveGameActors;
UGameplayStatics::GetAllActorsWithInterface(GetWorld(), USaveGameInterface::StaticClass(), SaveGameActors);
for (AActor* Actor : SaveGameActors)
{
ISaveGameInterface::Execute_OnResetData(Actor);
}
CurrentSaveGame->SavedActors.Empty();
UGameplayStatics::SaveGameToSlot(CurrentSaveGame, SaveSlotName, 0);
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Data Reset Successful!"));
}
}