简介

常见存储路径

  • \Engine\Config\
  • \Engine\Saved\Config\ (运行后生成)
  • [ProjectName]\Config\
  • [ProjectName]\Saved\Config (运行后生成)
  • 打包文件
    • Development/Debug 打包目录\项目名称\Saved\Config\WindowsNoEditor
    • Shipping C:\Users\用户名\AppData\Local\项目名称\Saved\Config\WindowsNoEditor

常见配置文件

  • DefaultEditor.ini
  • DefaultEngine.ini
  • DefaultGame.ini
  • DefaultInput.ini

格式

  • 文件格式: 文件名.ini

  • 文本格式,分段和键-值对

    [配置标题Section]
    key1 = value1
    key2 = value2
    -ConsoleKeys=Tilde
    +ConsoleKeys=Tilde
    Bindings=(Name="Q",Command="Foo")
    .Bindings=(Name="Q",Command="Bar")
    ; 这是一条注释

指定可配置变量 UCLASS()、UPROPERTY()

简单使用

  • Config=Game 默认路径为 [ProjectName]\Saved\Config\Windows\Game.ini

  • 注意配置文件的 section 设置 [/Script/ModuleName.ExampleClass]

  • SaveConfig(CPF_Config, *ConfPath); 可以用于将变量值存到配置文件中

  • ReloadConfig(NULL, *ConfPath) 可以从配置文件中读取值,实时

  • LoadConfig() 可以从配置文件中读取值

    UCLASS(Config=Game)
    class TIPS_API AConfigActor : public AActor
    {
    // 省略 UPROPERTY(Config, BlueprintReadWrite)
    float Examplefloat; UPROPERTY(Config, BlueprintReadWrite)
    TArray<FString> ExampleArray;
    };
    void AConfigActor::BeginPlay()
    {
    Super::BeginPlay();
    if (GConfig)
    {
    GConfig->Flush(true, GGameIni); //刷新缓存
    LoadConfig(AConfigActor::StaticClass(), *GGameIni);
    //ReloadConfig(this->GetClass(), *ConfigPath);
    //SaveConfig();
    }
    }

  • 测试时,派生蓝图类,section 设为类路径也可以

  • 蓝图中添加的变量也可以使用配置文件,需要勾选 Config Variable

    [/Game/CPPFunction/DataDrive/BP_ConfigActor.BP_ConfigActor_C]
    ExampleArray=1
    ExampleArray=2
    ExampleArray=20
    ConfigVector=(X=10.000000,Y=20.000000,Z=30.000000)

自定义配置文件

  • 创建 UDeveloperSettings 派生类

  • 支持 ProjectSettings 修改

  • 生成路径为 [ProjectName]\Config\DefaultGame.ini

    UCLASS(Config = DataDrivenSettings, defaultconfig)
    class TIPS_API UDataDrivenSettings : public UDeveloperSettings
    {
    GENERATED_BODY() public: /** Gets the settings container name for the settings, either Project or Editor */
    virtual FName GetContainerName() const override { return TEXT("Project"); }
    /** Gets the category for the settings, some high level grouping like, Editor, Engine, Game...etc. */
    virtual FName GetCategoryName() const override { return TEXT("DataDrivenSettings"); }
    /** The unique name for your section of settings, uses the class's FName. */
    virtual FName GetSectionName() const override { return TEXT("DataDrivenSettings"); } public:
    UPROPERTY(Config, EditAnywhere, BlueprintReadWrite)
    FString PlayerName="Default"; UPROPERTY(Config, EditAnywhere, BlueprintReadWrite)
    float PlayerHealth = 100.0f;
    };


