Setting up a ChildActor for my PlayerCharacter, to use as a weapon C++

After fidiling around for quite a while, trying to give my PlayerCharacter a weapon slot for their left hand, I seem to be getting somewhere with UChildActorComponent, but if I pick up my first weapon implemented so far, when I PIE (currently a brick the Player can throw), The brick remains on the ground and does not snap to the Player’s weapon slot, that has been declared as follows:

/** The player's weapon that is currently active in their left hand */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Weapons)
UChildActorComponent* P_LeftHandActiveWeapon; 

(Above code snippet is from the header file for my PlayerCharacter; QuestToBeDefinedCharacter)

I set up this variable in the .cpp file as per so:

P_LeftHandActiveWeapon = NewNamedObject<UChildActorComponent(this, TEXT("LeftHandActiveWeaponComponent"));
P_LeftHandActiveWeapon->ChildActorClass = AQTBDWeapon::StaticClass();
P_LeftHandActiveWeapon->CreateChildActor();
P_LeftHandActiveWeapon->AttachTo(Mesh, FName("LeftHandWeapon"), EAttachLocation::SnapToTarget);
P_LeftHandActiveWeapon->ChildActorName = "LeftHandActiveWeapon";

There is a function that I have declared to set the value of the Player’s active weapon, this is as shown here:

/** Modify the player's left hand active weapon to the given active weapon */
void AQuestToBeDefinedCharacter::ModActWep(AQTBDWeapon* NewActiveWeapon)
{
	/*P_LeftHandActiveWeapon->ChildActor = NewActiveWeapon;
	left commented out for now, as I do not have access to the ChildActor of P_LeftHandActiveWeapon, so that I can test the weapon slot in editor */ 
}

I can then compile the code, that will of course, load the editor, this screenshot shows that P_LeftHandActiveWeapon
is an editable component in Blueprint, as intended and is also attached to the correct socket, on the Player’s left hand:

In spite of this, when I PIE, the weapon is not displayed on the PlayerCharacter’s in game mesh, upon picking up the BrickWeapon. Calling the ModActWep function has no effect, as the only line of code in that function has been commented out, or I cannot PIE, as VS2013 states that UE4 threw an exception.

Does anyone know how to properly set up a UChildActorComponent? As I do not seem to have done the required steps, many thanks.

Edit: I have tried getting UActorComponent or UPrimitiveComponent to do what I wish for, similar to using UChildActorComponent, but I still cannot modify the value of such, does anyone know if there is a certain way to construct these components? Also, could they be used for the same purpose, I was trying to use UChildActorComponent for?

1 Like

Trying out something I thought of, I will edit this post to signify such soon.

Edited my post, edit at the bottom of the post.

Please, can anyone offer their knowledge to help me? I will provide more information if required.

I have managed to figure it out: First off, I have re-declared P_LeftHandActiveWeapon as so:

/** The player's weapon that is currently active in their left hand */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Weapons)
	TAssetPtr<AQTBDWeapon> P_LeftHandActiveWeapon;

Only a pointer is required for the weapons stats, for the weapon’s mesh to be shown on the player, I have declared a UStaticMeshComponent, as per so:

/** The static mesh for the player's currently active weapon */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Weapons)
	UStaticMeshComponent* P_LeftHandActWepMesh;

I then only need to attach the P_LeftHandActWepMesh to the player’s model, in the following manor in QuestToBeDefinedCharacter’s constructor:

// Setup the mesh component for the player's left hand active weapon
	P_LeftHandActWepMesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("LeftHandActiveWeaponMesh"));
	P_LeftHandActWepMesh->AttachTo(Mesh, FName("LeftHandWeapon"), EAttachLocation::SnapToTarget);
	P_LeftHandActWepMesh->SetWorldScale3D(FVector(0.2f, 0.2f, 0.2f));

After compiling and initiating debugging, I add an override material to P_LeftHandActWepMesh, then setup the player character’s blueprint to work differently, to account for the changes in the character’s code, as per so:

Upon picking up the BrickWeapon:

When the BrickWeapon is thrown:

(The Screenshots only show what has been changed, the other nodes in the event graph still execute what is required and did not need changing)