What is the difference between PostEditChangeProperty and OnConstruction?

I have an actor class in C++ that I can place in the level through the editor. This actor has a few properties that can be adjusted in the editor and it updates based on those properties. Nothing too fancy.

I have some code that updates the actor based on its properties. I have just realised that I’m calling this same code in both the OnConstruction function and the PostEditChangeProperty function. So that got me thinking… why am I doing that?

So I decided to comment out one of them to see what would happen… Nothing! It worked exactly the same. So I uncommented that one and commented out the other one to see what would happen… Nothing! It worked exactly the same again. Then I remembered that OnConstruction is called anytime a property of the class changes. But hang on… PostEditChangeProperty is called anytime a property of the class changes too. So essentially they are doing the same thing. In my case the same code was just being run twice for no reason.

So what is the difference? More importantly, what is the correct way to respond to changes to a property of a class? Should my code by in OnConstruction or PostEditChangeProperty?

I see that PostEditChangeProperty has the advantage of being able to determine exactly which property changed, but other than that I don’t see any difference. If you don’t care which property changed, can you use OnConstruction without a problem?

From what I read from the documentation, OnConstruction is called when it’s first placed into the editor. So it seems like OnPropertyChanged is the right one to use.

What kind of things are you doing to get OnConstruction called when it’s already placed in the level?

Actually OnConstruction is called when first placed in the world, AND anytime a property of the object changes in the editor. I’m not doing anything special, that’s just how it works. Essentially the same behaviour as PostEditChangeProperty. So my question still stands, what’s the difference between them?

The difference lies in both when they are called and what arguments are passed to them. OnConstruction happens after the object is spawned and/or when placed in the editor, whether any properties have been changed or not. PostEditChangeProperty occurs whenever a property is changed (but, I believe, not when the object is first placed within the game world).

OnConstruction is eventually called after PostEditChangeProperty, because whenever a property is changed within the editor that object is then respawned to reflect its new creation state; so OnConstruction is more of a side effect of changing a property, rather than a direct result.

PostEditChangeProperty is also passed a FPropertyChangedEvent which gives you information about what property was changed and the nature of that change. This allows you the opportunity to screen which properties have been changed that triggered PostEditChangeProperty and to perform other ancillary tasks based on those specific changes. OnConstruction does not include this event data, although it does include a FTransform reflecting the spawn transform data.

Choosing which one to use depends on what you are trying to do/respond to and why, and there are situations that it could make sense to use either (especially if order of execution is immaterial). If you wanted to limit your code to only executing when specific properties are modified, you would want to use PostEditChangeProperty exclusively, and include a check for which property is changed.

1 Like

They do about the same thing but, OnConstruction can NOT be called in components. ActorComponents and SceneComponents can use PostEditChangeProperty.

I wanna do something in PostEditChangeProperty to update details panel of Scene Component, but I also need to get another component or its reference. How should I do?