Static local variable


I haven’t programmed with C++ in a while and I stumbled over this use of the static keyword for a local variable in a tutorial:

static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));

So please correct me if I am wrong: This means that this specific line of code is only executed once during runtime, and the next time that function is called, PlayerPawnBPClass will have the same value that was assigned to it the last time this function was called. Basically reducing the number of times that the initialisation is being executed to one, hence saving some processing time. Correct?

Thanks in advance

Mostly that is correct. It will keep whatever it’s value is across function calls, so if you assign to it after initialization that will be the value it has the next time the function is called (not sure if that’s what you were getting at with the wording of “last time this function was called” or not).

Personally, for something like that I would almost always declare it as const also.

1 Like

Also, keep in mind that after you reload code with Live Coding, updates to static local variable won’t be applied, you’ll have to restart the editor.

This is annoying, but I doubt it’s even possible to fix in future versions. Maybe it’s possible to do the same thing as with reloading functions?


Thanks, understood. The const makes sense to me in this scenario, same for hot reload. Thanks to both of you!