# How to call a function from another c++ class

**URL:** <https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707>\
**Category:** Editor Scripting\
**Tags:** reference, calling-a-function, unreal-engine, pointer, functions, CPP, question\
**Created:** [October 16, 2018, 12:22am UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707 "2018-10-16T00:22:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brandon236](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Brandon236](https://forums.unrealengine.com/u/Brandon236)\
**Post date:** [October 16, 2018, 12:22am UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707/1 "2018-10-16T00:22:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ex3me](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ex3me](https://forums.unrealengine.com/u/ex3me)\
**Post date:** [October 16, 2018, 1:01am UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707/2 "2018-10-16T01:01:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brandon236](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Brandon236](https://forums.unrealengine.com/u/Brandon236)\
**Post date:** [October 16, 2018, 1:18am UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707/3 "2018-10-16T01:18:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ex3me](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ex3me](https://forums.unrealengine.com/u/ex3me)\
**Post date:** [October 16, 2018, 1:22am UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707/4 "2018-10-16T01:22:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brandon236](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Brandon236](https://forums.unrealengine.com/u/Brandon236)\
**Post date:** [October 16, 2018, 1:53am UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707/5 "2018-10-16T01:53:31Z")

</div>

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() {
```

---

<div class="post-metadata">

**Author:** ![ex3me](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ex3me](https://forums.unrealengine.com/u/ex3me)\
**Post date:** [October 16, 2018, 2:01am UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707/6 "2018-10-16T02:01:50Z")

</div>

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
}
```

---

<div class="post-metadata">

**Author:** ![Brandon236](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Brandon236](https://forums.unrealengine.com/u/Brandon236)\
**Post date:** [October 16, 2018, 2:12am UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707/7 "2018-10-16T02:12:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ex3me](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ex3me](https://forums.unrealengine.com/u/ex3me)\
**Post date:** [October 16, 2018, 2:18am UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707/8 "2018-10-16T02:18:56Z")

</div>

oh yea my bad, in header you don’t need to use public before a function, obviously 🙂 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

---

<div class="post-metadata">

**Author:** ![ZDron3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ZDron3](https://forums.unrealengine.com/u/ZDron3)\
**Post date:** [October 16, 2018, 1:09pm UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707/9 "2018-10-16T13:09:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![morbal](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@morbal](https://forums.unrealengine.com/u/morbal)\
**Post date:** [October 16, 2019, 4:55pm UTC](https://forums.unrealengine.com/t/how-to-call-a-function-from-another-c-class/435707/10 "2019-10-16T16:55:00Z")

</div>

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 😉
