How do you add components to an actor class at runtime in C++?

I have my own actor class and a series of ‘building block’ blueprints which I want to add to my actor to build it up based on user input. I think AddComponent is the method I want, however I’m not having much luck getting it to work.

FName TemplateName - I’m not sure what the format of this should be. Should it just be “/Game/Path/BlueprintName”?

UObject * ComponentTemplateContext - I have no idea what this should be. It defaults to the actor class I’m calling this from I think, but I get the impression this should be the class of the blueprint I’m adding… if so, then how do I get that? At the moment I’m just passing NULL.

It runs without error, but nothing appears to happen. No matter what I put for the TemplateName, no error is produced, which leads me to think that it’s just failing silently.

Is this the right approach at all? Does anyone have a working example of doing this sort of thing?

Components.Add was available during the beta. It may still be the case. Something along those lines.

Otherwise, look at how the pawn has its movement component set.

Why are you needing to add components to a class at runtime?

Rama

My ultimate goal is to allow my players to create content in the game from a series of building blocks (specifically space ships a the moment). So I have some building block blueprints that include meshes, lights, interactive elements with Blueprint logic, etc.

The player will create then will be able to pilot the vehicle - so I want to be able to apply forces to the vehicle as a whole. I also want to create further building blocks without going into C++ code, and specify which building blocks are available to a player at runtime.

Maybe I need to spawn and parent them?

#Attach

What you CAN easily do

is spawn Static Mesh Actors

and Attach them to your core ship component that is always created.

Then you can go wild with creating Static Mesh assets and then you can dynamically set the Static Mesh Actor’s Static mesh in code

#Set Static Mesh

//AStaticMeshActor* MySMA in .h
if(!MySMA) return;
MySMA->StaticMeshComponent->SetStaticMesh(your static mesh)

#Dynamic Load Object

See my wiki page to load your various static mesh assets from file path

#Attach

You can create Static Mesh Actors and attach them to a core invisible actor that is the ships’ origin point


Then they will move, rotate, AND SCALE with the core component


so you just move around and scale the core actor, and all attached (at runtime) static mesh actors will follow just as you want


It’s easy!


#Attach, Actor.h

#Use EAttachLocation::KeepWorldPosition

/**
 * Attaches the RootComponent of this Actor to the RootComponent of the supplied actor, optionally at a named socket.
 * @param InParentActor				Actor to attach this actor's RootComponent to
 * @param InSocketName				Socket name to attach to, if any
 * @param AttachLocationType	Type of attachment, AbsoluteWorld to keep its world position, RelativeOffset to keep the object's relative offset and SnapTo to snap to the new parent.
 */

UFUNCTION(BlueprintCallable, meta=(FriendlyName = "AttachActorToActor", AttachLocationType="KeepRelativeOffset"), Category="Utilities|Orientation")

void AttachRootComponentToActor(AActor* InParentActor, FName InSocketName = NAME_None, EAttachLocation::Type AttachLocationType = EAttachLocation::KeepRelativeOffset);

#Set Static Mesh

Spawn your static mesh actor (AStaticMeshActor* MySMA)

if(!MySMA)return;
MySMA->StaticMeshComponent->SetStaticMesh(YourSMAsset)

#Dynamic Load Object

UStaticMesh* YourSMAsset = LoadedObjectFromFilePath

For rest of details see my wiki page

#Implementation

  • create lots of static mesh assets
  • create ONE blueprint of the static mesh actor class
  • spawn this blueprint at runtime
  • set the static mesh asset from file path for this spawned SMA

so you have

One Static Mesh Actor Blueprint = ShipPartBP
and looots of actual static mesh assets

and you attach the spawned Ship Parts to the invisible ship Core Actor

and then only move / rotate / scale the core ship actor

Rama

You are amazing. Thank you for all your input.

You’re welcome!

:slight_smile:

Rama