Cast to First Person Character C++

Hi guys.

I’m trying to make the equivalent of this Blueprint in C++:

Well I have the OnActorBeginOverlap event, what I need is the Cast To FirstPersonCharacter.

How do I achieve this casting in the class?

Someone suggested my this

void AHealth::OnPickup(AActor* OtherActor)
{
	AFirstPersonCharacter* OtherCharacter = Cast(OtherActor);

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("on actor begin overlap works!")));
}

But it doesn’t work: AFirstPersonCharacter is undefined

Cast uses templates so to get it work it needs you to specify class in template arguments (or whatever those are called)

AFirstPersonCharacter* OtherCharacter = Cast<AFirstPersonCharacter>(OtherActor);

It says that AFirstPersonCharacter is undefined and that Cast doesn’t require those parameters…

Do you have the #include "FirstPersonCharacter.h" in the beginning of your .cpp file?

I already tried, but it says No suck file or directory, it means there isn’t such .h

Well, what is your character class called? I just blindly assumed your character class was AFirstPersonCharacter. Just replace those AFirstPersonCharacter with the name of your character class name (which you probably can find in VisualStudio by looking into Solution Explorer or alternatively from Content Browser in UE Editor)

For example, if you can find MyProjectNameCharacter.h from VS Solution Explorer or if there is actor called MyProjectNameCharacter under “C++ Classes” in Content Browser, you get the name in code by simply prefixing that with an “A”, thus in this example that would make your character class name in code AMyProjectNameCharacter

Solved :slight_smile: thanks!

But the problem is that now I need the equivalent of other nodes like SET ACTOR HIDDEN IN GAME or PLAY SOUND AT LOCATION

I CAN’T open a new topic for every Blueprint node i want to transform in C++ code, I need kind of a wiki where it’s written how to convert this stuff.

I’m stuck

Most of those ‘nodes’ exist as member functions and properties of the actor. For example, in answer above, you could access them with OtherActor->SetActorHiddenInGame(...)

A full list of AActor members can be found here, and from there it shouldn’t be too hard to navigate to other classes as well. Also, remember that UE4 documentation and google (or whatever is your preferred search engine) are your best friends :slight_smile:

Yeah, sure; but I also speak about nodes like Play Sound at Location and Delay (float)

Well yeah, UE4 documentation can be quite quirky and limited when it comes to certain things. But in most cases for me at least API Reference and documentation (and of course google) have been enough to come up with solutions, even that it might have taken a hour or two of searching and testing things.

I currently read that it doesn’t exist a Delay function in C++, but I have to use multithreading. I’ll open a new post for that lol, on the forum though