How to bind to SkeletalMesh->GetOnMeshChanged

Hi there,

I’m trying to bind a function to [SkeletalMesh OnMeshChanged][1], but I’m having trouble getting my function to bind. My usecase for this function is to be able to update another SkeletalMeshComponent that needs to grab the new skeletal mesh every time the Character’s skeletal mesh is changed.

Here’s what I have in my .h file:

public:
    // Editor related code
#if WITH_EDITOR
    UFUNCTION()
    void SetToonSkeletalMesh();
#endif

And here’s what’s in my .cpp file (in my constructor):

#if WITH_EDITOR
    this->GetMesh()->SkeletalMesh->GetOnMeshChanged().AddUObject(this, &ATdpCharacter::SetToonSkeletalMesh);
#endif

and further down in the file, is the implementation of that method

#if WITH_EDITOR
void ATdpCharacter::SetToonSkeletalMesh()
{
    ToonOutlineComp->SetSkeletalMesh(this->GetMesh()->SkeletalMesh);
}
#endif

This compiles, but the editor crashes immediately on launch. I’m familiar with AddDynamic, but that particular function does not seem to be available here. I wasn’t sure which of these to use (Add, AddUObject, AddUFunction, etc.)

Does anyone have any pointers on how I can attach my method to the OnMeshChanged event?

I was also able to do a lambda declaration inline in the .cpp file:

    auto OnSkeletalMeshChanged = [this]()
    {
        UE_LOG(LogTemp, Log, TEXT("SkeletalMeshChanged!"));
        ToonOutlineComp->SetSkeletalMesh(this->GetMesh()->SkeletalMesh);
    };

    this->GetMesh()->SkeletalMesh->GetOnMeshChanged().AddLambda(OnSkeletalMeshChanged);

This also seems to build fine but crash the editor on start…

#if WITH_EDITOR
this->GetMesh()->SkeletalMesh->GetOnMeshChanged().AddUObject(this, &ATdpCharacter::SetToonSkeletalMesh);
#endif

is indeed the correct way to bind a function to a multicast delegate but I believe you have to it in the ‘BeginPlay’ of your class, not in the constructor.

You can also have a look at the example provided in the Unreal Engine 4 Scripting with C++ Cookbook.

Cheers