Call a function from another Class(No Blueprint)

Hello Unreal Community,
I’m new to C++ programming any advice or examples of my question would be very much appreciated. I have 2 classes, Class A extend GameMode and Class B extend BlurprintFunctionLibrary both created in C++. I’ve created a function in Class B and in trying to call that function from Class A; Is this possible?

Thanks for your help in advanced.

I just answered this XD https://answers.unrealengine.com/questions/246615/calling-c-functions-of-another-class-in-blueprint.html

Hey thanks for the reply but I forgot to mention that the classes were both created in c++ and not blueprint…So maybe if you can also show me an example with the c++ class that would be great…:slight_smile: :smiley:

yeah, the first part of it is just C++
the blueprint part is just attaching a blueprint to the C++ class, it’s not needed though.

so basically you have AClass1 and Aclass2

In AClass1 header, you put the AClass2.h header include so that way AClass1 can find/read AClass2

After that, in the header of AClass1 you would put:

AClass2  *CurrentClass2;

in the AClass1 header you put:

CurrentClass2 = ObjectInitializer.CreateDefaultSubobject<AClass2>(this, TEXT("Class2"));

Hope that helps!

Just an extra note here - if you’re using a pointer to an Unreal class (as most of your code would probably be) you must also decorate the pointer declaration with UPROPERTY(), e.g.

UPROPERTY()
AClass2* CurrentClass2;

Otherwise you will run into trouble with the Garbage Collection - it will destroy these objects unless you’ve marked them as properties.

Thanks you guys for your quick response…But i have one more question. So the function in AClass 1 is called AClass1_Function.
after I add

#include AClass2.h

UPROPERTY()
AClass2* CurrentClass2;

CurrentClass2 = ObjectInitializer.CreateDefaultSubobject(this, TEXT(“Class2”));

All the Above is added in the Header file of AClass1. Where Exactly am i Calling the AClass_Function??

If you want to call the function from Class 1 in Class 2, you need to do it the other way round.

You want to include the header file of the Class 1 in Class 2 and create a SubObject there. Otherwise you can only call functions of Class 2 in Class 1.

Hello,

I am using 4.8, ObjectInitializer is undefined when I am trying to use it. Can anybody explain why?

Hello,

I am using 4.8, ObjectInitializer is undefined when I am trying to use it. Can anybody explain why?

Sorry, I’ve been looking through all of the posts and yet i still can’t find the command used to call a function from other classes, you say how to set it up, but not the actual command used to call the function