UE4 game save

If you need to pause the game due to some things during the running of the game, you need to save the current progress of the game, and you can play it again when you open it next time. What we mainly save here is. All that is needed is a game saving mechanism. UE4 provides such a mechanism.

  1. First create a new class inherited from SaveGame, named FirstSaveGame
    insert image description here
  2. Open VS2019 to edit the code
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/SaveGame.h"
#include "FirstSaveGame.generated.h"

/**
 * 用来保存游戏的结构体
 */

USTRUCT(BlueprintType)
struct FCharacterStates {
    
    
	GENERATED_BODY()

	UPROPERTY(VisibleAnywhere, Category="SaveGameData")
	float heath;//角色健康值

	UPROPERTY(VisibleAnywhere, Category = "SaveGameData")
	float maxHeath;//角色的最大健康值

	UPROPERTY(VisibleAnywhere, Category = "SaveGameData")
	float stamina;//耐力值

	UPROPERTY(VisibleAnywhere, Category = "SaveGameData")
	float maxStamina;//最大耐力值

	UPROPERTY(VisibleAnywhere, Category = "SaveGameData")
	int32 coinsCount;//金币数量

	UPROPERTY(VisibleAnywhere, Category = "SaveGameData")
	FVector loaction;//角色的位置

	UPROPERTY(VisibleAnywhere, Category = "SaveGameData")
	FRotator rotation;//角色旋转
};

UCLASS()
class FIRSTPROJECT_API UFirstSaveGame : public USaveGame
{
    
    
	GENERATED_BODY()
public:
	UFirstSaveGame();

	//Category 必须设置为Basic, 否则无法保存游戏
	UPROPERTY(VisibleAnywhere, Category = Basic)
	FString playerName = TEXT("aaa");

	UPROPERTY(VisibleAnywhere, Category = Basic)
	uint32 userIndex = 0;

	UPROPERTY(VisibleAnywhere, Category = Basic)
	FCharacterStates characterState;
};

// Fill out your copyright notice in the Description page of Project Settings.


#include "FirstSaveGame.h"

UFirstSaveGame::UFirstSaveGame() {
    
    

}
  1. Add save and load functions to AMainCharacter in the character class
	UFUNCTION(BlueprintCallable)
	void saveGame();

	UFUNCTION(BlueprintCallable)
	void loadGame();
void AMainCharacter::saveGame() {
    
    
	UFirstSaveGame* saveGameInstance = Cast<UFirstSaveGame>(UGameplayStatics::CreateSaveGameObject(UFirstSaveGame::StaticClass()));
	saveGameInstance->characterState.heath = health;
	saveGameInstance->characterState.maxHeath = maxHeath;
	saveGameInstance->characterState.stamina = stamina;
	saveGameInstance->characterState.maxStamina = maxStamina;
	saveGameInstance->characterState.coinsCount = coinsCount;
	saveGameInstance->characterState.loaction = GetActorLocation();
	saveGameInstance->characterState.rotation = GetActorRotation();

	UGameplayStatics::SaveGameToSlot(saveGameInstance, saveGameInstance->playerName, saveGameInstance->userIndex);
}

void AMainCharacter::loadGame() {
    
    
	UFirstSaveGame* loadGameInstance = Cast<UFirstSaveGame>(UGameplayStatics::CreateSaveGameObject(UFirstSaveGame::StaticClass()));
	loadGameInstance = Cast<UFirstSaveGame>(UGameplayStatics::LoadGameFromSlot(loadGameInstance->playerName, loadGameInstance->userIndex));

	health = loadGameInstance->characterState.heath;
	maxHeath = loadGameInstance->characterState.maxHeath;
	stamina = loadGameInstance->characterState.stamina;
	maxStamina = loadGameInstance->characterState.maxStamina;
	coinsCount = loadGameInstance->characterState.coinsCount;
	
	SetActorLocation(loadGameInstance->characterState.loaction);
	SetActorRotation(loadGameInstance->characterState.rotation);
}
  1. In the blueprint class of AMainCharacter, set the button o to load and p to save
    insert image description here

aaa

Guess you like

Origin blog.csdn.net/wb175208/article/details/127459763