UE reads and writes ini files
foreword
UE has encapsulated the ini read and write functions, we only need a few simple steps to read and write ini files
1. Create a new C++ file
Here we choose the Blueprint Function Library type
2. Write code
2.1 header file h file
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "RwTxtFiles.generated.h"
/**
*
*/
UCLASS()
class SKILLDEMO_API URwTxtFiles : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Read and Write File")
static void ReadIniValue(FString Section,FString inKey, FString& outValue);
UFUNCTION(BlueprintCallable, Category = "Read and Write File")
static bool WriteIni(FString newSection, FString newKey, FString newValue);
};
2.2 cpp files
// Fill out your copyright notice in the Description page of Project Settings.
#include "RwTxtFiles.h"
#include "FileHelpers.h"
void URwTxtFiles::ReadIniValue(FString Sectoin, FString inKey, FString& outValue)
{
if (!GConfig)
return;
GConfig->GetString(*Sectoin, *inKey, outValue, GGameIni);
}
bool URwTxtFiles::WriteIni(FString newSection, FString newKey, FString newValue)
{
if(!GConfig)
return false;
GConfig->SetString(*newSection,*newKey,*newValue,GGameIni);
return true
}
3. Function description
Now GConfig
it is packaged by UE, and the last parameter GGameIni
is the file that UE has created for us. The specific path is as follows:
3.1 write ini
The result after running:
3.2 read ini
4. Read the ini data after packaging
4.1 Game.ini when opening the Editor
4.2 Packaged Game.ini
4.3 Access Game.ini
As long as the 4.1 file is copied to 4.2 and overwritten, we can access our ini value in the release file.