How to replace a component with a different class (Same actor)

Is there a way in the editor to take an existing component that you added, and change it to a subclass of that same component? The only way I see to do it is to completely remove the component and create a new one, and then I have to go through every script where the old one was referenced and fix it manually. This can be a nightmare if you have a lot of existing blueprint code.

2 Likes

Hi, did u find a way?

Found a way to do this by calling the following static function from within an AssetActionUtility blueprint

void UMyGenericUtils::MigrateComponents(UObject* Object)
{
	UBlueprint* Blueprint = Cast<UBlueprint>(Object);
	if (Blueprint && Blueprint->SimpleConstructionScript)
	{
		const TArray<USCS_Node*>& ScsNodes = Blueprint->SimpleConstructionScript->GetAllNodes();
		TArray<USCS_Node*> OldComponents = {};
		for (const auto ScsNode : ScsNodes)
		{
			if (ScsNode)
			{
				if (const auto SceneComponent = Cast<USceneComponent>(ScsNode->ComponentTemplate))
				{
                                       // Change this logic to your own to target the components you want
					if (SceneComponent->GetName().Contains("Socket")
						&& !SceneComponent->GetName().Contains("Sockets"))
					{
						OldComponents.Add(ScsNode);
					}
				}
			}
		}

		for (const auto OldNode : OldComponents)
		{
			USCS_Node* NewNode = Blueprint->SimpleConstructionScript->CreateNode(
				UBuildingSocket::StaticClass(),
				FName(OldNode->ComponentTemplate->GetName().Replace(TEXT("_GEN_VARIABLE"), TEXT(""))));
                        // Add parent node so that we can SetupAttachment and add a transform
			auto ParentNode = Blueprint->SimpleConstructionScript->FindParentNode(OldNode);
			if (ParentNode) NewNode->SetParent(ParentNode);
			USceneComponent* OldComp = Cast<USceneComponent>(OldNode->ComponentTemplate);
			USceneComponent* NewComp = Cast<USceneComponent>(NewNode->ComponentTemplate);
			NewComp->SetRelativeTransform(OldComp->GetRelativeTransform());
			NewComp->ComponentTags = OldComp->ComponentTags;
			Blueprint->SimpleConstructionScript->AddNode(NewNode);
			// Blueprint->SimpleConstructionScript->RemoveNode(OldNode);
		}
	}
	else
	{
		UE_LOGFMT(MyLogDefault, Error, "Could not cast to blueprint");
	}
}

Make sure the BP you wish to migrate is closed before running or else it wont work. This will auto increment the component names as well so that it doesn’t overwrite the existing ones, so uncomment the RemoveNode line after you are satisfied with your changes.

This doesn’t work well, there are weird problems with root bone .
I’m trying to replace all ISMC to HISMC.

There is something like this for the character movement component.