Hello everyone I was wondering how I could reference the skeletal mesh of my player from another class. I tried making a skeletal mesh “Variable” by putting USkeletalMesh* Mesh in the header file so that I could set it in a blueprint but that did not work. Is there anyway that I could reference the skeletal mesh of my player? The player mesh is simply called Mesh.
Thanks that works. I have one more question. I am using this on a class for a weapon and when the player pickups the weapon, this line of code is called GunMesh->AttachToComponent(MeshRef, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT(“WeaponSocket”));
When I try to compile I get this error: error C2664: ‘bool USceneComponent::AttachToComponent(USceneComponent *,const FAttachmentTransformRules &,FName)’: cannot convert argument 1 from ‘USkeletalMesh *’ to ‘USceneComponent *’
And: note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Do you know how I can fix this?
What didn’t work when you tried to access it in blueprint ? If you couldn’t see the variable in the derived blueprint class you have to use the UPROPERTY macro, something like this :
UPROPERTY(EditDefaultsOnly, Category = "MyCategory")
USkeletalMesh* MeshRef;
Or is it the player’s mesh that you failed to get ?
Is your MeshRef a SkeletalMesh or a SkeletalMeshComponent ?
Because you cannot attach a SkeletalMesh to something, you should use a USkeletalMeshComponent. You can create a SkeletalMeshComponent, assign it your SkeletalMeshRef and then attach it I think.
It should look something like :
USkeletalMeshComponent* MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("SkeletalMeshComp"));
MeshComp->SetSkeletalMesh(MeshRef);
Not sure if my answer was clear here x)
The error you have says that your Meshref is a SkeletalMesh, it has to be a SkeletalMeshComponent, otherwise you can’t attach something to it.
It is a SkeletalMesh. I am trying to attach a Static Mesh from my gun to the Skeletal Mesh of my player through a socket I have called WeaponSocket.
Ah I see. Do you know of any other way that I could accomplish this? What I am trying to do is make a pickup system similar to what you would see in Fortnite or PUBG where there is an Item on the ground and that is attached to the player.
I am using the default Third Person Character. I don’t need anything too in depth, all I am asking is how would I snap a Static Mesh existing in the level to a socket on the hand of my player’s Skeletal Mesh.
This would be hard without seeing your code, how are you setting up the mesh of your character ? Is it just a variable you created or did you inherit it from the character class ?
I think you can just use the function you tried to use before : AttachToComponent. Just make sure that you are attaching it to a SkeletalMeshComponent, not a SkeletalMesh. That’s what I would use.
Wait, is there an AttachToCharacter function, or something like it? If so how would I go about putting that into code?
My pleasure
Don’t forget to mark the answer as solution in case someone is searching for the same thing, so they can see it was resolved, good luck with your project!
Actually there is an AttachToActor function :
AttachToActor
but I never used it.
Don’t hesitate to search through the documentation for any “AttachTo” function, but I believe you are on the right tracks.
Ok Thank you so much this is very helpful.
Hi sorry to bother you again but can you explain the syntax of the AttachToActor function? What I have so far is this->AttachToActor(PlayerCharacter, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT(“WeaponSocket”)); It doesn’t work even when I have created a scene component in my Weapon Class. By it doesn’t work I mean that it doesn’t attach to my character. The engine doesn’t crash but I can’t figure out the error and the unreal engine documentation doesn’t help at all.
I don’t think I can answer you without more details. I never used this function but it should work like you are trying to use it. Maybe you should open a new question about this if you really need help on this particular function.