IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Sweet Snippet 之 PlayerPrefs for UE4 -> 正文阅读

[游戏开发]Sweet Snippet 之 PlayerPrefs for UE4

在 Unity 中进行本地存储,我们一般会用到 PlayerPrefs,而在 UE4 中,我们一般会使用 USaveGame,不过 USaveGame 在使用上和 PlayerPrefs 相差较大,这里给出一个 UE4 的 PlayerPrefs 实现,原理上仅是对 USaveGame 做了进一步的封装

  • 首先我们继承 USaveGame 创建 UPlayerPrefsSaveGame 类型
#include "CoreMinimal.h"
#include "GameFramework/SaveGame.h"
#include "PlayerPrefsSaveGame.generated.h"

class PlayerPrefs;

UCLASS()
class UPlayerPrefsSaveGame : public USaveGame
{
	GENERATED_BODY()

protected:
	UPROPERTY()
    TMap<FString, int> IntMap;
	
	UPROPERTY()
    TMap<FString, float> FloatMap;

	UPROPERTY()
	TMap<FString, FString> StringMap;

	friend class PlayerPrefs;
};
  • 接着就是 PlayerPrefs 类型了,提供了和 Unity 中 PlayerPrefs 基本一致的接口
#include "CoreMinimal.h"
#include "PlayerPrefsSaveGame.h"

class PlayerPrefs
{
public:
	void Init();
	void Release();
	void Update(float DeltaTime);

	// removes all key values from the preferences
	void DeleteAll();
    // removes key from the preferences, if key does not exist, DeleteKey has no impact
	void DeleteKey(const FString& Key);
	// returns the value corresponding to key in the preference file if it exists
	float GetFloat(const FString& Key, float Default = 0);
	// returns the value corresponding to key in the preference file if it exists
	int GetInt(const FString& Key, int Default = 0);
	// returns the value corresponding to key in the preference file if it exists
	FString GetString(const FString& Key, const FString& Default = TEXT(""));
	// returns the value corresponding to key in the preference file if it exists
	bool GetBool(const FString& Key, bool Default = false);
	// returns true if the given key exists in preference, otherwise returns false
	bool HasKey(const FString& Key);
	// writes all modified preferences to disk
	void Save();
	// sets the float value of the preference identified by the given key
	void SetFloat(const FString& Key, float Value);
	// sets a single integer value for the preference identified by the given key
	void SetInt(const FString& Key, int Value);
	// sets a single string value for the preference identified by the given key
	void SetString(const FString& Key, const FString& Value);
	// sets a single bool value for the preference identified by the given key
	void SetBool(const FString& Key, bool Value);

private:
	UPlayerPrefsSaveGame* mPlayerPrefsSaveGame{ nullptr };
	bool mPlayerPrefsDirty{ false };
	float mPlayerPrefsUpdateTime{ 0 };
	float mPlayerPrefsUpdateInterval{ 5 };
};

值得提到的一点是 PlayerPrefs 主动存储的实现方式,代码中除了释放 PlayerPrefs 时会做一次主动存储以外,另外还使用了一个脏标记(mPlayerPrefsDirty)来定时的检查是否要进行主动存储

#include "PlayerPrefs.h"
#include "Kismet/GameplayStatics.h"

namespace 
{
	FString PlayerPrefsSaveGameSlotName = TEXT("PlayerPrefs");
}

void PlayerPrefs::Init()
{
	check(!mPlayerPrefsSaveGame);

	auto SavedGame = UGameplayStatics::LoadGameFromSlot(PlayerPrefsSaveGameSlotName, 0);
	mPlayerPrefsSaveGame = Cast<UPlayerPrefsSaveGame>(SavedGame);

	if (!mPlayerPrefsSaveGame)
	{
		mPlayerPrefsSaveGame = Cast<UPlayerPrefsSaveGame>(UGameplayStatics::CreateSaveGameObject(UPlayerPrefsSaveGame::StaticClass()));
		bool Saved = UGameplayStatics::SaveGameToSlot(mPlayerPrefsSaveGame, PlayerPrefsSaveGameSlotName, 0);
		if (!Saved)
		{
			UE_LOG(LogTemp, Error, TEXT("[PlayerPrefs]Error to save PlayerPrefs ..."));
			return;
		}
	}

	if (!mPlayerPrefsSaveGame)
	{
		UE_LOG(LogTemp, Error, TEXT("[PlayerPrefs]Error to init PlayerPrefs ..."));
		return;
	}

	// add GC reference
	mPlayerPrefsSaveGame->AddToRoot();
}

void PlayerPrefs::Release()
{
	Save();

	if (mPlayerPrefsSaveGame)
	{
		// remove GC reference
		mPlayerPrefsSaveGame->RemoveFromRoot();
	}

	mPlayerPrefsSaveGame = nullptr;
}

void PlayerPrefs::Update(float DeltaTime)
{
	mPlayerPrefsUpdateTime += DeltaTime;
	if (mPlayerPrefsUpdateTime >= mPlayerPrefsUpdateInterval)
	{
		mPlayerPrefsUpdateTime = 0;
		if (mPlayerPrefsDirty)
		{
			Save();
		}
	}
}

// removes all key values from the preferences
void PlayerPrefs::DeleteAll()
{
	if (mPlayerPrefsSaveGame)
	{
		mPlayerPrefsSaveGame->IntMap.Empty();
		mPlayerPrefsSaveGame->FloatMap.Empty();
		mPlayerPrefsSaveGame->StringMap.Empty();
		mPlayerPrefsDirty = true;
	}
}

