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

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

// 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.

Thanks for requesting the code :slight_smile:

#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

#:heart:

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 , as usual.