Accessing C++ class (implemented as blueprint) variable by another class

I have a C++ class called PlaneClass. It should store the plane’s origin ( planeOrigin ) and normal vector( planeNormal ). There is another class called IntersectionTest which will want to access this plane’s data when doing some tests.

The PlaneClass is implemented as a blueprint class PlaneClassBP which updates the position of normals every tick (creates a rotating plane). So, this blueprint PlaneClassBP also sets the variable value every time for planeOrigin and planeNormal.

What is the best way for the IntersectionTest class methods to access the variables planeOrigin and planeNormal which will be updated every frame by the blueprint? Will my class reflect the changes the Blueprint is making every frame?

I thought I could make the PlaneClass a singleton by using:

PlaneClass.h

public:
    static const APlaneClass* planeClassObject;

PlaneClass.cpp

PlaneClass()
   APlaneClass = this;

so that I can access these variables in the IntersectTest class using just this instance, for example:

FVector origin = APlaneClass::planeClassObject->planeOrigin;

but then I get an error :

unresolved external symbol "public: static class APlaneClass const * const APlaneClass::planeClassObject" (?planeClassObject@APlaneClass@@2PEBV1@EB

The error your seeing is because you haven’t defined your static variable in your cpp, plus you’re also trying to change the value of a const variable. If you change it to just be static rather than const static, and also make sure you define the variable in your cpp too.

However, this isn’t the way I would do it. I’ve seen some issues around garbage collection and proper cleanup between sessions with this kind of strategy. Instead I’d either have the PlaneClass register itself OnBeginPlay with either a custom GameInstance class, or preferably some manager (e.g. PlaneManager) that is initialised and owned by the game instance class. This way any system in your game can access the plane (or multiple planes).

Note, as you’ve defined your class in C++, it’d be more efficient to actually perform the tick in the C++ class too. Invoking the blueprint tick every frame is relativly expensive, and what you’re doing can be easily implemented into C++ for better performance.

I actually did try making it just a static variable but that would give me the same unresolved external symbol error in both the PlanceClass and IntersectionTest class objects.

Also, how to do “define” the static variable? I currently set it to planeClassObject = this in the PlaneClass.cpp both in the constructor and BeginPlay. It didn’t seem to make a difference.

And yes, it kind of makes sense that a manager class could help. Thanks! I am new to working with C++ in UE4 and trying to strike a balance between Blueprints too. I made it a Blueprint class because it is easier to debug and add graphical things to it and manage it in the scene!

static const APlaneClass* planeClassObject;
or
static APlaneClass* planeClassObject;

in the header is just the declaration you are missing

const APlaneClass* APlaneClass::planeClassObject = something;
or
APlaneClass* APlaneClass::planeClassObject = something;

in your PlaneClass.cpp file

but as Phil said the whole thing sounds a little bit wonky.
Would suggest adding a pointer

UPEROPERTY()
APlaneClass* planeClassObject;

into a Manager that could be in a component on the game mode or game state
the UPROPERTY() is important to prevent garbage collection

Lardo has shown how to define the variable in his comment below. It get why you missed it though. After many, many years of writing C++ I still forget to do this, but fortunatly you become familiar with the error messages and it’s very quick to fix.