【UE4 C++】 SaveGame 存档/读档
创建 SaveGame 类
-
继承自 USaveGame
UCLASS() class TIPS_API USimpleSaveGame : public USaveGame { GENERATED_BODY() public: UPROPERTY(EditAnywhere) FString PlayerName; UPROPERTY(EditAnywhere) float PlayerHealth = 0.f; UPROPERTY(EditAnywhere) FTransform PlayerTrans; };
存档/读档
-
存档
bool ADataDriveActor::SaveGame(const FString& Name, float Health, FTransform Transform) { USimpleSaveGame* SaveGameIns = Cast<USimpleSaveGame>(UGameplayStatics::LoadGameFromSlot(TEXT("SaveGame"), 0)); if (!SaveGameIns) { SaveGameIns = Cast<USimpleSaveGame>(UGameplayStatics::CreateSaveGameObject(USimpleSaveGame::StaticClass())); } SaveGameIns->PlayerName = Name; SaveGameIns->PlayerHealth = Health; SaveGameIns->PlayerTrans = Transform; return UGameplayStatics::SaveGameToSlot(SaveGameIns, TEXT("SaveGame"), 0); }
-
读档
bool ADataDriveActor::LoadSaveGame(FString& Name, float& Health, FTransform& Transform) { USimpleSaveGame* SaveGameIns = Cast<USimpleSaveGame>(UGameplayStatics::LoadGameFromSlot(TEXT("SaveGame"), 0)); if (!SaveGameIns) { return false; } Name = SaveGameIns->PlayerName; Health = SaveGameIns->PlayerHealth; Transform = SaveGameIns->PlayerTrans; UE_LOG(LogTemp, Warning, TEXT("%s\t %f\n %s"), *Name, Health, *Transform.ToString()); return true; }
作者:砥才人
出处:https://www.cnblogs.com/shiroe
本系列文章为笔者整理原创,只发表在博客园上,欢迎分享本文链接,如需转载,请注明出处!