Hello @ben,
so, I am stuck. To spawn Actor I need to have an Actor class, but we are having a StaticMeshComponent for Turret and Barrel.
In my header file I have added the following:
// Local StaticMeshComponent reference for turret spawning
UPROPERTY(Category = Mesh, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = “true”))
class UStaticMeshComponent* StaticMeshComponent;
// Local StaticMesh reference
UStaticMesh* StaticMesh;
// Array of ALL the turrets on the tank
TArray<FName> TurretArray;
// Array of the spawned turrets on the tank
TArray<FName> SpawnedTurretArray;
// Array of the only LEFT side (1, 3, 5...)
TArray<FName> LeftTurretSocketArray;
// Array of the only RIGHT side (2, 4, 5...)
TArray<FName> RightTurretSocketArray;
void GetTurretSockets();
void SpawnTurret(FName _TurretSocketName);
void DestroyTurret();
In c++:
within the constructor:
// Create static mesh component
StaticMeshComponent = CreateDefaultSubobject(TEXT(“Static Mesh”));
StaticMesh = StaticMeshComponent->StaticMesh;
void ATank::GetTurretSockets()
{
TurretArray = StaticMeshComponent->GetAllSocketNames();
// Applying turrets to all sockets on the static mesh
// TODO: more flexible spawning not to ALL sockets, but some special (by name)
for (int i = 0; i < TurretArray.Num(); i++)
{
SpawnTurret(TurretArray[i]);
}
}
void ATank::SpawnTurret(FName _TurretSocketName)
{
// First we want to make sure these values are all zeroed out
FVector SocketLocation;
FTransform SocketTransform;
SocketLocation = FVector(0, 0, 0);
SocketLocation.Rotation() = FRotator(0, 0, 0);
UTankTurret* TurretGun = GetWorld()->SpawnActor<UTankTurret>(Turret, SocketLocation, SocketLocation.Rotation());
// Adding spawned turret to an array
SpawnedTurretArray.Add(TurretGun);
}
I have problem with calling static mesh (I suppose) and with actor spawning. Can you please help me how to solve it?