x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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?

Product Version: UE 4.11
Tags:
more ▼

asked Oct 16 '18 at 12:22 AM in C++ Programming

avatar image

Brandon236
88 29 40 45

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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?

more ▼

answered Oct 16 '18 at 01:01 AM

avatar image

ex3me
78 4 5

avatar image Brandon236 Oct 16 '18 at 01:18 AM

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.

avatar image ex3me Oct 16 '18 at 01:22 AM

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

avatar image Brandon236 Oct 16 '18 at 01:53 AM

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

avatar image ex3me Oct 16 '18 at 02:01 AM

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
 }
avatar image Brandon236 Oct 16 '18 at 02:12 AM

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.

avatar image ex3me Oct 16 '18 at 02:18 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Oct 16 '18 at 01:09 PM

avatar image

ZDron3
66 3 6 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question