直接读写配置文件

  • GConfig 类型为 FConfigCacheIni* ,下面为 FConfigCacheIni 的一些方法,更详细的接口可以查看文章尾部的附录

    void LoadFile( const FString& InFilename, const FConfigFile* Fallback = NULL, const TCHAR* PlatformString = NULL );
    void SetFile( const FString& InFilename, const FConfigFile* NewConfigFile );
    void UnloadFile( const FString& Filename );
    void Detach( const FString& Filename ); 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 );
    bool GetSection( const TCHAR* Section, TArray<FString>& Result, const FString& Filename );
    bool DoesSectionExist(const TCHAR* Section, const FString& Filename); void SetString( const TCHAR* Section, const TCHAR* Key, const TCHAR* Value, const FString& Filename );
    void SetText( const TCHAR* Section, const TCHAR* Key, const FText& Value, const FString& Filename );
    bool RemoveKey( const TCHAR* Section, const TCHAR* Key, const FString& Filename );
    bool EmptySection( const TCHAR* Section, const FString& Filename );
    bool EmptySectionsMatchingString( const TCHAR* SectionString, const FString& Filename ); FString GetStr(const TCHAR * Section, const TCHAR * Key, const FString & Filename);
    bool GetInt(const TCHAR * Section, const TCHAR * Key, int32 & Value, const FString & Filename);
    bool GetFloat(const TCHAR * Section, const TCHAR * Key, float& Value, const FString & Filename);
    bool GetDouble(const TCHAR * Section, const TCHAR * Key, double& Value, const FString & Filename);
    bool GetBool(const TCHAR * Section, const TCHAR * Key, bool& Value, const FString & Filename);
    int32 GetArray(const TCHAR * Section, const TCHAR * Key, TArray<FString>&out_Arr, const FString & Filename);

读取

读取系统自带的配置属性

[/Script/EngineSettings.GameMapsSettings]
GameDefaultMap=/Game/CPPFunction/MultiThread/Map_multithread.Map_multithread
EditorStartupMap=/Game/CPPFunction/MultiThread/Map_multithread.Map_multithread
if (GConfig)
{
FString GameDefaultMap;
bool success = GConfig->GetString(TEXT("/Script/EngineSettings.GameMapsSettings"), TEXT("GameDefaultMap"), GameDefaultMap, GEngineIni);
if (success)
{
UE_LOG(LogTemp, Warning, TEXT(" GameDefaultMap: %s"), *GameDefaultMap);
return true;
}
}
return false; // >> LogTemp: Warning: GameDefaultMap: /Game/CPPFunction/MultiThread/Map_multithread.Map_multithread

读取自定义配置属性

[DarkSoul]
Name=Frank
DeadTimes=100
if (GConfig)
{
FString Name;
int32 DeadTimes;
bool success = GConfig->GetString(TEXT("DarkSoul"), TEXT("Name"), Name, GGameIni);
success &= GConfig->GetInt(TEXT("DarkSoul"), TEXT("DeadTimes"), DeadTimes, GGameIni);
if (success)
{
UE_LOG(LogTemp, Warning, TEXT(" Name: %s, DeadTimes: %d"), *Name, DeadTimes);
return true;
}
}
return false;
  • GGameIni 默认路径:[ProjectName]\Saved\Config\Windows\Game.ini

    GGameIni 也可以读取 [ProjectName]\Config\DefaultGame.ini

读取自定义 SympleConfig.ini

FConfigFile CustomConfigFile;
FString Path = FPaths::ProjectConfigDir() / TEXT("SympleConfig.ini");
CustomConfigFile.Read(Path); FString ProjectID;
bool success = CustomConfigFile.GetString(TEXT("ProjectSettings"), TEXT("ProjectID"), ProjectID);
if (success)
{
UE_LOG(LogTemp, Warning, TEXT(" ProjectID: %s"), *ProjectID);
return true;
}
return false;

写入

  • Set

    if (GConfig)
    {
    FString Name = "Mike";
    TArray<FString> Buff;
    Buff.Add(TEXT("Fire"));
    Buff.Add(TEXT("Strong"));
    Buff.Add(TEXT("Speed")); GConfig->SetString(TEXT("DarkSoul"), TEXT("Name"), *Name, GGameIni);
    GConfig->SetArray(TEXT("DarkSoul"), TEXT("Buff"), Buff, GGameIni);
    GConfig->SetVector(TEXT("DarkSoul"), TEXT("Name"), GetActorLocation(), GGameIni);
    }

附录

