x

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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How do I call a method from Another Class?

Hey Community I had a quick question for you. I've got two classes, one Character(Default 3rd Person) and an Actor class that i've turned into a "pick-up" sort of item. I'd rather not use blueprints but I was wondering something...

How exactly would I call a function inside the character from the other actor class? I've already tried &AProjectX2Character::numSubtract;

However the function in the character class doesn't seem to be responding. I was wondering if you could offer abit of some friendly advice?

p.s

I'm guessing it isn't pointing to the correct spot but i'm not sure if that's true because there is only one instance of AProjectX2Character. (That I know of.)

Thanks a bunch!

(As requested Mr. Rama. :D)

Product Version: Not Selected
Tags:
more ▼

asked Jun 15 '14 at 04:12 AM in C++ Programming

avatar image

Mark James G
534 44 24 51

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

2 answers: sort voted first

Thanks Mike for requesting the code :)

Solution

 void ALightSwitchCodeOnly::OnOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
 {
     AProjectX2Character* MyChar = Cast<AProjectX2Character>(OtherActor);  
    if(MyChar)
   {
       MyChar->numSubtract();
    }
 }


Summary

You need to use the passed in Actor ptr and cast it to your custom class

if the cast passes you can access the functions on that particular instance of your character class

Rama

more ▼

answered Jun 15 '14 at 05:51 AM

avatar image

Rama
10.6k 446 345 1087

avatar image Mark James G Jun 15 '14 at 05:56 AM

Casting! Ah I knew I was missing something! Darn it I kept trying some of the older pointer tricks but it didn't work, haha.

I should probably check out those blueprints for reference when exploring a little bit more in C++(At least for Unreal Engine!). Thanks a bunch Rama, as usual.

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

Post your code please. there's nothing special to do to call a function if you have the reference and the function is public. You can do that in either code or blueprints.

more ▼

answered Jun 15 '14 at 04:54 AM

avatar image

mikepurvis
2.5k 102 66 261

avatar image Mark James G Jun 15 '14 at 04:59 AM
 // LightSwitchCodeOnly.cpp
 
 void ALightSwitchCodeOnly::OnOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
 {
     GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Green, FString::FString("Toggled on!"));
     &AProjectX2Character::numSubtract;
 }
 
 // AProjectX2Character.cpp
 
   void AProjectX2Character::numSubtract()
 {
     testingNum = testingNum - 1;
     GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Cyan, FString::FromInt(testingNum));
     GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Black, FString::FString("I was touched!"));
 }
 
 

Not a problem. No need to post the ENTIRE bit of code and the #include's, yadda yadda yadda. Just wondering why this particular part isn't working.

(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