How to create a read/write system for txt files (C++ tutorial)

In this tutorial we will dive a little into C++ again, it won’t be anything too difficult, so even if you’re a beginner, this should be relatively easy to follow.

Before we begin, we must ask ourselves the question why would we want such a thing as a read/write ability of txt files? And that is a valid question, it does seem useless at first glance, but think of this situation: what if you could tweak variables from your game without actually patching any game files, you just edit them in the file, the rest falls in place automatically. That is one of many useful situations where such an ability would come in handy, but that functionality unfortunately isn’t exposed to Blueprints. Hence the need to make it C++ and then use it from Blueprints once it’s created.

This tutorial is based on older code from here and here, I made some modifications to it so that it runs on UE 4.22 😀

What you’ll need:
-Unreal Engine 4 (4.22 used in the tutorial)
-Visual Studio 2017 (Community Edition used in the tutorial)

With all that said, let’s begin!

1. Create a new FPS Game Starter Kit project

2. Go to File, New C++ Class
cpp_newclass
NOTE: This will turn it into a C++ project, you can’t reverse this step.

3. Choose Blueprints Function Library as the Class you want to create
cpp_choosebpfl

4. Give it a name and make it Public (use the same name for the sake of simplicity)
cpp_public

5. Wait for it to create the necessary project files in Visual Studio (and finishes Parsing)
cpp_wait

6. Finally the code part, basically you see 2 files in Visual studio, a .cpp file and a .h file, make them look like this:
cpp_file_cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "FPS_Game_Function_Library.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"

bool UFPS_Game_Function_Library::LoadTxt(FString FileNameA, FString& SaveTextA)
{
return FFileHelper::LoadFileToString(SaveTextA, *(FPaths::ProjectDir() + FileNameA));
}

bool UFPS_Game_Function_Library::SaveTxt(FString SaveTextB, FString FileNameB)
{
return FFileHelper::SaveStringToFile(SaveTextB, *(FPaths::ProjectDir() + FileNameB));
}

cpp_file_h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "FPS_Game_Function_Library.generated.h"

/**
* 
*/
UCLASS()
class FPS_CPP_API UFPS_Game_Function_Library : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:

UFUNCTION(BlueprintPure, Category = "FPS Game CPP Nodes", meta = (Keywords = "Load Text File"))
static bool LoadTxt(FString FileNameA, FString& SaveTextA);

UFUNCTION(BlueprintCallable, Category = "FPS Game CPP Nodes", meta = (Keywords = "Save Text File"))
static bool SaveTxt(FString SaveTextB, FString FileNameB);
};

 

7. After that SaveAll, click on Game and Build
cpp_build_vs

8. Click the Compile button in UE4 as well
cpp_build_ue4
NOTE: If you can’t see any new nodes, you need to restart the editor, you can close Visual Studio at this point as well.

 

That’s it!

cpp_newnodes

You can try it out like this:
cpp_use