Trying to Access Box Component

I’ve been trying to create a rudimentary Zelda clone to practice my skills and am getting hung up here.
I have a sword weapon with a box collider I added so I could adjust the hitbox if needed.
image

	SwordHitbox = FindComponentByClass<UBoxComponent>();
	if (SwordHitbox != nullptr) {


		SwordHitbox->SetSimulatePhysics(true);
		SwordHitbox->SetNotifyRigidBodyCollision(true);
		SwordHitbox->BodyInstance.SetCollisionProfileName("BlockAllDynamic");
		SwordHitbox->OnComponentHit.AddDynamic(this, &ASwordWeapon::OnCompHit);
		UE_LOG(LogTemp, Warning, TEXT("Hey looks like you got a hitbox"));
	}

This is how I am attempting to set it up in the constructor. At this stage, I am merely trying to get past my nullptr check. I’ve tried several methods for retrieving the UBoxComponent but have had no luck. What am I missing about how this works?

Do you add your component in cpp?
If you create your component in cpp, it will be easy to setup in the constructor with CreateDefaultSubobject function and it will be exposed to blueprint with property Specifiers

I created the box collider in bp so I could get a visualization when I changed it in case I wanted to make adjustments. Can I change things like the scale of the box collider in the bp editor if I created in cpp?

Where did you write this code?

Yes you can change these parameters when the component is created in cpp. You need to add property specifier like EditAnywhere or EditDefaultsOnly. In this example it is in the private section but you can put it in protected too if you plan a child class needing access to this component

private:

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Component", meta = (AllowPrivateAccess = true))
	class UBoxComponent* TriggerVolume;

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms