Announcement

Collapse
No announcement yet.

SpawnActor wrong location and attach fail in C++

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SpawnActor wrong location and attach fail in C++

    I've tried to spawn an weapon and attach it to my character

    But it
    a). is spawned in wrong location
    b). doesn't attached to character

    However, I've found that this issue depends on where I place the code.

    If I place the code in PostInitializeComponents(), it will spawned and attached as I expected.

    However, if I place it in a function, for example, in Tick(), the issue will occur.

    So what I want to know is why this happen?

    Can I fix it? (If I want it to be a function, not in PostInitializeComponents() )

    here's the code I use:

    if( bpWeapon )
    {
    AWeapon *weapon = ( AWeapon * ) GetWorld()->SpawnActor<AWeapon>( bpWeapon, GetActorLocation(), GetActorRotation() );

    if( weapon )
    {
    const USkeletalMeshSocket *socket = GetMesh()->GetSocketByName( "hand_rSocket" );
    socket->AttachActor( weapon, GetMesh() );
    }
    }

    #2
    I found that the reason that the actor spawned in wrong location because I didn't override the Tick() in weapon class. It makes sense since the actor has to keep refine its location in each frame;

    However, I still cannot attach the weapon to my character. I hope that someone can explain the cause.

    Comment


      #3
      Hey just curious about this because I've never called the AttachActor from the socket, I always call it on the thing I want attached, so:
      Code:
      weapon ->AttachRootComponentToActor(this, "hand_rSocket", EAttachLocation::SnapToTarget, false);
      Although I can't remember if that auto-casts to FName for the socket name, so you may need to fix that.
      Dev Blog http://www.error454.com

      Comment


        #4
        Originally posted by hyperdr1ve View Post
        Hey just curious about this because I've never called the AttachActor from the socket, I always call it on the thing I want attached, so:
        Code:
        weapon ->AttachRootComponentToActor(this, "hand_rSocket", EAttachLocation::SnapToTarget, false);
        Although I can't remember if that auto-casts to FName for the socket name, so you may need to fix that.
        Actually after restarting the unreal editor, the issue will be fixed, which means you can't just recompile the project.
        All I need to do is restart the program.

        Comment

        Working...
        X