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"

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.

alt text

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


Product Version: UE 4.18
Tags:
set-plane.png (94.2 kB)
more ▼

asked Apr 24 '19 at 07:52 AM in C++ Programming

avatar image

sudhamr
7 1 1 3

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

1 answer: sort voted first

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.

more ▼

answered Apr 24 '19 at 09:42 AM

avatar image

phil_me_up
1.5k 6 6 12

avatar image sudhamr Apr 24 '19 at 11:42 PM

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!

avatar image phil_me_up Apr 25 '19 at 01:21 PM

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.

avatar image Lardo Deepdelver Apr 25 '19 at 11:45 AM

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

(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