读写配置文件(*.ini)【UE4】【C++】

以Game.ini为例

其他配置的读写看这里 CoreGlobals.h,替换下面的 GGameIni 参数即可,这些字符串保存的是对应配置文件的路径。

extern CORE_API FString GEditorIni;
extern CORE_API FString GEditorPerProjectIni;

extern CORE_API FString GCompatIni;
extern CORE_API FString GLightmassIni;
extern CORE_API FString GScalabilityIni;
extern CORE_API FString GHardwareIni;
extern CORE_API FString GInputIni;
extern CORE_API FString GGameIni;
extern CORE_API FString GGameUserSettingsIni;

写入

会写入到YourGame\Saved\Config\Windows\Game.ini

void AMyActor::WriteIniFile()
{
	if (!GConfig) return;

	//String
	GConfig->SetString(
		*VictorySection,
		TEXT("Str"),
		TEXT("HelloWorld"),
		GGameIni
	);

	//FColor
	GConfig->SetColor(
		*VictorySection,
		TEXT("Red"),
		FColor(255, 0, 0, 255),
		GGameIni
	);

	//FVector
	GConfig->SetVector(
		*VictorySection,
		TEXT("Location"),
		FVector(0, 0, 0),
		GGameIni
	);

	//FRotator
	GConfig->SetRotator(
		*VictorySection,
		TEXT("Rotation"),
		FRotator(90, 0, 0),
		GGameIni
	);

	GConfig->Flush(false, GGameIni);
}

 

读取

 

void AMyActor::ReadIniFile()
{
	FString Str;
	GConfig->GetString(
		*VictorySection,
		TEXT("Str"),
		Str,
		GGameIni
	);
	GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Blue, Str);

	FColor Color;
	GConfig->GetColor(
		*VictorySection,
		TEXT("Red"),
		Color,
		GGameIni
	);
	GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Blue, Color.ToString());

	FVector Vector;
	GConfig->GetVector(
		*VictorySection,
		TEXT("Location"),
		Vector,
		GGameIni
	);
	GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Blue, Vector.ToString());

	FRotator Rotator;
	GConfig->GetRotator(
		*VictorySection,
		TEXT("Rotation"),
		Rotator,
		GGameIni
	);

	GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Blue, Rotator.ToString());
}

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值