I currently have a project with 2 actor classes, one called Ship and one called Weapon, I want to attach Weapon to Ship but am not sure how I would go about that. I tried making an AClass object called weapon in the Ship script but wasn’t sure what to do with that to make it an object of the Weapon class.
I don’t know if its the correct way to do it but this is what I’m doing.
//In My player class (or your ships header file)
/** Weapon in left hand */
UPROPERTY(EditDefaultsOnly, Category = Projectile)
TSubclassOf<class AWeapon> LeftWeaponClass;
class AWeapon* LeftWeapon;
//In my player (your ship) cpp file
if (LeftWeaponClass != NULL)
{
if (GetWorld() != NULL)
{
const FRotator SpawnRotation = GetControlRotation();
const FVector SpawnLocation = GetActorLocation();
//Set Spawn Collision Handling Override
FActorSpawnParameters ActorSpawnParams;
ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
ActorSpawnParams.Owner = this;
// spawn the weapon
LeftWeapon = GetWorld()->SpawnActor<AWeapon>(LeftWeaponClass, SpawnLocation, SpawnRotation, ActorSpawnParams);
//Attach weapon to the Skeleton, doing it here because the skeleton is not yet created in the constructor
LeftWeapon->AttachToComponent(NameOfMesh, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("Socket_Name"));
}
}
Then I make a blueprint of my player and the TSubclassOf will give me a dropdown of all the classes of that type I can pick from you can read about it here TSubclassOf | Unreal Engine Documentation