How to adjust a float in Class A from Class B?

I am trying to make a PickUp class that when picked up adds 50 to the Character class. The OnHit even works properly as it destroys the object as intended I just can’t figure out how to call variable from other classes. I have tried many different approaches including Casts, #include PickUp.h, and public function calls, so I think it would be easier if someone could just post an example code to show me the proper way or link me to a tutorial that goes over this.

Thank you,
Russ Rockjaw.

Just for the record, the things you’ve tried aren’t mutually exclusive - in fact you need to do them all.

In order to reference another class in your Character class, you will have to #include it. In order to call a function externally it must have the “public” access modifier. In order to access your class you will have to typecast to it.

e.g.

YourPickup.h

public:
	float GetFloatValue();

YourCharacter.cpp

 void YourCharacter::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
 {
	Super::OnHit(OtherActor, OtherComp, NormalImpulse,  Hit);
	
	AYourPickup* pickup = Cast<AYourPickup>(OtherActor);
	
	if(pickup)
	{
			float val = pickup->GetFloatValue();
			// Do other stuff
	}
 }

For a pickup I would probably use overlap…

void AYourPickup::NotifyActorBeginOverlap(class AActor* Other)
{
	Super::NotifyActorBeginOverlap(Other);
	AYourCharacter* YourCharacter = Cast<AYourCharacter>(Other);
	if(YourCharacter != nullptr)
	{
		YourCharacter->SetSomeValue(1234);
	}
}

This helped. I got it to work thank you. For some reason my Code Sample isn’t working here or I would post my code.

I would normally use Overlap for a pickup but for this circumstance I need OnHit.

Thanks though.

Seeing as how you answered my question and gave an alternate solution I’m going to mark you as an answer as well. Who knows it could help someone in the future.