How to call a function from another c++ class

I haven’t seen alot of tutorials on how to do this. If I have a function in an actor c++ class, how do I call that function from another c++ class? Let’s say I have two actors called MyActor and MyOtherActor. In my header class for MyActor, I defined a pointer to MyOtherActor

UPROPERTY(EditAnywhere)
	AActor* MyOtherActor;

So from that point, how would I call a function from that actor class or do I need to set up a different type of point for function calls?

This is not really UE, but rather a C++ thingy, maybe that’s why you haven’t seen it. In C++, if you have a pointer to an object (as in your example), you don’t use dot notation like this

MyOtherActor.SomeFunction();

But rather

MyOtherActor->SomeFunction(); 

Is this what you are asking about?

I tried using the arrow and that didn’t work. It kept saying “AActor has no member named SomeFunction” even though the function is 100% in the other class.

well that’s odd, could it be an issue of protection? Hard to say without seeing the code, but make sure your function is public. If it’s just something VS says it might not actually be a problem, just trying compiling the code.

Some other possibilities are typos (in function name and/or header file), forgetting to include header file, but I’m kinda just shooting here

I tried making my function public but now it says public void is a syntax error. What’s going on now? The header for the method simply says:

public void AMyOtherActor::SomeFunction() {

if this is a header than the function declaration should be

public void SomeFunction();

then in your .cpp file will have a method declared as

public void AMyOtherActor::SomeFunction() {
    // some code
}

When you declare a function in your header file, there’s a public and a private section if the function is listed under the public section then it should already be a public function. Also putting public before the method doesn’t work. So this brings me back to my first question. Why does it say that AActor has no member named SomeFunction? I included the header file of my other actor so it should be able to find the function.

oh yea my bad, in header you don’t need to use public before a function, obviously :slight_smile: Uhm as mentioned hard to say unless I can put my hands on it, but try getting intelisense up see what it offers to you, sometimes this might be caused just by files not being complied, although it doesn’t seem to be the case.

anyway Intelisense usually offers the options that should be good to compile

Hi there,
To call a function from one actor class to another you need to use the reference of actor class have it store something and use that, Let’s say you have two actors called MyActor and MyOtherActor. In the header class for MyActor, you define a pointer to MyOtherActor
In MyActor.h

#include MyOtherActor.h

public :

UPROPERTY()

MyOtherActor* otheractorname;

In c++ , you need to store something in the reference like maybe use a spawnactor or getactorofclass somewhere before you call the function present in MyOtherActor

In MyActor.cpp

otheractorname = GetWorld()->SpawnActor(MyOtherActor::StaticClass(), myLoc, myRot, SpawnInfo);

then use this reference to call function

otheractorname->someFunction();

In MyOtherActor.h

public :

void someFunction();

In MyOtherActor.cpp

void MyOtherActor::someFunction()
{
//do something
//change return type and return something to be used in MyActor.cpp
}**

*Above code is just a structure.

Hope it helps.

first include MyOtherActor.h in MyActor.cpp

find a pointerto Other Actor (the actor itself not MyOtherActor class) and save it in a pointer of typer actor(AActor* OtherActor) for example When Detecting collision this is Done automatically in function declaration:

void AMyActor::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)

here the pointer to collided actor is automatically saved in AActor* OtherActor
now make a pointer of type MyOtherActor which we find using your OtherActor pointer and cast your class to it (note that we are now pointing to MyOtherActor class ):

AMyOtherActor* MyOtherActorPointer=Cast<AMyOtherActor>(OtherActor);

always make sure that your pointer is not null or things might crash all around

if (MyOtherActorPointer!= nullptr) {
			MyOtherActorPointer->SomeFunction();
		}

so here is your final code in your MyActor.cpp:

#include "MyOtherActor.h"
//some stuff
void AMyActor::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
AMyOtherActor* MyOtherActorPointer=Cast<AMyOtherActor>(OtherActor);
    if (MyOtherActorPointer!= nullptr)
         {
    		MyOtherActorPointer->SomeFunction();
    	}
}

by the way you can pass variables as your SomeFunction arguments too :wink: