create USkeletalMeshSocket with function?

I am trying to create a USkeletalMeshSocket inside a function, every time that function is called.

I have this, but it gives a ‘nonstatic member must be relative’ error on CreateDefaultSubobject.


void ACntrls::Target(AStaticMeshActor* item1, AStaticMeshActor* item2)
{
	USkeletalMeshSocket* Target = CreateDefaultSubobject<USkeletalMeshSocket>(TEXT("target"));

}


Is it possible to create Components inside functions like this?

thanks!

You need to specify where you want this subobject to belong to. Generally, you would use it with FObjectInitializer object inside of a constructor. Something like that:


ACustomCharacter::ACustomCharacter(const class FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
    ThirdPersonCameraBoom = ObjectInitializer.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("ThirdPersonCameraBoom"));
    //...
}

USkeletalMeshSocket is not a component, and shouldn’t be getting created at runtime in general (unless you’re building some custom mesh builder or something like that). What are you actually trying to do, attach something to a socket or find a socket or what?

Cheers,
Michael Noland