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:
- Include the header file of your player character class in your pickup.h (above #include “pickup.generated.h”)
- Cast the overlapping actor to whatever your player class is called
- Check that the cast was successful
- 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;
}