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"

Spawning a mesh during an animation using C++

Having an skeletal mesh animation I wanted to spawn a static mesh and attach it to the socket of skeletam mesh.

First idea

Just create UPROPERTY() UStaticMeshComponent* MeshComponent; and allow it to be modified in the editor, so that any1 can pick proper mesh per animation.

tl;dr: it simply was crashing at some point (it was probably when it was attached to a different component but I can't remember now). Could it possibly be that due to notification not having World reference it won't create attachable components?

Second idea idea - construct static mesh component

UAnimNotifyState deriving class seemed to be a perfect candidate for just it because it features NotifyBegin and NotifyEnd methods - that seems to be super handy. There were 2 additional variables that I wanted to expose for change:

  • Mesh - so that you can tell what mesh should be spawned in the editor,

  • SocketName - so that you can also pick where the mesh should be bound to,

Editor timeline preview

So I came with a class like this (you might find a more comfortable preview on GitHub):

 #pragma once
 #include "AnimNotifyWithComponent.generated.h"
  * A solution based on manually creating UStaticMeshComponent in NotifyBegin function.
  * It work ingame no problem, but causes weird freeze in the editor anim preview.
 class UAnimNotifyWithComponent : public UAnimNotifyState
     // Exposed Mesh property, so that mesh can be changed in the editor.
     UPROPERTY( EditAnywhere )
     UStaticMesh* Mesh;
     // Name of the socked, where the item will be placed.
     UPROPERTY( EditAnywhere )
     FName SocketName;
     UStaticMeshComponent* MeshComponent;
     virtual void NotifyBegin( USkeletalMeshComponent * MeshComp, UAnimSequenceBase * Animation, float TotalDuration ) override;
     virtual void NotifyEnd( USkeletalMeshComponent * MeshComp, UAnimSequenceBase * Animation ) override;

You may find cpp file here.

In game it works just fine.


The code was not working in the editor prview. Once editor hits my notification (and calls NotifyBegin) it freezes for couple seconds giving an error on assigning reference to UPROPERTY(), here's a dump:

 UE4Editor-Core.dll!FWindowsPlatformStackWalk::StackWalkAndDump() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\windows\windowsplatformstackwalk.cpp:175]
 UE4Editor-Core.dll!FDebug::EnsureFailed() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\misc\outputdevice.cpp:276]
 UE4Editor-Core.dll!FDebug::OptionallyLogFormattedEnsureMessageReturningFalse() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\misc\outputdevice.cpp:385]
 UE4Editor-Engine.dll!UActorComponent::RegisterComponent() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\components\actorcomponent.cpp:866]
 UE4Editor-sample1-6656.dll!UAnimNotifyWithComponent::NotifyBegin() [c:\users\mlewandowski\documents\unreal projects\sample1\source\sample1\animnotifywithcomponent.cpp:59]
 UE4Editor-Engine.dll!UAnimInstance::TriggerAnimNotifies() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\animation\animinstance.cpp:1213]
 UE4Editor-Engine.dll!UAnimInstance::UpdateAnimation() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\animation\animinstance.cpp:433]
 UE4Editor-Engine.dll!USkeletalMeshComponent::TickAnimation() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\components\skeletalmeshcomponent.cpp:479]
 UE4Editor-Engine.dll!USkeletalMeshComponent::TickPose() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\components\skeletalmeshcomponent.cpp:588]
 UE4Editor-Engine.dll!USkinnedMeshComponent::TickComponent() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\components\skinnedmeshcomponent.cpp:451]
 UE4Editor-Engine.dll!USkeletalMeshComponent::TickComponent() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\components\skeletalmeshcomponent.cpp:600]
 UE4Editor-UnrealEd.dll!UDebugSkelMeshComponent::TickComponent() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\editor\unrealed\private\animation\debugskelmeshcomponent.cpp:728]

Third approach - spawn AStaticMeshActor

Let me just note at the very begining that this is overkill :D The idea was to:

  1. Spawn AStaticMeshActor that would be owned by Animation Skeletal Mesh owner.

  2. Hijack it's StaticMeshComponent and attach it to the Skeletal Mesh.

  3. Destroy AStaticMeshActor because... I don't need it for anything beside constructing StaticMeshComponen!

This idea is totally crazy, but implementation can be found in AnimNotifyWithActor class.


Here's the thing: it works both for the editor and game. It's the worst solution, yet it works the best.

The code

I decided to push all the code to a GitHub repository, feel free to see it there.

Product Version: UE 4.10
more ▼

asked Jan 25 '16 at 10:14 PM in C++ Programming

avatar image

80 4 10 17

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

0 answers: sort voted first
Be the first one to answer this question
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