C++ How to add StaticMesh to Actor?

How are you supposed to add static meshes to Actors?

In UE3 I did this mostly in defaultproperties. I know that is now done in the constructor, but can’t find an example of it. I looked at the pickups in the ShooterGame but they didn’t do that, they did add collision in the constructor.

I want to be able to spawn from code so don’t want to have to link something in a Blueprint.

I found it. not sure if I should delete the question. An example in the ShooterCharacter

	Mesh1P = PCIP.CreateDefaultSubobject(this, TEXT("PawnMesh1P"));
	Mesh1P->AttachParent = CapsuleComponent;
	Mesh1P->bOnlyOwnerSee = true;
	Mesh1P->bOwnerNoSee = false;
	Mesh1P->bCastDynamicShadow = false;
	Mesh1P->bReceivesDecals = false;
	Mesh1P->SkinnedMeshUpdateFlag = SMU_OnlyTickPoseWhenRendered;
	Mesh1P->PrimaryComponentTick.TickGroup = TG_PreAsyncWork;
	Mesh1P->bChartDistanceFactor = false;
	Mesh1P->BodyInstance.SetMovementChannel(ECC_PawnMovement);
	Mesh1P->BodyInstance.SetCollisionEnabled(ECollisionEnabled::NoCollision);
	Mesh1P->BodyInstance.SetResponseToAllChannels(ECR_Ignore);
	Components.Add(Mesh1P);

Dear Michael,

Your code is the way I tend to do it,

but I’ve also done it the fancy new UE4 way and must say it is a lot simpler/faster/less sensitive to assets being moved around in project.

Fancy New UE4 Way

I’ve been informed on multiple occasions (its a great new thing I had no idea about), that you can

  1. simply set up some pointers in your .h file on your base class

  2. and then blueprint that class,

  3. and then set the static mesh / skeletal mesh component properties in the Editor in the Blueprint default properties!

Here’s the code you’d need

/** Skeletal Mesh Comp, Set In BP Default Properties */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category=SkelMeshComponents)
TSubobjectPtr RoyalSwordL;

	
/** Static Mesh Comp, Set In BP Default Properties */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category=StaticMeshComponents)
TSubobjectPtr AwesomeSword;

Advantages Of this Method:

  1. Much less code and less chance of errors as result
  2. Drag and drop visual interface yaaay
  3. if you change name of assets, the BP will update your pointers automatically
  4. you can get access to the components in BP or in C++ very fast

I heavily restricted access from BP / editor itself as my choice, but you could also use

(EditAnywhere, BlueprintReadWrite) for total access and reading/writing

Note of caution:

Never expect that your pointers are valid, always do the null check, especially during your first compile, before going to set the values in the editor

:slight_smile:

Rama

When I try to do this I get an error:
TSubobjectPtr: Subobject properties can't be editable (use VisibleAnywhere or BlueprintReadOnly instead).