How to get player character in PlayerController

Hello everyone

I have problem getting the Character in my PlayerController

I have this code:

void ATTSPlayerController::SetupInputComponent()

	ATTSCharacter* Character = Cast<ATTSCharacter>(GetControlledPawn());


The intellisense works fine and I can access my Character’s functions, but When I try:

GEngine->AddOnScreenDebugMessage(1, 1.0f, FColor::Red, FString::SanitizeFloat(Character->Health));

And the engine says: Access violation reading location 0x00000000000004F0.

It doesn’t seem like I’m doing something right. Can you guys give me a hand?

I assume you did a null check before that line? Maybe declare the FString outside of the function call.

Ignore intellisense, all thats telling you is that the syntax is ok, it doesn’t tell you anything about the logic. Have you checked the cast? does it return a non null value? You should always check pointers before using them anyway. So I’d start there.

Are you sure that the controller is possessing that pawn?

Just added the check:

if (!Character)
		GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::Red, TEXT("Character is null!"));
		GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::Green, TEXT("Character got!"));

I get message: Character is null!

So it seems the Character is not getting assigned… So do I have to write somewhere what character should the playercontroller possess?

are you doing the cast inside the input component?

If ATTSPlayerController and ATTSCharacter are your default PC and pawn classes then possession happens automatically. It may be that this happens after input component setup though. Try accessing the character later. For example, override PC’s Possess function, call Super::Possess function and then do your stuff.

@CNKIT yes I’m casting inside InputComponent

@NisshokuZK well yeah they are default. I set them in GameMode constructor. So you mean I should override Possess() and inside the function casting to my character?

Edit: Just tried, still not working :confused:

But just to ask… I’m doing InputComponent->BindAxis(“MoveForward”, MyCharacter, &ATTSCharacter::MoveForward()); inside the PlayerController… is that right? Or I should put that inside Character’s SetupPlayerInputComponent()

your better off putting character movement controls inside the characters inputcomponent, stuff inside the player controller input is usually menu controls and anything you want players to have access to no matter what pawn they posses (like opening the pause menu and etc).

I see… I was such idiot trying to put controls there :smiley:

Anyway I’m making multiplayer shooter… can I put XP system inside PlayerController?

you could put it int here but make sure you look into playerstate since you said its gonna be multiplayer, playercontrollers dont get replicated so you would pass the info into the playerstate to be replicated between all client (score screen, modifiers, etc) i believe (i’m still learning replication myself)

Hmm this is going to turn out harder than I thought :smiley:

Don’t give up, once you figure out the ideas behind the game mode related classes you will know quicker what to put where. :slight_smile:

just stick through it, things may see daunting at first but as you get used to the logic and api it becomes clear

Thanks for the advices guys, I’ll keep learning :slight_smile: