x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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. 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.

Product Version: UE 4.12
Tags:
more ▼

asked Sep 03 '16 at 06:29 PM in C++ Programming

avatar image

PavelK.
38 2 4 10

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Sep 05 '16 at 07:33 PM

avatar image

ImVawx ♦♦ STAFF
6.8k 114 18 128

avatar image PavelK. Sep 05 '16 at 08:49 PM

Hello Kyle 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?

avatar image ImVawx ♦♦ STAFF Sep 05 '16 at 09:01 PM

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 RootComponent of the AShip, you can use:

RootComponent

avatar image PavelK. Sep 05 '16 at 09:19 PM

Yes, I make it work with RootComponent.

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());
         }
     }
avatar image PavelK. Sep 05 '16 at 09:20 PM

Thank you!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question