How can I access class variables from an AActor Instance in OnHit?

I am working on a relatively maze game, in which the player can pick up one of three types of pills. (Class APill, derived from AActor) Within APill, I have three public booleans, isWhite, isBlue, and isRed. Whenever the player runs into a white pill, I want to increment a variable in my player class (AAvatar).

My problem is with the OnHit function in my avatar.

void AAvatar::OnHit(AActor *SelfActor, AActor *otherActor, FVector NormalInpulse, const FHitResult &Hit)

if (GEngine){
	if (otherActor->GetActorLabel().Contains(TEXT("Pill"), ESearchCase::IgnoreCase,ESearchDir::FromEnd)){

		if (otherActor->isWhite) //Error: isWhite is not a member of 'AActor'


I have tried many different methods to somehow access the public variables of the pill that was hit, without any success. Is there a way to do this that I don’t know, or at least a way to modify the OnHit function to accept an APill* rather than AActor?

See if this will work for you. Assuming your otherActor is of class APill, you could say:

APill* MyOtherPillActor = Cast<APill>(otherActor);

That give you a pointer to work with that will see your derived class’s customizations.

This will return a null if you get a hit from something other than an APill.

I get the idea here, which is helpful, but there’s some syntax issue with the Cast. Visual Studio gives me this:

“Error: no instance of overloaded function “Cast” matches the argument list
argument types are (AActor*)”

Argh, sorry. The input form deleted out a portion of my code because I didn’t use the code button, the most important part. I’ll try again:

APill* MyOtherPillActor = Cast<APill>(otherActor);

That worked perfectly! Thanks a ton!

Very glad to hear that. Please remember to tick off the question as answered if it actually worked for you.

This was really helpful - thanks!

Thank you, I hadn’t looked at this in awhile and finally edited the mistake out of the original answer.

What do you really put into the otherActor ?

MasBass, in the context of this question, he was asking about it within an OnHit. In that case, the otherActor is populated by the OnHit when you receive the event and will be the AActor pointer to the actor that was hit. What we are doing with it, is casting it to the extended class (the class is assumed to be extended from AActor and if it’s not, the resulting pointer will be null, so that has to be checked after the cast) so we can access the extended functions and variables contained in that class.

Since OnHit only gives you back an AActor, you have to do this to really get much useful information about who you hit.

Glad to hear it, you’re welcome.

oh ok thank you for this information, I really need it because I’m using a cast after my spawn actor, so I did something like you said and it worked, thank you sir :slight_smile: