How do I Register ActorComponents in UE5 blueprints?

Hi! Long time reader, first time poster.

Quick question for you all with a bit of background: I am currently using Unreal Engine 5 preview 1.
Trying to create some simple procedurally created buildings by gluing a bunch of premade assets together. I am doing this through blueprint by Adding an InstancedStaticMeshComponent to the building actor and adding the various walls and floors that I need. Working great, really fast, everything is good. Except…

Whenever I move the building actor, the mesh disappears. From my googling I think I’ve understood that the Component itself hasn’t been saved after it’s been filled with data, so there’s some under the hood redraw process that’s boning me.

So here’s my two related (hopefully) questions:
1.How do I Register the new component that I’ve made through my blueprint so it shows up in the Actor’s hierarchy?
2. How do I save ActorComponent changes that were made via blueprint so when I move the actor the changes don’t disappear or reset?

Thanks for your time, if you need any more info, let me know.

I think add component in the actor’s construct function will works. Just like the room of content example.
If you want add the component dynamic, my way is add a blueprintcallable func in Blueprint library.
For example:

UActorComponent* UPythonBPLib::AddComponent(TSubclassOf<class UActorComponent> ComponentClass, AActor* Actor, USceneComponent* ParentComponent, FName Name)
{
	
	UActorComponent* Result = nullptr;
	if (!ComponentClass.Get())
	{
		UE_LOG(PythonTA, Error, TEXT("AddComponent. ComponentClass is not valid."));
		return nullptr;
	}

	Result = NewObject<UActorComponent>(Actor, ComponentClass.Get(), Name);
	USceneComponent* AsSceneComponent = Cast<USceneComponent>(Result);
	if (AsSceneComponent) 
	{
		AsSceneComponent->AttachToComponent(ParentComponent ? ParentComponent : Actor->GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
		FTransform T;
		AsSceneComponent->SetComponentToWorld(T);
	}
	Actor->AddInstanceComponent(Result);
	Result->OnComponentCreated();
	Result->RegisterComponent();

	Actor->RerunConstructionScripts();
	
	return Result;
}

Then call it in bp or python. I call it in my python tool like this:

meshComp = unreal.PythonBPLib.add_component(unreal.HierarchicalInstancedStaticMeshComponent, actor, actor.root_component)

Then the component will be in the actor’s hierarchy.

Below is my blueprint library documentation.

1 Like

Thanks for the above!

For anyone else wondering if there’s a way to do this in Blueprints only, here’s what I’ve learned:
ActorComponents can be added to Actors but will not show up in the Inspector, nor will data added to them be persisted if the actor is moved.

Variables, on the other hand are persisted. Can components be variables? Yes, but through a proxy.

What I did to make this work was to simply create an Actor “InstancedStaticMeshComponentProxy” and to that actor add the InstancedStaticMesh component.

Then, in my original Actor Component, create an Array of InstancedStaticMeshComponentProxy objects. Whenever I want to dynamically add one, I simply “Create New Instance” in the script and assign whatever mesh and material I want to it.

You can use this method to create mutliple persistent instances of InstanceStaticMeshComponent on the fly, only using blueprints.

2 Likes

Hello!
Could you send a code example?