Using ConstructorHelpers::FObjectFinder within a for loop

I’ve been trying to reference multiple classes inside my actor by doing this:

for(FString reference : weaponReferences){
	static ConstructorHelpers::FObjectFinder<UBlueprint> WeaponBlueprint(*reference);
	if (WeaponBlueprint.Succeeded()){
	WeaponSpawn.Push((UClass*)WeaponBlueprint.Object->GeneratedClass);
	}
}

with ‘weaponReferences’ being a

TArray<FString> weaponReferences;

in which I stored all blueprint locations
and ‘WeaponSpawn’ being a

TArray<TSubclassOf<class AWeapon>> WeaponSpawn;

However when I get the first and second object of ‘WeaponSpawn’ I get the exact same object although the paths are definitely different.
Is there a way to clear the ‘WeaponBlueprint’ after I stored it in my Array? would that even help addressing the issue?

Excuse my weird formatting, I’m still trying to get used to the Text-Editor.

Hello.

It happens, because you declared and initialized FObjectFinder as local static variable.

In such case, the variable is initialized only first time and persists through lifetime of the game and can be accessed in that scope as many times as you want.

Normally, doing that with FObjectFinder in constructor is a valid aproach. That way you don’t have to actually search for given asset every time a constructor is called. In your case however it clearly won’t work properly. To solve this, you could simply remove the ‘static’ keyword. BUT, that will have impact on performance (on constructing instances of your class). I don’t know how big that impact would be tho.

If I were you, I’d consider removing that from constructor and keeping it as configurable, blueprint array.

Alternatively, you could refine your loop, so it would contain static map of that FObjectFinder and cache assets that you already found there. And then, instead of searching for assets everytime, you’d first check if these are already cached etc.

Cheers.