x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How to bind to SkeletalMesh->GetOnMeshChanged

Hi there,

I'm trying to bind a function to SkeletalMesh OnMeshChanged, 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.) alt text

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

Product Version: UE 4.21
Tags:
addfunction.png (37.5 kB)
more ▼

asked Mar 12 '19 at 10:19 PM in C++ Programming

avatar image

jm991
6 1 2

avatar image jm991 Mar 12 '19 at 10:45 PM

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...

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first
 #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

more ▼

answered Mar 13 '19 at 02:27 AM

avatar image

Elou44
179 3 9 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question