Function after component is initialized

Hi,

is there a way to call a function that is executed in the editor after the ActorComponent’s variables are initialized from the properties we set in the editor?
With actors construction script / constructor works fine, but the component’s variables aren’t set when the component’s constructor or when the owner’s constructor is executed.

Thanks,

Elathan

Have you tried overriding OnComponentCreated() ?

Thanks for your reply.

I just tried it - doesn’t work. Float and int values are 0, pointers are NULL, no matter what value I set in the editor.

Maybe the are initialized only when the game begin?

Hi,

Try with PostInitializeComponents

//.h
virtual void PostInitializeComponents() override;

//.cpp
void AYourActorClass::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	//... your code
}

Best regards

Thanks.

Unfortunately it’s an ActorComponent, not an actor.
I also tried PostInitProperties() - it is called every time I change a value in the editor on the component, but the result is the same: I see default values.

Ohh on ActorComponent, sorry . . . well try with InitializeComponent.

Initializes the component. Occurs at level startup. This is before BeginPlay (Actor or Component). All Components in the level will be Initialized on load before any Actor/Component gets BeginPlay Requires component to be registered, and bWantsInitializeComponent to be true.

//.h
virtual void InitializeComponent() override;
 
//cpp
 void UYourComponentClass:: InitializeComponent()
 {
	Super::InitializeComponent();
	//Your code
 }

Best regards

That wouldn’t help, because I need something which is executed in the editor, not just before the game starts.

OK, I understand, try with: PostEditChangeProperty this function is called ONLY ON EDITOR when a property on this object has been modified externally.

It is from UObject, so probably you can use in a ActorCOmponent but, personally, I only have used in Actor class. But try, maybe it work for you.

IMPORTANT: Note the #if WITH_EDITOR macro, because this function is just for the Editor

//.h
#if WITH_EDITOR
virtual void PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent) override;
#endif // WITH_EDITOR

//.cpp
void YourClass::PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent)
{
	Super::PostEditChangeProperty(PropertyChangedEvent);

	//... your code
}

Try it and let me know if this work for you.

Best regards

Thanks, it actually worked :slight_smile:

SET PRE Initialise Component