Get variables from casted character

Hi, i’m just starting to learn c++ for UE4, so assume that in know very little about c++.
I was trying to make a simple pickup object, so made the actor and everything and then I setup the collisions, everything works fine, when I step on the pickup object the overlap event gets called correctly… now my question, if i wanted to restore the character health when i walk on the object how would I do it? My character has the variable health already setup but how can I access it inside the pickup object?

Hello Daf4x,

In your overlap function you should have a AActor* parameter. You will need to do the following inside your overlap function:

  1. Include the header file of your player character class in your pickup.h (above #include “pickup.generated.h”)
  2. Cast the overlapping actor to whatever your player class is called
  3. Check that the cast was successful
  4. If the cast was successful you need to dereference the actor to access the health variable

If the cast works you can perform operations on the variable (addition, subtraction, etc).

Thanks,

Example Pickup.cpp code:

//This casts the actor that overlapped the pickup

APlayerCharacter* LocalActorReference = Cast<APlayerCharacter>(OverlappingActor);

//Make sure that the cast is went through

if(LocalActorReference != nullptr)
{
     LocalActorReference->Health += 20;
}