How to Use data member function of a C++ class in unrreal engine blueprint

Hi,

I am trying to use a C++ library (TimPr) that will read data of a sensor in unreal engine blueprint.

What I did so far is that I linked the headers and static library of this C++ library(TimPr) and then I created a C++ class based on BlueprintFunctionLibrary class and named it BPFL.

Inside the BPFL.h, I added the following code.

#include “TimPr.h”

UFUNCTION(BlueprintCallable)

static TimPr* ReturnAPointerToTimPrObject(float inputDataForTimPrConstructor);

// the TimPr object has a function member that return its data member float. (GetData()).

UFUNCTION(BlueprintCallable)

static float ReturnDataValueOfGivenPointerObject(TimPr* PointerToTimPr);

Inside BPFL.CPP I added the following code:

#include “BPFL.h”

TimPr* UBPFL:: ReturnAPointerToTimPrObject(float inputDataForTimPrConstructor)

{

TimPr ObjectOfTimPr(inputDataForTimPrConstructor);

TimPr* PTimPr = &ObjectOfTimPr;

return PTimPr;

}

float UBPFL::ReturnDataValueOfGivenPointerObject(TimPr* PointerToTimPr)

{

return PointerToTimPr->GetData();
}

Now when I try to build the project I get following errors:

1)Unrecognized type ‘TimPr’ - type must be a UCLASS, USTRUCT or UENUM.

2)Error MSB3073 The command ““C:\Program Files\Epic Games\UE_4.26\Engine\Build\BatchFiles\Build.bat” LastDayOfWeekEditor Win64 Development -Project=“C:\Users\meysam.imanipour\UE\LastDayOfWeek\LastDayOfWeek.uproject” -WaitMutex -FromMsBuild” exited with code 6. LastDayOfWeek C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.MakeFile.Targets

==============================================================================

What I want is to be able to use a function of this TimPr class which is a function member so I thought for this solution to create two static member function inside the BPFL class that one of them create an object instance and return a pointer to it, and the other function use this pointer to access the function member of the the TimPr class. I could do This in C++ but I don’t know what is the problem here.

I appreciate your help,

Thanks.

Is this a header only library? Otherwise how did you link it?

This code is returning a pointer to a local object that will be destroyed at the end of the function. I’m not quite sure what you’re trying to do.

Actually, the TimPr library is containing header file and also .lib file (static library).
To link this library to my UE project, I did the following steps:
1- I created a folder inside the directory of my UE project and named it “Plugin”
2- I added two sub folders inside this “Plugin” directory and named them “headers” and “libraries” respectively.
3- I copied the header file of TimPr C++ project into the “headers” directory of my UE project.
4- I copied the .lib file from the TimPr C++ project (solution directory of TimPr/x64/Release) into “libraries” directory of my UE project and renamed it to “TimPr.x64.lib”.
5- I opend the file UEprojectname.Build.cs and inside this file I added two following lines:

PublicIncludePaths.Add(@" Directory to headers I explained in step 2");
PublicAdditionalLibraries.Add(@" Directory to libraries I explained in step 2/ TimPr.x64.lib")

6- I also opend the property configuration of my UE project in visual studio by right clicking on project name and choosing “Properties” and insdie Configuration Properties/ VC++ Directories/ I added to Include Directories section the path to my “headers” that I explained in step 2.

Then after these configuration I did also the followings:
I created a C++ class from inside UnrealEngine and choose the parent to be Blueprint Function Library and named it “BPFL”.

After then inside the BPFL.h and BPFL.cpp I added the code that can be seen in the question I asked first.

Above was the way to link library to my UE project.
I see in your answer you mentioned that my function storing the pointer in local object, and it will be destroyed at the end of the function. For this, I will work on it to find a solution, if this is the issue.

What I am trying to do is that I have a third party C++ class ( TimPr ) and I want to be able to use this class inside the blueprint. The problem is that the member functions of this library (TimPr) are non-static functions and to be able to call a function of this class (TimPr) from blueprint I need an object of this class. So to be able to use this class in my blueprint I consider creating two static functions in BPFL class that one of them creates an object of the TimPr class and return it as pointer and the other static function will use this pointer to call a function of TimPr created object.

I created this picture so It might explain better, instead of many text :smile:

You might say why don’t I create a simple static function that inside it, construct the object of the class, and then call the desire function on that object and return the value. (like below)

static float foo (float data)
{
   TimPr obj(data);
   return obj.GetData();
}

the problem is that the function I want to use, will first do some configuration to connect to a sensor, and then it will send me the sensor data after finishing the configuration in a loop. but the configuration step takes time and I want to configure it for once at the beginning and then use it during my game.

For more information, I can probably have this function in the picture above as two separate functions (one function as constructor that is for configuration step, another function that use the object that is constructed and configured by first function to send data of sensor in a loop.)

Please tell me if something not clear, I will explain more, and I appreciate in advance for your help,
Meysam.

I presume you are trying to do with Game timer,

This might help, if not I would say sorry at the outset

I’ll try get a library up in Unreal later and see if I have any issues. As for your returning a function local variable problem, you could make it a static local variable.

TimPr& UBPFL:: ReturnAPointerToTimPrObject(float inputDataForTimPrConstructor)
{
    static TimPr Object(inputDataForTimPrConstructor);
    return Object;
}

which means it’ll be initialised only once and won’t be destroyed at the end of the function; though it would mean you call this with an unused argument 99% of the time.

Edit: I did essentially what you said you did and it included the header just fine. I suspect you didn’t specify the correct path in your Build.cs.

1 Like

Thank you for your reply,
I created another project and did all the process again and I was able to solve it.
I guess the problem I had was either linking error, or pass issue as you said.
Now it works fine, however when I use the functionality of third party class, the engine will freeze. I guess because the function I use is using multithread and it seems I have also to handle it inside unreal engine which is out of scope of my current ability ( no experience with threads)
so I will consider this post is answered to close it.
Thanks.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms