在游戏运行过程中由于某些事情需要暂停一下游戏,那就需要把游戏当前的进度保存起来,下次打开的时候还可以再次玩,我们这里主要保存的就是。所有需要有一个游戏保存的机制。UE4提供了这样的机制。文章来源:https://www.toymoban.com/news/detail-516950.html
- 首先新建一个类继承在SaveGame,命名为FirstSaveGame
- 打开VS2019编辑代码
// 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() {
}
- 在角色类中AMainCharacter中添加保存和加载函数
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);
}
- 在AMainCharacter的蓝图类中,设置设置按键o加载和p保存
aaa文章来源地址https://www.toymoban.com/news/detail-516950.html
到了这里,关于UE4游戏保存的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!