Download

FMultiComponentReregisterContext - what does it do?

I’ve been trying to implement PostEditChangeProperty with some success but there are still some areas that are not clear for me. Especially FMultiComponentReregisterContext. Consider this API example https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/PostEditChangeProperty/index.html


#if WITH_EDITOR  
void AMyComponentSpawner::PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent)  
{  
    //Get all of our components  
    TArray<UActorComponent*> OwnedComponents;  
    GetComponents(OwnedComponents);  

    //Get the name of the property that was changed  
    FName PropertyName = (PropertyChangedEvent.Property != nullptr) ? PropertyChangedEvent.Property->GetFName() : NAME_None;  

    // We test using GET_MEMBER_NAME_CHECKED so that if someone changes the property name  
    // in the future this will fail to compile and we can update it.  
    if ((PropertyName == GET_MEMBER_NAME_CHECKED(AMyComponentSpawner, MyMesh)))  
    {  
       ** FMultiComponentReregisterContext ReregisterContext(OwnedComponents);  **

        for (UActorComponent* Comp : OwnedComponents)  
        {  
            if (UStaticMeshComponent* MeshComp = Cast<UStaticMeshComponent>(Comp))  
            {  
                MeshComp->StaticMesh = MyMesh; // Update the component to the new mesh  
            }  
        }  
    }  

    // Call the base class version  
    Super::PostEditChangeProperty(PropertyChangedEvent);  
}  
#endif  

According to this link: https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/FMultiComponentReregisterContext/index.html , FMultiComponentReregisterContext does this:

It explains me nothing. What ReregisterContext does? Why PostEditChangeProperty code example is using it?

The constructor of FMultiComponentReregisterContext will iterate over the entire array of ActorComponents you pass to it and unregister them first. While it unregisters, it stores the ActorComponent/world pairs in an array. Once the FMultiComponentReregisterContext has its destructor called, it will iterate over the array of ActorComponent/world pairs and register the components again. So “for the lifetime of this class” (FMultiComponentReregisterContext) the components will be unregistered.

So basically RegisterContext checks if the components of my actor are residing in the world? So if I am not using ActorComponents but I just want to do something with my private UStaticMeshComponent stored in .h file, I can skip doing this registration check? I am confused with this registering and unregistering process.

So after all, is this actually needed in order to react to change properties, please?