Save a text file using Blueprints

Hi,

I’m working on a content-only project, and I need to save an array of strings to a text file locally.

I know this is possible with Rama’s plug-in as I have tried it, however as I understand it, I can’t use these plug-ins in a content only game when packaged? I get this error message when the plug-in is enabled and I try package the game:

14895-ramaplugin.png

Is there any other way to do this through blueprints without a plug-in, or how should I be doing it?

Just add these codes to your project code, and then simply call it in the blueprint.

lol screenshots with code; ))

Thanks yRezaei, excuse my ignorance on this, but will this will work if I am working on a blueprint only project? i.e. no code. Do I still have a .h file for my project either way?

The base class and functions for writing text into a file exist in UE4. But unfortunately there is no blueprint callable function to access them. I am afraid, this is the only way, as far as I know.

Just found this amazingly useful! Thanks so much, yRazaei!

Thought I’d post my code to save anyone who needs this from having to type it all out again.
Cant seem to reply to the answer for some reason but all credit goes to yRazaei (except maybe for the typing!)

hope thats ok.

WriteToFile.h

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

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "Paths.h"
#include "WriteToFile.generated.h"

/**
 * 
 */
UCLASS()
class UWriteToFile : public UBlueprintFunctionLibrary
{
	GENERATED_UCLASS_BODY()
	
public:
	UFUNCTION(BlueprintCallable, meta = (HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject", FriendlyName = "File-IO"), Category = "WriteToFile")
		static bool FileIO__SaveStringTextToFile(FString SaveDirectory, FString fileName, FString SaveText, bool AllowOverWriting);
	
};

WriteToFile.cpp

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

#include "LeapExamples.h"
#include "WriteToFile.h"

UWriteToFile::UWriteToFile(const class FObjectInitializer& PCIP)
	:Super(PCIP)
{

}

bool UWriteToFile::FileIO__SaveStringTextToFile(FString SaveDirectory, FString fileName, FString SaveText, bool AllowOverWriting)
{
	FString path;
	path = FPaths::GameDir();
	path += "/my_data";

	if (!FPlatformFileManager::Get().GetPlatformFile().DirectoryExists(*path))
	{
		FPlatformFileManager::Get().GetPlatformFile().CreateDirectory(*path);
		if (!FPlatformFileManager::Get().GetPlatformFile().DirectoryExists(*path))
		{
			return false;
		}
	}

	path += "/";
	path += fileName;

	if (!AllowOverWriting)
	{
		if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*path))
		{
			return false;
		}
	}
	
	return FFileHelper::SaveStringToFile(SaveText, *path);
}

Could I get a bit more dumbed down version on how to execute this?

Where do I go to get to these files and add this text?

Hey, I’m really interested in this but I need a bit more in-depth explanation.

I have done:

File > Add Code

Created the WriteToFile.cpp / .h

Copied and pasted the text, saved.

Went in to Unreal and “Refresh Visual Studio”.

Did I miss a compile somewhere? I assume the blueprint node ‘UWriteToFile’ should now appear?

This is very helpfull for me but sadly I can’t get it to work after packaging. :frowning:
Could someone help me?

What is the problem you are having?

It won’t work if you just copied and pasted, note that "#include “leapExamples.h” etc. should be replaced with your own project name, have you done this?

Well during runtime in the editor the script does save my .txt file. But after packaging it doesn’t

I think the issue is because of the folder where the file should be saved.
Does “path = FPaths::GameDir();” work for packaged games?

Check this, works for me: