StaticMesh C++ call and GetAllSocketNames game crash

Hi!

I’ve just started to learn C++ and trying to apply it within UE4. I have a problems with static meshes and sockets at this point.

  1. I have an Actor class in and I want to get it’s all sockets in order to manipulate with them as an array or only by names (still by using array’s indexes). but just to be simple here is situation:

  2. In my header file am defining function:

    UFUNCTION(BlueprintCallable, Category = Debug)
    void GetSockets();

  3. And in cpp file:

    void AShi::GetSockets()
    {
     // BUG - Game crashes when function is called
     /*
     auto ShiRoot = GetOwner()->GetRootComponent()->GetAllSocketNames();
     auto temp = ShiRoot.Num();
     UE_LOG(LogTemp, Warning, TEXT("Actor has sockets: %i"), temp);
     */
    }
    
  4. Game crashes when function is called (within BeginPlay).

My Actor’s static mesh is defined via blueprint. Is it critical to define it within the code to make it work? I want to put in these sockets a smaller object (C++ StaticMeshComponent, not an Actor! - is it possible??)

I have tried the following as well
In the header file:

// Array 
TArray<FName> TuArray;

void GetTuSockets();
void SpawnTu(FName _TuSocketName);

UPROPERTY(Category = BlueprintSpawn, EditDefaultsOnly, BlueprintReadOnly)
TSubclassOf<ATu> TuG;

// Local StaticMeshComponent reference
UPROPERTY(Category = Mesh, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
class UStaticMeshComponent* StaticMeshComponent;
// Local StaticMesh reference
UStaticMesh* StaticMesh;

cpp:

constructor:

// Create static mesh component
StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Static Mesh"));
StaticMesh = StaticMeshComponent->StaticMesh;

    void AShi::GetTuSockets()
    {
      TuArray = StaticMeshComponent->GetAllSocketNames();
      // Applying to all sockets on the static mesh
      for (int i = 0; i < TuArray.Num(); i++)
      {
	      SpawnTu(TuArray[i]);
      }
    }

Game crashes anyway.

Hey PavelK,

I am not experiencing any issue with getting all of the socket names of a mesh. My use case:

void AAH481776Character::OnFire()
{
    FVector SpawnLoc = GetActorLocation( ) + FVector( 0.f, 0.f, 128.f );
    
    ASocketActor *SpawnSocketActor = GetWorld( )->SpawnActor<ASocketActor>( ASocketActor::StaticClass( ), SpawnLoc, GetControlRotation( ) );
    if( SpawnSocketActor )
    {
        FVector Direction = GetControlRotation( ).Vector( ).GetSafeNormal( );
        FVector NewVelocity = Direction * 1200.f;
        SpawnSocketActor->Mesh->SetPhysicsLinearVelocity
        
        TArray<FName> SocketNames = SpawnSocketActor->Mesh->GetAllSocketNames( );
        for( int Name = 0; Name < SocketNames.Num( ); Name++ )
        {
            UE_LOG( LogTemp, Warning, TEXT("Socket: %s"), *SocketNames[ Name ].ToString( ) );   
        }            
    }
}

Which results in the UE_LOG of:

LogTemp:Warning: Socket: Socket
LogTemp:Warning: Socket: Socket0
LogTemp:Warning: Socket: Socket1
LogTemp:Warning: Socket: Socket2
LogTemp:Warning: Socket: Socket3

Which are all the sockets on the Mesh attached to the Actor I am spawning.

If you want, you can post your crash log and I can try to help track down why you are crashing but at this time, I do not believe that GetAllSocketNames( ) is the reason for your crash.

GetOwner( ) is returning nullptr, probably because AShip doesn’t have an Owner set. I’d look into that first. Or, if you just want the of the AShip, you can use:

Hello Langley, thank you for the reply.

So, the function is the following:

   void AShip::GetSockets()
   {
       // BUG - Game crashes when function is called
       auto ShipRoot = GetOwner()->GetRootComponent();

       if (ShipRoot)
       {
	       auto StaticShipRoot = ShipRoot->GetAllSocketNames();
	       auto temp = StaticShipRoot.Num();
	       UE_LOG(LogTemp, Warning, TEXT("Ship has sockets: %i"), temp);
       }
    }

Visual Studio debugger says the following:

   Exception thrown: read access violation.
   this->Owner was nullptr.

and shows ShipRoot definition. I am calling this function via the blueprint by pressing a binded key (before it function call was within BeginPlay in c++ class, but game crashed instantly).

My Pawn is defined via Blueprints (Static meshes, Sockets filled by hand etc.) for the time being and I want to try spawn some other Actors within the sockets, but cant reach the Root component.

How can I protect a pointer of the ShipRoot variable and get it’s mesh?

Yes, I make it work with .

Here is the code:

	auto ShipRoot = GetRootComponent();
	if (ShipRoot)
	{
		auto SocketNames = ShipRoot->GetAllSocketNames();

		for (int SocketName = 0; SocketName < SocketNames.Num(); SocketName++)
		{
			UE_LOG(LogTemp, Warning, TEXT("Socket: %s"), *SocketNames[SocketName].ToString());
		}
	}

Thank you!