Why ConstructionHelper::FClassFinder in FPP template gamemode is static?

I am extremely curious why in FPP template, the provided game mode have static ConstructionHelpers::FClassFinder instead of non-static? Is it kind of optimialization? Won’t be better if we throw away this helper after we find required APawn? Constructor for gamemode is called once, so what is the purpose of keeping this helper?

	: Super()
	// set default pawn class to our Blueprinted character
	static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/FirstPersonCPP/Blueprints/FirstPersonCharacter"));
	DefaultPawnClass = PlayerPawnClassFinder.Class;

	// use our custom HUD class
	HUDClass = ACodeFppDemoHUD::StaticClass();

Static functions are executing without initiation of a object so there nothing in memory created for that function if you use ConstructorHelpers. It’s code is always stored in memory together with rest of game and engine code, whatever you like it or not OS loads entire content of exe’s and dll’s to memory because this is only way CPU can access it.

In fact “ConstructorHelpers” here is actually used as namespace then object or class when calling static function. Same goes with ACodeFppDemoHUD::StaticClass() this won’t initiate any ACodeFppDemoHUD object, the code of that function is loaded either way with the rest of the game code, “ACodeFppDemoHUD::” is just namespace here and this function it will just return UClass of a this class which is stored in memory by UE4 reflection system either way.

So in short, fact that you use ConstructorHelpers won’t effect memory in anyway, at least direcly

Hmm I think he means what is the Lifetime of the struct if you use static in front of it.

ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/FirstPersonCPP/Blueprints/FirstPersonCharacter"));


static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/FirstPersonCPP/Blueprints/FirstPersonCharacter"));

To my understanding with the static keyword in front it will be initialized once the Codeblock is hit the first time and kept in memory until the Application Exits. That prevents it from reinitializing if the Codeblock is called again.

You are correct, not to mention it only takes 64-bits(8 bytes) of memory

Yep, lifetime of this variable is clear to me now, however I am still curious what is the purpose of using static in this case?

Not much benefit on Game Mode I guess. But if you have a Actor you spawn a couple hundred times you skip the construction part of the Construction Helper since he is basicly a global but limited to Class/Function scope.

Great! It will be stored in memory until game ends. So if it is used once, maybe it will be better to access ConstructionHelper as non-static, because it will occupy memory all the time but it won’t be used anymore until next creation of gamemode? I know that modern machines have great amounts of RAM, still my purist nature forcing me to looking for reason “why it is static and why to store it as global whole the time” and in my opinion in that case it will be better as non-static :), but I am not confident enough to argue with code written by EPIC :P.