[UE] Read and write configuration files ue read and write 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

insert image description here

Here we choose the Blueprint Function Library type

2. Write code

2.1 header file h file

insert image description here

// 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

insert image description here

// 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 GConfigit is packaged by UE, and the last parameter GGameIniis the file that UE has created for us. The specific path is as follows:
insert image description here

3.1 write ini

insert image description here
The result after running:
insert image description here

3.2 read ini

insert image description here

4. Read the ini data after packaging

4.1 Game.ini when opening the Editor

insert image description here

4.2 Packaged Game.ini

insert image description here

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.

Guess you like

Origin blog.csdn.net/weixin_46840974/article/details/126182948