Download

Creating a static member variable in UE pawn class

Hi, I am a very new learner, so I am sorry in advance. I am currently following along a tutorial and am confused

Here, the pawn class is called Collider. Is it okay for you to declare and initialize a static variable of type FObjectFinder inside a constructor? Would that not defeat the purpose of it being static (not related to any instance of Collider)? Also, out of curiousity, I was trying to declare a static class member of type FObjectFinder in my Collider.h file, before initializing it in Collider.cpp file (just like how you declare and initialize static class members in normal C++)

In my header file:

In my cpp file

However, it said that the class member is not found. It seems that the error is solved if I include #include “UObject/ConstructorHelpers.h” in the header file. However, this crashes my UE and it refuses to open. My question is simply then: how would I declare and initialize a static class member of type FObjectFinder? What is the code and includes for both .h and .cpp files? Thanks in advance!

MeshComponent varies by instance. (e.g. There is a different component instance on each pawn.) But the skeletal mesh asset that each MeshComponent uses is the same asset. It does not vary by instance. FObjectFinder is also a helper object that does not vary by instance. It is a global helper to find the asset. So everytime the helper finds the same asset.

Hope that clarifies.

Yes, I understand that part. But that still does not explain why I am not able to create FObjectFinder as a static class member in my .h file (instead of declaring and instantiating as a local static variable in my constructor in the .cpp file).