How to use TSoftObjectPtr for meshes?

Class Projectile has a mesh representation:

// Visible mesh
UPROPERTY(EditAnywhere, Category = "Components", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UStaticMeshComponent> Mesh;

The static mesh can then be set in the blueprint class using the details pane of the Mesh component:

setmesh

To reduce what is loaded when Projectile is referenced, TSoftObjectPtr can be used instead of TObjectPtr. However, the static mesh can then not be set in the blueprint. The whole details panel for the Mesh component is blank.

What is the correct way to use TSoftObjectPtr so that the mesh can be assigned:

  1. in the editor before gameplay
  2. dynamically during gameplay using C++?

The issue is due to a misunderstanding of what TSoftObjectPtr should reference. You shouldn’t use TSoftObjectPtr for the component itself, but rather for the mesh asset that the component uses.

UPROPERTY(EditAnywhere, Category = "Components", meta = (AllowPrivateAccess = "true")) TObjectPtr<UStaticMeshComponent> Mesh;

UPROPERTY(EditAnywhere, Category = "Mesh", meta = (AllowPrivateAccess = "true")) TSoftObjectPtr<UStaticMesh> MeshAsset;
1 Like

Thanks! That makes more sense.

1 Like