Static function with ConstructorHelper returns last value

This code only gives me an ImpactDefault particle system for both variables:

EnvironmentHitVFX = ULib::CreateParticleSystem("/Game/BulletVFX/Particles/VFX_Impact_Default");
ExplosionVFX = ULib::CreateParticleSystem("/Game/Particles/P_Explosion");

This code only gives me the Explosion particle system for both variables (all I did was switch the order):

ExplosionVFX = ULib::CreateParticleSystem("/Game/Particles/P_Explosion");
EnvironmentHitVFX = ULib::CreateParticleSystem("/Game/BulletVFX/Particles/VFX_Impact_Default");

This is the function being called (notice that while debugging I found out that the string is refering to “/Game/Particles/P_Explosion” but it still returns the “VFX_Impact_Default”:

I assume it has something to do with the static variable “ParticleSystemObj”. Maybe the game is like “oh it’s already filled with something let’s not overwrite it”? How can I make this work for a generic function like this one?

Do your code require to reference those particle systems in constructor?

Yes, I need to set those references in the class-constructor (because it’s a parent-class) so that I can properly override those variables in child-classes. Thus I can not initialize it in BeginPlay. I need them at design-time.

Have you wrote this methods your self or they are provided in the engine ? if they are in the engine than tell me what class object have you used so I can try to investigate

Ok so I will assume that you have coded this methods your self and answer taking this to account.

first thing the “static” keyword is the most confusing one and people mostly misunderstand it static means create only one and only once ! consider this code

void MyFunc(int Value)
{
static int myStaticInt = Value;  // this will be executed only once 
// doesn't matter how many times you call this method passing in different value
}

however consider this code

void MyFunc(int Value)
{
static int myStaticInt = 0; // this will be executed only once 

myStaticInt = Value;  // This will work as intended 
}

can you see whats going on there ? when you declare a static variable it is done only once the same when you declare it and initialize it at one line it will be done once so you have to declare it first at one line and than you can modify its value at next one

Yes, those methods are written by me.

Btw yes I know you can’t declare a construction helper as it has no default constructor but you can trick it

UParticleSystem* tempSystem = nullptr;

tempSystem = ConstructorHelpers::FObjectFinderOptional<UParticleSystem>(assetFileName).Get();

#if WITH_EDITOR
if(tempSystem == nullptr) {LogError ...;}
#endif

return tempSystem;

I see so they are different from C# where you can change a static-value as long as it is not a constant.

Thanks for that trick! It worked perfectly. Full working code:

UParticleSystem* ULib::CreateParticleSystem(const FString Path)
{
	UParticleSystem* Temp = nullptr;
	Temp = ConstructorHelpers::FObjectFinderOptional<UParticleSystem>(*Path).Get();

#if WITH_EDITOR
	if (Temp == nullptr) { ULib::LogErrorSafely("ULib::CreateParticleSystem()", Path); }
#endif

	return Temp;
}

You can change it it is not const thats what I mean by people misunderstand it static is not const

I know what you trying to do you are trying to reference "Load "assets at run time am I right ?

if yes than you can’t use construction helper to do it as it is only for Construction Time Reference there are other methods do load assets at run time here a link

and remember static is not const static value can be changed but it must be done after declaration

static int  myStaticInt = Value; // Executed only once

myStaticInt = Value; // perfectly fine 
myStaticInt = anotherValue; // perfectly fine 

// where

const int myConstInt = Value; // must be initialized

myConstInt = newValue; // illegal error 


const static int  myConstStaticInt = 0;   // ok but most be initialized

myConstStaticInt = newValue;  // illegal error

Hey a little tip if you have a class and you declare a static variable in it all instances of this class will share the one created first so if you change the value in one class it will be changed in all of them