How to set Static Mesh?

Hi, guys.
I want to have ability to take some objects in my game, that’s why I created UStaticMeshComponent in my PlayerController file.

.h:

UStaticMesh* MilkMesh;
UMaterialInterface* MilkMat;
...
UPROPERTY(EditAnywhere, category = "StaticMesh")
	UStaticMeshComponent* MeshComp;

.cpp:

LoadObject<UStaticMesh>(MilkMesh, TEXT("/Game/Meshes/MilkPacket.MilkPacket"));
LoadObject<UMaterialInterface>(MilkMat, TEXT("/Game/Materials/MilkPacketMat.MilkPacketMat"));
...
UStaticMeshComponent* MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
MeshComponent->SetStaticMesh(MilkMesh);
MeshComponent->SetMaterialByName(TEXT("MilkPacketMat"), MilkMat);
MeshComponent->SetRelativeLocation(FVector(18, 16, 133));
MeshComponent->SetRelativeRotation(FRotator(-15, -13, 61));
MeshComponent->SetupAttachment(RootComponent);

It doesn’t give any errors, but in MyCharacterBP (BP class, based on this C++ class) MeshComp doesn’t have Static Mesh:

My idea was to change this comp’s StaticMesh to another every time I take or put something. I saw another ways, with creating another C++ class for this object, but I thought my way is much easier. So if you don’t know how to fix my problem, could you, please, at least tell me how to do it in other way?

Your PlayerController and your Character Pawn class are two different classes.

Just because you add a static mesh to the Player Controller doesn’t mean that whatever possessed Pawn object somehow inherits that object.

The right thing to do is to either add a static mesh component to the character, parented to the appropriate hand node (or socket,) or to use “attach actor to actor” when grabbing something, and, again, parent the attached object to the appropriate “hand” object (or wherever else you want to attach it.)

Sorry, I’m new into UE4, so I actually don’t understand what is

I tried to attach this StaticMeshComponent to CameraComponent of my player, but now, when I move my camera, it only moves in Y and Z, but it ignores X-axis. What should I do? And maybe you can explain me, or give example, what is “socket”? Because I tried to search it, but only found that “Socket is part of Skeletal Mesh”, but my project is First Person game, so I don’t have Skeletal Mesh in my Player Pawn. Thank you very much!

I don’t have Skeletal Mesh in my Player Pawn

Don’t you have hands? Don’t you show a weapon of some sort?

If you open up the first person shooter starter project (you can create a new project based on this template, to examine) you can see how gun attachment works for the first-person-hands mesh/skeleton in that game.

If you still choose to not have a skeleton of any kind, then you can parent the picked up object to the root node of the character. You could even create an empty Scene Node and call it “Grabby Hand” and then parent the mesh to that, so you can move it to wherever it will work best in the view.

I tried to attach it to RootComponent(in main question I described it), but then it’s not viewable in Editor(I pinned picture in main question)

Oh, your question is one about the Unreal reflection system, not actually scene graphs!

Components you add to an actor, have to be exposed with UPROPERTY(EditableAnywhere) to actually be editable. And it needs to be a member of your class for UnrealHeaderTool to find it.

Header File:

public:
	// Sets default values for this character's properties
	AMyCharacter();

        UPROPERTY(EditAnywhere)
	UStaticMeshComponent* SomeMesh;


Source File:

// Sets default values
AMyCharacter::AMyCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	SomeMesh = CreateDefaultSubobject<UStaticMeshComponent>(FName("SomeMesh"), false);
}

You don’t generally need to load a mesh and a material directly, either. Instead, once you have the object loaded in the editor, you create a Blueprint subclass of the C++ class, and on that Blueprint subclass you configure the mesh and material you want. Then you specify this Blueprint subclass as the class you want to create wherever you use it (for example, in the GameMode.)

My advice on this bit: Don’t ask why, and don’t try to resist. Unreal Engine is a lifestyle, not a library :slight_smile:

I dit, but now I can’t see details of MeshComponent in Editor:

I posted the code that I used to get that screen shot. If you use that code, and recompile and re-load the plugin, you will see the component and its fields in the editor.

If it was wrong before, and you fixed it, and it still doesn’t show up, then it’s possible that some particular blueprint has still captured the “wrong” state. To fix that, if that’s the case, I haven’t found a better way than to create a new blueprint subclass from your C++ class, and edit that instead.

Ok, it worked, but I still have problem with moving this Mesh Component when I move my camera vertically. Maybe any suggestions?

First person shooter games typically don’t move the camera, because most people can’t change the length of their neck.

If you have a “look spring” type mechanic (where players can look around separately from the aim direction) then there are three options:

  1. Change the character orientation, not the camera orientation – this means that the object will rotate with the camera still. Typical FPS aiming mechanic.
  2. Hide the carried object/s when the look-spring button is pressed. Either animate it down/away, or simply don’t display it.
  3. Parent it to the camera. It’s totally OK to have the property owned by the character, yet make it parented to the camera when you attach it; that will still work fine in the editor.

Yes, I’m using 1 way you described