// removes key from the preferences, if key does not exist, DeleteKey has no impact
void PlayerPrefs::DeleteKey(const FString& Key)
{
	if (mPlayerPrefsSaveGame)
	{
		bool Ret = mPlayerPrefsSaveGame->IntMap.Remove(Key) > 0;
		Ret |= mPlayerPrefsSaveGame->FloatMap.Remove(Key) > 0;
		Ret |= mPlayerPrefsSaveGame->StringMap.Remove(Key) > 0;
		
		if (Ret)
		{
			mPlayerPrefsDirty = true;
		}
	}
}

// returns the value corresponding to key in the preference file if it exists
float PlayerPrefs::GetFloat(const FString& Key, float Default)
{
	if (mPlayerPrefsSaveGame)
	{
		if (mPlayerPrefsSaveGame->FloatMap.Contains(Key))
		{
			return mPlayerPrefsSaveGame->FloatMap[Key];
		}
	}

	return Default;
}

// returns the value corresponding to key in the preference file if it exists
int PlayerPrefs::GetInt(const FString& Key, int Default)
{
	if (mPlayerPrefsSaveGame)
	{
		if (mPlayerPrefsSaveGame->IntMap.Contains(Key))
		{
			return mPlayerPrefsSaveGame->IntMap[Key];
		}
	}

	return Default;
}

// returns the value corresponding to key in the preference file if it exists
FString PlayerPrefs::GetString(const FString& Key, const FString& Default)
{
	if (mPlayerPrefsSaveGame)
	{
		if (mPlayerPrefsSaveGame->StringMap.Contains(Key))
		{
			return mPlayerPrefsSaveGame->StringMap[Key];
		}
	}

	return Default;
}

// returns the value corresponding to key in the preference file if it exists
bool PlayerPrefs::GetBool(const FString& Key, bool Default)
{
	auto DefaultInt = Default ? 1 : 0;
	auto Value = GetInt(Key, DefaultInt);
	return Value > 0;
}

// returns true if the given key exists in preference, otherwise returns false
bool PlayerPrefs::HasKey(const FString& Key)
{
	if (mPlayerPrefsSaveGame)
	{
		bool Ret = mPlayerPrefsSaveGame->IntMap.Contains(Key) ||
		           mPlayerPrefsSaveGame->FloatMap.Contains(Key) ||
		           mPlayerPrefsSaveGame->StringMap.Contains(Key);
	    
		return Ret;
	}

	return false;
}

// writes all modified preferences to disk
void PlayerPrefs::Save()
{
	if (mPlayerPrefsSaveGame)
	{
		bool Saved = UGameplayStatics::SaveGameToSlot(mPlayerPrefsSaveGame, PlayerPrefsSaveGameSlotName, 0);
		if (!Saved)
		{
			UE_LOG(LogTemp, Error, TEXT("[PlayerPrefs]Error to save PlayerPrefs ..."));
		}
		
		mPlayerPrefsDirty = false;
	}
}

// sets the float value of the preference identified by the given key
void PlayerPrefs::SetFloat(const FString& Key, float Value)
{
	if (mPlayerPrefsSaveGame)
	{
		if (mPlayerPrefsSaveGame->FloatMap.Contains(Key))
		{
			mPlayerPrefsSaveGame->FloatMap[Key] = Value;
		}
		else
		{
			mPlayerPrefsSaveGame->FloatMap.Add(Key, Value);
		}

		mPlayerPrefsDirty = true;
	}
}

// sets a single integer value for the preference identified by the given key
void PlayerPrefs::SetInt(const FString& Key, int Value)
{
	if (mPlayerPrefsSaveGame)
	{
		if (mPlayerPrefsSaveGame->IntMap.Contains(Key))
		{
			mPlayerPrefsSaveGame->IntMap[Key] = Value;
		}
		else
		{
			mPlayerPrefsSaveGame->IntMap.Add(Key, Value);
		}

		mPlayerPrefsDirty = true;
	}
}

// sets a single string value for the preference identified by the given key
void PlayerPrefs::SetString(const FString& Key, const FString& Value)
{
	if (mPlayerPrefsSaveGame)
	{
		if (mPlayerPrefsSaveGame->StringMap.Contains(Key))
		{
			mPlayerPrefsSaveGame->StringMap[Key] = Value;
		}
		else
		{
			mPlayerPrefsSaveGame->StringMap.Add(Key, Value);
		}

		mPlayerPrefsDirty = true;
	}
}

// sets a single bool value for the preference identified by the given key
void PlayerPrefs::SetBool(const FString& Key, bool Value)
{
	if (mPlayerPrefsSaveGame)
	{
		if (mPlayerPrefsSaveGame->IntMap.Contains(Key))
		{
			mPlayerPrefsSaveGame->IntMap[Key] = Value ? 1 : 0;
		}
		else
		{
			mPlayerPrefsSaveGame->IntMap.Add(Key, Value ? 1 : 0);
		}

		mPlayerPrefsDirty = true;
	}
}

值得注意的一点是,虽然实现上使用了不同类型的映射表(TMap)来存储不同类型的数值,但程序概念上表键(Key)是相通的,不同映射表之间不存在重复的表键(Key)

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-08-19 12:23:28  更:2021-08-19 12:24:34 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 4:55:20-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码