UE4 C++(18):配置文件
作者:互联网
12/06/2020
文章目录
配置文件
配置文件通常以(.ini)后缀名结尾,UE4引擎自己也有配置文件,当你自己建立完项目也会有配置文件,当编译完之后,Saved文件中也会生出另一份配置文件,通常配置文件存储在Config文件夹中。
上述图片描述了UE4引擎和项目中的配置文件(Config)
配置变量名的路径
配置文件通常有自己的路径,都存储在CoreGlobals.cpp文件中
//CoreGlobals.cpp
FString GEngineIni; /* Engine ini filename */
/** Editor ini file locations - stored per engine version (shared across all projects). Migrated between versions on first run. */
FString GEditorIni; /* Editor ini filename */
/** Editor per-project ini files - stored per project. */
FString GInputIni; /* Input ini filename */
FString GGameIni; /* Game ini filename */
Saved文件夹目录下的Config是运行项目后生成的!
配置基础格式(键值对)
[SectionName]
key=value
key=value
通常配置文件由三个部分组成,SectionName,Key和Value,与函数参数相对应
基础
先从GConfig这个全局变量学起来,先进入GConfig的文件夹
//CoreGlobals.cpp
//ConfigCacheIni.h
FConfigCacheIni* GConfig = nullptr; /* Configuration database cache */
void Flush( bool Read, const FString& Filename=TEXT("") );
void LoadFile( const FString& InFilename, const FConfigFile* Fallback = NULL, const TCHAR* PlatformString = NULL );
bool GetString( const TCHAR* Section, const TCHAR* Key, FString& Value, const FString& Filename );
bool GetText( const TCHAR* Section, const TCHAR* Key, FText& Value, const FString& Filename );
- Section是中括号包裹的内容
- 获取配置文件中的键值对,即key=value
- Filename是配置文件的路径
获取配置文件信息(Get)
/*
[Core.System]
Paths=../../../Engine/Content
Paths=%GAMEDIR%Content
Paths=../../../Engine/Plugins/2D/Paper2D/Content
Paths=../../../Engine/Plugins/Developer/AnimationSharing/Content
Paths=../../../Engine/Plugins/Enterprise/DatasmithContent/Content
Paths=../../../Engine/Plugins/Lumin/MagicLeap/Content
Paths=../../../Engine/Plugins/Media/MediaCompositing/Content
Paths=../../../Engine/Plugins/Runtime/Oculus/OculusVR/Content
Paths=../../../Engine/Plugins/Runtime/PostSplashScreen/Content
Paths=../../../Engine/Plugins/Runtime/Steam/SteamVR/Content <--------- 最后输出的内容
*/
if (GConfig)
{
FString data;
bool success = GConfig->GetString(TEXT("Core.System"), TEXT("Paths"), data, GEngineIni);
if (!success)
{
GEngine->AddOnScreenDebugMessage(-1, 10.0, FColor::Red, TEXT("failure"));
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 10.0, FColor::Red, data);
}
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 10.0, FColor::Red, TEXT("GConfig not load"));
}
//GetString
bool success = GConfig->GetString(TEXT("StartupActions"), TEXT("InsertPack"), data, GGameIni);
//GetArray
UFUNCTION(BlueprintPure, Category = "Config")
static void GetArrayFromGameIni(const FString& InSection, const FString& InKey,FString& OutStringArray)
{
GConfig->GetArray(*InSection, *InKey, OutStringArray, GGameIni);
}
- GetString/Text/Int/Float 等等
- GetArray是获取相同key对应的所有value值
测试发现GGameIni不仅可以表示Saved/Config/Windows/Game.ini 也可以表示项目下的DefaultGame.ini
写入内容到配置(Set)
if (GConfig)
{
GConfig->SetString(TEXT("MapStat"), TEXT("Stat_Names"), TEXT("World Frame time"), GGameIni);
GEngine->AddOnScreenDebugMessage(-1, 10.0, FColor::Red, GGameIni);
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 10.0, FColor::Red, TEXT("GConfig not load"));
}
- SetString/Color/Vector等等
- 存储位置属于Saved/Config/Windows/Game.ini
LoadConfig/SaveConfig
UCLASS(config=Game)
class TESTFILEHELPER_API ATestSettingActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATestSettingActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(config, EditAnywhere)
FString data;
};
ATestSettingActor::ATestSettingActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
data = TEXT("hello");
if (GConfig)
{
SaveConfig();
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 100.0, FColor::Red, TEXT("GConfig not load"));
}
}
- UClass和UPROPERTY需要声明config属性,config=Game表示SaveConfig默认存储到GGameini文件中
- SaveConfig函数默认带有Flush属性
//Saved/Config/Windows/Game.ini
[/Script/TestFileHelper.TestSettingActor]
data=hello
TestFileHelper.TestSettingActor 表示模块名和类名,模块名由.build.cs文件声明
/*
[/Script/TestFileHelper.TestFileHelperCharacter]
ExampleVariable=0.5
*/
if (GConfig)
{
//SaveConfig();
GConfig->Flush(true, GGameIni);
LoadConfig(ATestFileHelperCharacter::StaticClass(), *GGameIni);
GEngine->AddOnScreenDebugMessage(-1, 100.0, FColor::Red, FString::SanitizeFloat(ExampleVariable));
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 100.0, FColor::Red, TEXT("GConfig not load"));
}
蓝图的Config属性
每个蓝图变量名有一个Config属性(见下图)
如果确定使用配置文件来初始化变量,可以在图上的路径写入键值对。
总结
配置文件可以从外部读写蓝图或者C++变量
参考资料
- UE4官网 配置文件
- 腾讯学院
标签:const,配置文件,..,TEXT,C++,UE4,GConfig,FString 来源: https://blog.csdn.net/weixin_44200074/article/details/109786566