I have been following the weapon essential tutorial series on youtube, and even have tried doing it my own way, and for the life of me I can not get this to work.
I able to spawn a weapon into the world an attach to the actors socket, however whenever I try to spawn it from the inventory, the weapon is spawned into the world and functions but refuses to attach to the socket.
I have a USphereComponent
to detect collision with weapons:
void AThirdPersonCharacter::OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
//If we have collided with a pickup
APistolWeaponPickup *Pistol = Cast<APistolWeaponPickup>(OtherActor);
if (Pistol)
{
Inventory[1] = Pistol->GetClass();
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Picked up pistol");
Pistol->Destroy();
}
AAssaultRifleWeaponPickup *Rifle = Cast<AAssaultRifleWeaponPickup>(OtherActor);
if (Rifle)
{
Inventory[0] = Rifle->GetClass();
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Picked up rifle");
Rifle->Destroy();
}
}
Everything appears to be functioning normal. But then I have this spaghetti madness which pulls the weapon stored in the first index of the TArray.
void AThirdPersonCharacter::EquipPrimaryWeapon()
{
//Equip our primary weapons
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Trying to equip");
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
AWeaponPickup *Spawner = GetWorld()->SpawnActor<AWeaponPickup>(Inventory[0], SpawnParams);
if (Spawner)
{
if (CurrentWeapon != NULL)
{
for (int32 i = 0; i < 3; i++)
{
if (Inventory[i] != NULL && Inventory[i]->GetDefaultObject<AWeaponPickup>()->WeaponConfig.WeaponName == CurrentWeapon->WeaponConfig.WeaponName)
{
Inventory[i] = NULL;
Inventory[i] = CurrentWeapon->GetClass();
}
}
CurrentWeapon->Destroy();
Spawner->CollisionSphere->SetCollisionEnabled(ECollisionEnabled::NoCollision);
Spawner->AttachRootComponentTo(GetMesh(), "RifleSocket", EAttachLocation::SnapToTarget);
CurrentWeapon = Spawner;
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, Spawner->WeaponConfig.WeaponName);
}
else
{
Spawner->CollisionSphere->SetCollisionEnabled(ECollisionEnabled::NoCollision);
Spawner->AttachRootComponentTo(GetMesh(), "RifleSocket", EAttachLocation::SnapToTarget);
CurrentWeapon = Spawner;
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, Spawner->WeaponConfig.WeaponName);
}
}
}
My TArray in my header of defined in the following way:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Inventory)
TArray<TSubclassOf<AWeaponPickup>> Inventory;
Again the issue is not the weapon not spawning but the weapon not attaching to the character’s socket when being spawned from an array. I have even tried via blueprint and it failed. Not sure if its a bug or something obvious staring me in the face.
Also I made sure the socket is correct!