Download

Giving AActor a static mesh and enabling physics

I have an AActor that I am creating as a basis for all objects that going to be pickup-able. One of the shared features I want is a StaticMesh and a hitbox with physics simulation. Most of my attempts have resulted in no mesh being visible, or crash on startup. I want to be able to choose the hitbox and StaticMesh in the pickup-able’s constructor. How would I do this?

I guess it all really depends on how you want to set it up. If you wanted to do it purely by code alone see below.
Personally I think exposing something to blueprints would be best here. Allowing you to pick and choose assets on the fly, instead of having to recompiling all the time.

In your Class’ .h:
// – Object Inti Const-- //
// Use the old object initializer methodology
myClassName(const FObjectInitializer& ObjectInitializer);

// This will allow you to set the mesh you want in the blueprinted version
UPROPERTY(VisibleAnywhere, Category = Mesh)
UStaticMeshComponent* myMesh;

// This will allow you to set the collision box
UPROPERTY(VisibleAnywhere, Category = Collision)
TSubobjectPtr<UBoxComponent> collisionComp;

In the .cpp(obviously yours is going to be A<ClassName>::A<ClassName>, but you get the idea)
myClassName::myClassName(const FObjectInitalizer& ObjectInitializer)
: Super(ObjectInitalizer)
{

// Create a default sub object, and set it as the mesh.  
 myMesh = ObjectInitializer.CreateDefaultSubobject&lt;USkeletalMeshComponent&gt;(this, TEXT("Our Mesh"));

// Set up the mesh how you want here
// Example: allowing it to cast a dynamic shadow in the game world
myMesh-&gt;bCastDynamicShadow = true;
myMesh-&gt;bHiddenInGame = false; 

// you could set the mesh as the root or not(I would)
 RootComponent = myMesh; 


 //   -- For the collison sphere, there are two ways -- // 
 // One just added in the blue prints to get the size set how you want, and by your question this is the route i would go.
 // if you prefer the code way right way here it it. 
 
 // Create the Default Subobject
 collisionComp = ObjectInitializer.CreateDefaultSubobject&lt;UBoxComponent&gt;(this, TEXT("Collision")); 
 
 // Set the size(here its set to 0(you will need to determine the size you want))
  collisionComp-&gt;InitBoxExtent(FVector::ZeroVector); 

  // You can set up anything else this sub object needs to do here.. 
  // Example:
  collisionComp-&gt;bCastDynamicShadow = true;     // if you wanted it to cast a shadow for some reason 
  collisionComp-&gt;bHiddenInGame = true;             // if you wanted it to be hidden in game 
 

 // Attach  the collision comp to the mesh(root)
collisionComp-&gt;AttachParent = RootComponent; 

}