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?