Basic C++ Questions

I’m struggling with coming over to Unreal Engine 4 with my C++ knowledge so hopefully you guys could be kind enough to answer some of my silly questions.

All I want to do is to be able to access members/functions of one class from another, after following on from this tutorial:

For example, at the moment I’ve got all my Action/Axis input controls in a FPSCharacter.cpp and one of them executes the function within FPSCharacter called Output() when ‘O’ is pressed. I want this Output() function to be able to print out a value from a different class called FPSProjectile either by directly accessing the member or perhaps using a getValue() function.

So the basic question is, how can I allow FPSCharacter to access FPSProjectile members/execute FPSProjectile functions?

I hope that makes sense, thanks a lot for the help!


First of, you need access to your FPSProjectile object.
If it’s spawned dynamically and FPSCharacter just can’t have pointer to it, you can use level actors iterator to find your projectile and then call or access any FPSProjectile members. If you spawn it within FPSCharacter, you can store pointer to projectile and then use it when needed, make sure you check it against null all the time you’re trying to access it, since projectile could be destroyed by the time you access it.


    Projectile* Proj = FindActor();

If you only ever have one projectile, then keeping track if it is easy. Store the pointer in some variable and call it later. Projectile->Report();

It might get unwieldy if you have a lot of projectiles. There are some methods to handle that like the observer pattern.
An example:

Modifying the concept to fit UE4, you would probably use TArray rather than std::vector, and you can use UE4 concept of interfaces in the same way they as the AlarmListener is done.

Another option might be to set up an event… broadcasting to each bullet a reference for them to write back to.

Thanks for the help guys but so far I feel like I’m going in circles a little bit, just confusing myself.

I just want to bring it right down to basics quickly and ask if what I am doing wrong

Got a class called FPSTest with a function called FuncTest() that prints out some text to the screen with AddOnscreenDebugMessage.
I want to be able to execute that function from within my FPSCharacter class, simple right? Not for me!

I thought it would be as simple as writing something like (simplified)


Someone put me out of my misery please! Haha

With the simplified example it’s hard to say exactly what might be going wrong, so I’ll just answer as if that was exactly what you have in your .cpp file.

AFPSTest* TestClass; is a typed pointer, not an instance of AFPSTest. The pointer points at nothing until you assign it.
TestClass->FuncTest(); is esentially null->FuncTest();

You could maybe try to access static members through this without an instance, but the compiler would complain. The behavior is undefined.

AFPSTest implies it’s an actor so following the example you linked and following the directions to spawn an actor, you would get a pointer to an instance of AFPSTest this way:

TestClass = World->SpawnActor<AFPSTest>(TestClass, Location, Rotation, SpawnParams);

After you have done that, you can call a public method of TestClass by using the same format you’ve used in your simplified example assuming the object pointed to by TestClass has not been destroyed.


AFPSTest would be defined in this way

/* .h 
UE4 boilerplate 
class AFPSTest : public AActor /*or your desired base class */
    /* UE4 body macro */

    void TestFunc();

/* .cpp
#include "FPSTest.h"

   : Super(PICP)
{ }

void AFPSTest::TestFunc()
     ;/* Stuff */