【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;
    }
    

    image

posted @ 2021-05-14 15:49  砥才人  阅读(2038)  评论(0编辑  收藏  举报