Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: Not Selected
more ▼

asked Oct 15 '15 at 10:38 PM in C++ Programming

avatar image

3 1 2 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Oct 15 '15 at 10:50 PM

avatar image

480 26 12 44

avatar image yarez Oct 15 '15 at 11:17 PM

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*)"

avatar image eagletree Oct 15 '15 at 11:37 PM

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);
avatar image TheGreatCabbage Apr 25 '17 at 06:05 PM

This was really helpful - thanks!

avatar image eagletree Apr 25 '17 at 06:16 PM

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

avatar image yarez Oct 15 '15 at 11:47 PM

That worked perfectly! Thanks a ton!

avatar image eagletree Oct 16 '15 at 01:07 AM

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

avatar image MasBASS Jun 28 '18 at 02:32 AM

What do you really put into the otherActor ?

avatar image eagletree Jun 28 '18 at 03:39 AM

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.

avatar image MasBASS Jun 28 '18 at 06:10 PM

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 :)

avatar image eagletree Jun 28 '18 at 06:24 PM

Glad to hear it, you're welcome.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question