How to Add Component inside “Call in Editor” Function ?
When I “Add Component by Class” node inside “Call in Editor” with simple “Sphere Collision” - I see on the Scene in Called Actor Sphere is showing up !
But in components list it does not appear ! And if I move a little this Actor, Sphere disappear !
In the other way, If Actor has by default Component Sphere and inside “Call in Editor” function with GetComponentsByClas->“For each Loop”->DestroyComponent, it tell me in “Output Log”: “Error: May not destroy component SphereComponent…”.
The same happen if my Actor hasn’t SphereComponent but I Add it by “Call in Editor” function and then destroy all components returned by GetComponentsByClass. I have errors on old Components not destroyed properly “Error: May not destroy component SphereComponent…”
Anyone know is it possible to Add/Delete Component to Actor in “Call in Editor” function ?
I solved this problem by inherit from SSCSEditor and call method “PerformComboAddClass”. This function can add visible component like after click green button “Add Component”.
UActorComponent * SEdit::DoAddComponent (UObject * obj, UClass * ComponentClass)
{
SelectRoot ();
return PerformComboAddClass (ComponentClass, EComponentCreateAction::SpawnExistingClass, nullptr); /// Protected method need to be Run in Child of SSCSEditor Class
} #endif
Second you add to your custom actor or blueprint function library this function (.cpp file):
#if WITH_EDITOR
if (GEditor)
{
if (BlueprintAsset)
{
GEditor->SelectActor (dynamic_cast<AActor *>(BlueprintAsset), false, false, false, true); /// Refresh Actor instance - Unselect Actor
TSharedPtr<SEdit> sedit;
sedit = SAssignNew (sedit, SEdit)
.EditorMode (EComponentEditorMode::ActorInstance)
.ActorContext (dynamic_cast<AActor *>(BlueprintAsset));
Comp = sedit->DoAddComponent (BlueprintAsset, ComponentClass);
sedit.Reset (); /// Remove Shared Pointer Reference. Because it is only one reference, Object "sedit" is destroyed.
GEditor->SelectActor (dynamic_cast<AActor *>(BlueprintAsset), true, true, true, true); /// Refresh Actor instance - Select Actor
};
}; #endif
return Comp;
}
This code take me many days to dig into Editor code and many trials.
This code concern UE 5.0.3.
For UE 4.27.2 code different a little.
I think Epic guys should expose this function for programmers in some kind of API. Same as many others functions and do not force developers to use tricks.