Passing USkeletalMeshComponent by reference is not working as expected


I’ve been trying to implement an equipment system. I have SkeletalMeshComponents for different equipment parts in my HumanCharacter class.

USkeletalMeshComponent* LegsEquipmentComponent;
USkeletalMeshComponent* FeetEquipmentComponent;

I ınitialize these components in HumanCharacter constructor as below:

CreateSubObjectMesh(FeetEquipmentComponent, FName("FeetEquipmentComponent"));

CreateSubObjectMesh method:

void AHumanCharacter::CreateSubObjectMesh(USkeletalMeshComponent* Comp, FName Name)
    Comp = CreateDefaultSubobject<USkeletalMeshComponent>(Name);
    Comp->RelativeLocation = FVector::ZeroVector;

Comp should directly effect FeetEquipmentComponent as far as I know. But it doesn’t.

I have EquipmentManager class which holds a reference to HumanCharacter class as “Owner”. Through this reference I assign mesh of these SkeletalMeshComponents:

void UEquipmentManager::EquipItem(UEquipment* Equipment)

Here, Owner->FeetEquipmentComponent is acting like it is not assigned and causing editor to crash. If I remove CreateSubObjectMesh method and directly work on FeetEquipmentComponent everything works without a problem. But I do not want to replicate this for each component.

What am I missing here?

You are using Pass-by-pointer, whereby a copy of the pointer is passed to the function.

You need to pass the pointer by reference:

USkeletalMeshComponent*& Comp

Thanks! Makes sense, I will try that.