Download

Spawn Weapon, send raycast from reference point

Hi I want to be able to spawn and attach different weapons to a character socket via C++ as well raycast from another socket.
I am creating a third person shooter so I cant have the ray sent from the camera because it will hit objects behind him.
I found this link but I dont know how to utilise the code.


UFUNCTION(BlueprintCallable, Category="Utilities|Transformation", Meta=(Keywords="Bone"))
virtual FVector GetSocketLocation
(
    FName InSocketName
)

after I type this in the header file it gives an error saying its missing a ‘;’, when I add it at the end of ‘FName InSocketName’ it then says it doesnt like how I defined the function. How do I fix this?

The funcion is alredy available in any Class that extends from USceneComponent ( Like USkeletalMeshComponent / Pawn Mesh), And since its alredy defined in USceneComponet you only need to call the function in your .cpp file not the header

So if you wanted to just get a socket location from your character mesh all you would need to do is



// Your Player Character .cpp
FVector* SocketLocation;
SocketLocation = Mesh->GetSocketLocation(SocketNameHere);


ty for the quick response but not im getting this error


Error	1	error C2440: '=' : cannot convert from 'FVector' to 'FVector *'

I removed the * from FVector* SocketLocation;, but my code still doesn’t compile

Ah right, had forgotten about the * , Why still it doesnt compile?, Are you sure the code is In your player character and inside a function or event?

its working now, it said there where some broken links to some .dll files, but I closed everything and reopened it and its fine.

I havent finished looking into it but I was wondering if u or someone can point me in the right direction, I want to spawn a weapon at the socket location, attach it to that socket or the bone the socket is attached to, then delete that weapon and spawn a new one at the socket (to simulate swapping weapons). Also to draw a ray from the socket and to where the mouse is aimed at.

when i code something I’ll upload it and ask for help but thought someone might help point me in the right direction

The docs have a LOT of information you should really read, as well the answerhub & wiki

You alredy have the socket location all you need to do is spawn your weapon on its location And attach it , If you search for components / skeletalmeshcomponent in google you can easly find that there is a function called AttachTo , that takes in a mesh component to be attached to and a socket that serves as the location

So if you were to spawn the weapon and attach it to the player hand



// Player Character .cpp

#include "MyWeaponClass.h" // Include your weapon so the compiler recognises the class when we use it 
void MyCharacter::SpawnAndAttachWeapon()
{
// Spawninfo
FActorSpawnParameters SpawnInfo;
FVector SocketLocation;

SocketLocation = Mesh->GetSocketLocation(SocketName); // get the socket location before spawn
AMyWeapon* Weapon = GetWorld()->SpawnActor<AMyWeapon>(MyWeaponClass,SocketLocation SpawnInfo); // spawn the weapon on the socket location

Weapon->Mesh->AttachTo(Mesh, SocketNameHere); // attach the weapon to our pawn and socket 
}


And to “swap” weapons keep the current weapon on a variable i.e “CurrentWeapon”, and once the player pick ups a new one destroy the current and assign CurrentWeapon to the newly picked up weapon