Adding a Static Mesh Component to a custom actor + IDE woes

Hi guys,

Seen a few answers for similar things on the answer hub but couldn’t get any to function for me. How does one add a static mesh component to an actor they have defined (in my case, AItem). It inherits correctly from Actor but I haven’t found a simple way of doing it!

Additionally, I have put several of my classes in folders (or filters as VS calls them) but when adding a new class via the editor, they are removed. Any way to prevent this?

Hey!

In your header file, you would do this:



UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mesh")
TSubobjectPtr<UStaticMeshComponent> Mesh;


And in your implementation file:




AItem::AItem(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP)
{
      Mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("My Mesh"));
}



Hopefully that is what you were looking for.

About the disappearing classes, I had this same problem the other day. I ended up finding them in “[GameProject]/Intermediate/ProjectFiles/”. After I found them, I closed Visual Studio and the editor, and moved those files to a spot in my Source folder. Then just to make sure everything was alright again, I launched Visual Studio, removed the files I had to move to the Source folder from my project, and re-added them. Hope I helped! Good luck! :slight_smile:

Also you need to set the root component to the mesh so the constructor would look like this.



AItem::AItem(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP)
{
      Mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("My Mesh"));
      RootComponent = Mesh;
}


If you already have a RootComponent and need to attach it to the root component then it would be



Mesh->AttachTo(RootComponent);


I am currently not at my home computer to look at the exact code as this is from memory. So if this is wrong, please correct me.

Also in the header you can’t have the components as readwrite as that won’t compile.

You will need the UPROPERTY set up like so.



UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Mesh")
TSubobjectPtr<UStaticMeshComponent> Mesh;


You will still be able to assign a static mesh to the component but you shouldn’t be allowed to edit/remove the actual component itself.

I would of included this in the above reply but didn’t notice this until I already submitted.

I hope this helps and good luck.

Hey guys,

Thanks a lot for the tips! I did foolishly forget to mention, however, that I’d like to add a mesh at runtime dynamically, so I can’t really do so in the constructor. I could cache a lot of meshes and choose from them at runtime, but it seems silly to load dozens and dozens upfront and just store them in memory. Any tips?

Also another poor choice of wording in my original post - my classes never disappear, but whenever I add one, the folder/filter structure I set up in Visual Studio always disappears and my classes end up as one huge disorganised list.

I would assume you would need to set the component up in the constructor and then in a function you would just need to load in the static mesh and populate the component’s static mesh with the loaded in static mesh. This is how I would do it anyway. You will need a component for the mesh to be placed in and I don’t believe you can add a component at run time. I have successfully added components at run time through blueprints but not sure how to do it in C++. How I am going to go about handling items in my game is have the items themselves be in blueprints that extend my base class or sub class if it needs a little extra functionality or components for that item. I can’t remember off the top of my head how to find a static mesh and load that in to the component but I would assume it is similar to loading in a texture which is below.


static ConstructorHelpers::FObjectFinder<UTexture2D> CrosshairTexObj(TEXT("Texture2D'/Game/Textures/crosshair.crosshair'"));

I would assume it is similar to that of how to do so in UnrealScript which is StaticMesh’Path/To/StaticMesh’ but if someone could clarify that would be great as I will have to figure this out myself for future parts of my game.

I hope this helps you in figuring out what you have to do.

Lol blah! Thanks for the correction dude! I don’t know what I was thinking. That’s why you don’t stay up until 4AM each night kids! lol

Lol yea but that’s when the best ideas come to me.Unfortunately I forget a lot of the syntax at that point as well so I am always fixing syntax more than actual coding.

To create a new component at runtime just do something like this inside your Actor:


NewComp = NewObject<UStaticMeshComponent>(this);
NewComp->AttachTo(GetRootComponent());
NewComp->SetStaticMesh(MyMesh);
NewComp->RegisterComponent();