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
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
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 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
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
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:
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 ):