Component added at runtime doesnt have BeginPlay called on it

Hello, So i created a Blueprint Function Library in C++, to allow me to add a component to an actor based on a passed in class, the only problem is, the component that is getting added to the actor is never having BeginPlay called on it.

Note: I am calling my AddComponentByClass custom BP function, in the Actor’s BeginPlay event in the Actor’s blueprint. Yet my Component’s BeginPlay event, in its Blueprint, is never getting called.

From what i understand, all Default Components of an Actor have its BeginPlay called before the Actor’s BeginPlay if im not mistaken, so how can i go about calling the newly added Component’s BeginPlay event, when it may be added after the Actor has already been Initialized?

Ive tried calling RegisterComopnent, but it doesnt do anything, cause the component is already automaticlly registered when its created. Ive also already tried calling BeginPlay on the component after it is created and that crashes the Engine with an Assertion Failed: bRegistered.

Here is my code for my BP Function.

//Adds a UActorComponent Subclass, that is based on the passed in Class, and added to the Outer(Actor) object
UActorComponent* UFSBlueprintFunctionLibrary::AddComponentByClass(TSubclassOf<UActorComponent> Class, UObject* Outer)
{
	if (Class != nullptr && Outer != nullptr)
	{
		UActorComponent* Component = NewObject<UActorComponent>(Outer, *Class);
		if (Component != nullptr)
		{
			Component->RegisterComponent();
			return Component;
		}
		else
		{
			return nullptr;
		}
	}

	return nullptr;
}

Ive tested in the Actor Blueprint, that the return value is valid, so Component->RegisterComponent() is definitely being called, but BeginPlay is still never getting called for the component.

Edit: After i added the RegisterComponent(), i was then able to call BeginPlay() on the component afterwards, but it still seems like the Actor should call BeginPlay instead of me having to call it myself?

Yes, ive tried setting it to true, before and after ive called RegisterComponent.

The component i am adding is a BlueprintableComponent that inherits from ActorComponent, so no C++ is involved, and added a Call to the Parent:BeginPlay in the component, but that doesnt seem to work either. Maybe its a bug?

Im calling it at the end of the Actors BeginPlay in Blueprints.

II tried calling it, after the Actor had been spawned in (So after it had BeginPlay called on it), and it works now :slight_smile:

Thank you for the help, you provided much information and i thank you for that! :slight_smile:

For all that are interested, here is my final code for my BP Function that works wherever you call it.

//Adds a UActorComponent Subclass, and adds it to the Outer Actor.
UActorComponent* UFSBlueprintFunctionLibrary::AddComponentByClass(TSubclassOf<UActorComponent> Class, AActor* Outer)
{
	if (Class != nullptr && Outer != nullptr)
	{
		UActorComponent* Component = NewObject<UActorComponent>(Outer, *Class);
		if (Component != nullptr)
		{
			if (Outer->HasActorBegunPlay())
			{
				Component->bWantsBeginPlay = true;
			}

			Component->RegisterComponent();

			if (!Outer->HasActorBegunPlay())
			{
				Component->BeginPlay();
			}

			return Component;
		}
		else
		{
			return nullptr;
		}
	}

	return nullptr;
}