still not working, but now im getting this error
Weapon->AttachParent(Mesh, SocketLocationR); // attach the weapon to our pawn and socket
Error 1 error C2039: ‘AttachParent’ : is not a member of ‘AMyWeaponActor’
I also tried
Weapon->Mesh->AttachParent(Mesh, SocketLocationR);
Error 1 error C2039: ‘Mesh’ : is not a member of ‘AMyWeaponActor’
sorry, I’ve read the page and I dont see how the weapon gets attached to the player. All I see is; spawning the weapon and some phases that aren’t in the documentation like; Inventory.Num(), EquipWeapon(Inventory[0]), AddWeapon().
I don’t know how to do what you’re trying to do - but I’d guess that if you look at your own post above (where you mention “EquipWeapon()” ) and follow the code you’ll figure it out.
is how I attach my weapon to the FPS Mesh in my project.
I created a socket on the bone of the mesh and then spawn the Weapon Actor and then attach it.
Like this.
bool AMyProjectWeapon::AttachWeaponToPawn(APawn* WeaponOwner, FString SkeletalCompName)
{
OwningPawn = WeaponOwner; // Sets the owner of this weapon. (Note: OwningPawn is a APawn* member in my weapon class.)
SetOwner(OwningPawn); // AActor::SetOwner(APawn*);
// A method i created to get any skeletal component from the owning pawn. (Could be made a template to function to get any type.)
USkeletalMeshComponent* ArmMesh = GetPawnSkeletalComp(SkeletalCompName);
if (ArmMesh) // Check to see if our rig Skeletal Mesh Component is good.
{
// AActor::AttachRootComponentTo(..)
AttachRootComponentTo(ArmMesh, FName(TEXT("WeaponPoint")), EAttachLocation::SnapToTarget); // Attach the root component of our Weapon actor to the ArmMesh at the location of the socket.
return true; // Note: We can only assume it is attached, since epic did not provide a return value.
}
else
{
return false;
}
}
USkeletalMeshComponent* AMyProjectWeapon::GetPawnSkeletalComp(FString ComponentName)
{
TArray<UActorComponent*> Components; // Array for Pawns components
USkeletalMeshComponent* ArmMesh = NULL; // Will hold the skeletal mesh we want
if (OwningPawn)
OwningPawn->GetComponents(Components); // Get the components from the owning pawn
for (UActorComponent* Comp : Components)
{
ArmMesh = Cast<USkeletalMeshComponent>(Comp); // Cast any component to Skeletal Comp.
if (ArmMesh) // If we have a skeletal mesh
{
if (ArmMesh->GetName() == ComponentName) // Check to see if its the one we want.
return ArmMesh;
}
}
return NULL; // Finished with out finding the component we wanted.
}
but im getting errors saying
error C2597: illegal reference to non-static member ‘AMyWeaponActor::OwningPawn’ (a bunch of times)
error C2511: ‘bool AMyWeaponActor::AttachWeaponToPawn(APawn *,FString)’ : overloaded member function not found in ‘AMyWeaponActor’
and a whole bunch of other stuff, what needs doing?
As a public member in YourWeaponClassNameHERE::
And last but not least add the declerations in the header file.
UCLASS()
class AMyProjectWeapon : public AActor
{
GENERATED_UCLASS_BODY()
public:
/*
Attaches the weapon to the pawn and sets Current Weapon to this weapon.
@param WeaponOwner: Is the pawn that the weapon belongs to.
@param SkeletalCompName: The name of the component we want.
@return bool: True if attached the weapon to pawn was successfull.
*/
UFUNCTION()
virtual bool AttachWeaponToPawn(APawn* WeaponOwner, FString SkeletalCompName);
/*
Returns the Skeletal Component for the Pawn "Owner" of the Weapon.
@return USkeletalMeshComponent* or NULL if not found.
*/
UFUNCTION()
virtual USkeletalMeshComponent* GetPawnSkeletalComp(FString ComponentName);
/* The pawn that the weapons belong to. (NULL if on the ground.) */
UPROPERTY()
APawn* OwningPawn;
// ....
};
Let me know if you are stile having issues, should be a simple fix.
It’s telling you that your variable AMyWeapon* wep; is not set to anything but garbage. That means functions can’t be called on it etc. (they can but will just result in an error/crash).
Your variable needs to be instantiated in some way, calling new on it, or calling a function that will return you an instantiated AMyWeapon*.
(I’m new to the C++ side of the engine, so I don’t precisely know how you need to create a new AMyWeapon, but this should help you look in the right place.)
No,
AMyWeapon::AttachWeaponToPawn(APawn* WeaponOwner, FString SkeletalCompName);
can not be declared like that. a nonstatic member reference must be relative to a specific object.