how to write some data to text file from V

Dear All,

how am I supposed to write some data on text file from inside of custom source file. I use the following sode snippet but it seems that my class is not invoked at all during program execution, Furthermore I don’t even want to invoke a class for that simply would like to call function which will do that.


#include "Tst_Prj.h"
#include "MyClass.h"
#include <iostream>
#include <fstream>
using namespace std;

MyClass::MyClass()
{
	std::ofstream o("C:/Hello.txt");

	o << "Hello, World
" << std::endl;
}

MyClass::~MyClass()
{
}

Regards,

Hi, did you create your MyClass object somewhere with new operator (or NewObject if it’s a UObject) ?

And don’t use constructor to do this, simply create a static function that can be use everywhere.

I hope this help you ! :slight_smile:

Actually I didn’t created it, I only declared/defined it but don’t know where is the place to call it, normall there should be a main body function ?

Further reading reveals that all this need custom class/functions needs to be derived from UFunction in order to be executed by the main program workflow, can you confirm ?

Doing stuff in UE4 is different than in plain old C++. The concept of the main function doesn’t apply here. Rather, every Object - i.e. nearly every class that will be used in game - has it’s own functions for when gameplay starts, when the object is constructed, and every frame tick.

If you want to do file I/O in game, you can create an Actor class, put the code into it’s BeginPlay(), and spawn it when the level starts.

Furthermore, in UE4, you’re best off using FFileHelper for file i/o. Check out this wiki page, it covers writing: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

1 Like

UObject is more lightweight, don’t use Actor for stuff like this if replication or rendering is not necessary, and you can replicate an UObject as well (not necessary in this case).

And what do you want to store SBlade ? Actor variables ? Simple text ?

I’ll store integer casted to simple text where from time to time program will read and write. I really will neither need actor nor rendering but I’d like to handle the OnCreate event. What I couldn’t figure out is, where to place that event handling code, function whatever appropriate naming convention is suitable on UE4 side, I really don’t know where to place that and knowing when it will be triggered.

Can you show a small pseudo example and event handling and where all that needs to be placed?