系统默认配置文件

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 */
FString GEditorKeyBindingsIni; /* Editor Key Bindings ini file */
FString GEditorLayoutIni; /* Editor UI Layout ini filename */
FString GEditorSettingsIni; /* Editor Settings ini filename */ /** Editor per-project ini files - stored per project. */
FString GEditorPerProjectIni; /* Editor User Settings ini filename */ FString GCompatIni;
FString GLightmassIni; /* Lightmass settings ini filename */
FString GScalabilityIni; /* Scalability settings ini filename */
FString GHardwareIni; /* Hardware ini filename */
FString GInputIni; /* Input ini filename */
FString GGameIni; /* Game ini filename */
FString GGameUserSettingsIni; /* User Game Settings ini filename */
FString GRuntimeOptionsIni; /* Runtime Options ini filename */
FString GInstallBundleIni; /* Install Bundle ini filename*/
FString GDeviceProfilesIni; /* Runtime DeviceProfiles ini filename - use LoadLocalIni for other platforms'

FConfigCacheIni 获取配置文件中数据的方法

// Derived functions.
FString GetStr
(
const TCHAR* Section,
const TCHAR* Key,
const FString& Filename
);
bool GetInt
(
const TCHAR* Section,
const TCHAR* Key,
int32& Value,
const FString& Filename
);
bool GetFloat
(
const TCHAR* Section,
const TCHAR* Key,
float& Value,
const FString& Filename
);
bool GetDouble
(
const TCHAR* Section,
const TCHAR* Key,
double& Value,
const FString& Filename
);
bool GetBool
(
const TCHAR* Section,
const TCHAR* Key,
bool& Value,
const FString& Filename
);
int32 GetArray
(
const TCHAR* Section,
const TCHAR* Key,
TArray<FString>& out_Arr,
const FString& Filename
);
/** Loads a "delimited" list of strings
* @param Section - Section of the ini file to load from
* @param Key - The key in the section of the ini file to load
* @param out_Arr - Array to load into
* @param Filename - Ini file to load from
*/
int32 GetSingleLineArray
(
const TCHAR* Section,
const TCHAR* Key,
TArray<FString>& out_Arr,
const FString& Filename
);
bool GetColor
(
const TCHAR* Section,
const TCHAR* Key,
FColor& Value,
const FString& Filename
);
bool GetVector2D(
const TCHAR* Section,
const TCHAR* Key,
FVector2D& Value,
const FString& Filename);
bool GetVector
(
const TCHAR* Section,
const TCHAR* Key,
FVector& Value,
const FString& Filename
);
bool GetVector4
(
const TCHAR* Section,
const TCHAR* Key,
FVector4& Value,
const FString& Filename
);
bool GetRotator
(
const TCHAR* Section,
const TCHAR* Key,
FRotator& Value,
const FString& Filename
); void SetInt
(
const TCHAR* Section,
const TCHAR* Key,
int32 Value,
const FString& Filename
);
void SetFloat
(
const TCHAR* Section,
const TCHAR* Key,
float Value,
const FString& Filename
);
void SetDouble
(
const TCHAR* Section,
const TCHAR* Key,
double Value,
const FString& Filename
);
void SetBool
(
const TCHAR* Section,
const TCHAR* Key,
bool Value,
const FString& Filename
);
void SetArray
(
const TCHAR* Section,
const TCHAR* Key,
const TArray<FString>& Value,
const FString& Filename
); /** Saves a "delimited" list of strings
* @param Section - Section of the ini file to save to
* @param Key - The key in the section of the ini file to save
* @param out_Arr - Array to save from
* @param Filename - Ini file to save to
*/
void SetSingleLineArray
(
const TCHAR* Section,
const TCHAR* Key,
const TArray<FString>& In_Arr,
const FString& Filename
);
void SetColor
(
const TCHAR* Section,
const TCHAR* Key,
FColor Value,
const FString& Filename
);
void SetVector2D(
const TCHAR* Section,
const TCHAR* Key,
FVector2D Value,
const FString& Filename);
void SetVector
(
const TCHAR* Section,
const TCHAR* Key,
FVector Value,
const FString& Filename
);
void SetVector4
(
const TCHAR* Section,
const TCHAR* Key,
const FVector4& Value,
const FString& Filename
);
void SetRotator
(
const TCHAR* Section,
const TCHAR* Key,
FRotator Value,
const FString& Filename
);

参考

【UE4 C++】 Config Settings配置文件(.ini)的更多相关文章

  1. 利用GetPrivateProfileString读取配置文件(.ini)

    利用GetPrivateProfileString读取配置文件(.ini) 配置文件中经常用到ini文件,在VC中其函数分别为: 写入.ini文件:bool WritePrivateProfileSt ...

  2. Python常用配置文件ini、json、yaml读写总结

    开发项目时,为了维护一些经常需要变更的数据,比如数据库的连接信息.请求的url.测试数据等,需要将这些数据写入配置文件,将数据和代码分离,只需要修改配置文件的参数,就可以快速完成环境的切换或者测试数据 ...

  3. python 读取配置文件ini ---ConfigParser

    Python读取ini文件需要用到 ConfigParser 模块 关于ConfigParser模块的介绍详情请参照官网解释:https://docs.python.org/2.7/library/c ...

  4. maven settings 配置文件

    maven settings 配置文件 <?xml version="1.0" encoding="UTF-8"?> <settings xm ...

  5. LabVIEW--为设备添加配置文件.ini

    需求:我同一个程序下载到两台机器人上,有些参数是不一样的,比如说服务器的ID或者端口,以及存放文件的位置,如果我每次下载之前改程序的话就非常麻烦了(虽然在程序里面是作为全局变量来存的),不利于后期的更 ...

  6. .net Core Abp See config settings - "CustomSchemaIds" for a workaround

    Swagger  See config settings - "CustomSchemaIds" for a workaround System.InvalidOperationE ...

  7. ResourceBundle类的方式来读取config.properties配置文件参数值

    //获取config.properties配置文件参数值 public static ResourceBundle resource = ResourceBundle.getBundle(" ...

  8. Webpack探索【2】--- 安装、项目初始化、webpack.config.js配置文件

    本文主要讲安装.项目初始化.webpack.config.js配置文件方面的内容.

  9. Vue Cli 3:vue.config.js配置文件

    Vue Cli 3生成的项目结构,没有build.config目录,而是使用vue.config.js来进行配置. vue.config.js 是一个可选的配置文件,如果项目的 (和 package. ...

随机推荐

  1. 性能测试工具JMeter 基础(八)—— 测试元件: 逻辑控制器之事物控制器

    事物控制器是将控制器下的所有取样器作为一个事物统计.分析 事物控制器(Transaction Controller) 事务控制器一共有两个选项: Generate parent sample:默认不勾 ...

  2. NumPy的基本操作

    1 简介 NumPy 是用于处理数组的 python 库,部分用 Python 编写,但是大多数需要快速计算的部分都是用 C 或 C ++ 编写的.它还拥有在线性代数.傅立叶变换和矩阵领域中工作的函数 ...

  3. Python - 面向对象编程 - 实战(5)

    前言 主要是针对静态方法.类方法.实例方法.类属性.实例属性的混合实战 需求 设计一个 Game 类 属性 定义一个类属性 top_score 记录游戏的历史最高分,这个属性很明显只跟游戏有关,跟实例 ...

  4. Servlet生命周期和方法

    一.五个生命周期方法,有三个很重要,初始化方法.提供服务方法和销毁方法 1.三个主要方法 2.另外两个重写的成员方法只做了解 二.生命周期详解 其中,每次刷新页面都是一次对servlet访问: 页面访 ...

  5. 清除router路由后面的参数

    清除router参数: 1.this.$router.push({ query: {}}) 2.var path = this.$route.path; //获取路由路径    this.$route ...

  6. [第九篇]——Docker 镜像使用之Spring Cloud直播商城 b2b2c电子商务技术总结

    Docker 镜像使用 当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载. 下面我们来学习: 1.管理 ...

  7. git 要求密码的解决方法:【生成gitLab公钥】:以及如何配置GitLab中的SSH key

    参考链接: https://www.cnblogs.com/yjlch1016/p/9692840.html https://blog.csdn.net/u011925641/article/deta ...

  8. 推荐一个pycharm验证xpath表达式的插件XPathView +​ XSLT

    使用Appium进行自动化测试,使用xpath元素定位,想验证xpath定位是否正确,可以使用在线的xpath验证网站,也可以使用这次推荐的插件XPathView +​ XSLT.

  9. python继承细节

    不要子类化内置类型 内置类型(由C语言编写)不会调用用户定义的类覆盖的特殊方法. 例如,子类化dict作为测验: class DoppeDict(dict): def __setitem__(self ...

  10. [转载]Windows 2008多用户同时远程登陆配置方法

    有些朋友需要在在使用Windows 2008远程登录功能时,进行多用户登录,那么就可以采用以下配置方法: 首先要启用远程桌面这一功能:右击"我的电脑"→ 属性 → 远程配置 → 远 ...