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"

Retreiving a reference to Player Pawn/Character from C++ Player Controller

How would I retreive a non-null reference to a player character/pawn from a C++ Player Controller? I am using GameMode for possession of pawns.

I have tried using GetPawn() during both BeginPlay() and BeginPlayingState() and casted them using Cast() and I keep getting a null reference. As far as I'm aware this is because the possession of the pawn has not yet occured. Do I need to stall or use a timer or is there any specific event where I can guarantee the pawn has been possessed (assuming a valid class has been set in GameMode)?

 // Copyright ©2015 Austin R. Snider and Brandon L. Snider; do not copy modify or distribute without permisson. 
 #pragma once
 
 #include "GameFramework/PlayerController.h"
 #include "PlayerCharacter.h"
 #include "DragonsLairPlayerController.generated.h"
 
 
 /**
  * 
  */
 UCLASS()
 class DRAGONSLAIR_API ADragonsLairPlayerController : public APlayerController
 {
     GENERATED_BODY()
 private:

  //parts omitted

     void MoveForwardAxis(float AxisValue);
     void MoveSideAxis(float AxisValue);
     void LookVertical(float AxisValue);
     void LookHorizontal(float AxisValue);
     void LeftInteract(float AxisValue);
     void RightInteract(float AxisValue);
 
     void SwitchItem1();
     void SwitchItem2();
     void SwitchItem3();
 
     void SwitchPreviousItem();
     void SwitchNextItem();
 
     void UseItem();
     void UseShield();
     void Interact();
 
     APlayerCharacter* PlayerCharacterRef;
     UCameraComponent* PlayerCameraRef;
 
 public:
     ADragonsLairPlayerController(const FObjectInitializer& ObjectInitializer);
     virtual void BeginPlayingState() override;
     virtual void SetupInputComponent() override;
 
     
 };


 // Copyright ©2015 Austin R. Snider and Brandon L. Snider; do not copy modify or distribute without permisson. 
 #include "DragonsLair.h"
 #include "DragonsLairPlayerController.h"
 #include "Engine.h"
 
 
 ADragonsLairPlayerController::ADragonsLairPlayerController(const FObjectInitializer& ObjectInitializer)
 {
     
 }
 
 void ADragonsLairPlayerController::BeginPlayingState()
 {
     Super::BeginPlayingState();
     
     if (Cast<APlayerCharacter>(GetPawn())) PlayerCharacterRef = Cast<APlayerCharacter>(GetPawn());
     else
     {
     }
 
     PlayerCameraRef = PlayerCharacterRef->PlayerCameraRef;
 
 }
 
 void ADragonsLairPlayerController::SetupInputComponent()
 {
     Super::SetupInputComponent();
     InputComponent->BindAxis("MoveForwardAxis", this, &ADragonsLairPlayerController::MoveForwardAxis);
     InputComponent->BindAxis("MoveSideAxis", this, &ADragonsLairPlayerController::MoveSideAxis);
     InputComponent->BindAxis("LookVertical", this, &ADragonsLairPlayerController::LookVertical);
     InputComponent->BindAxis("LookHorizontal", this, &ADragonsLairPlayerController::LookHorizontal);
     InputComponent->BindAxis("LeftInteract", this, &ADragonsLairPlayerController::LeftInteract);
     InputComponent->BindAxis("RightInteract", this, &ADragonsLairPlayerController::RightInteract);
     InputComponent->BindAction("SwitchItem1", IE_Pressed, this, &ADragonsLairPlayerController::SwitchItem1);
     InputComponent->BindAction("SwitchItem2", IE_Pressed, this, &ADragonsLairPlayerController::SwitchItem2);
     InputComponent->BindAction("SwitchItem3", IE_Pressed, this, &ADragonsLairPlayerController::SwitchItem3);
     InputComponent->BindAction("SwitchPreviousItem", IE_Pressed, this, &ADragonsLairPlayerController::SwitchPreviousItem);
     InputComponent->BindAction("SwitchNextItem", IE_Pressed, this, &ADragonsLairPlayerController::SwitchNextItem);
     InputComponent->BindAction("UseItem", IE_Pressed, this, &ADragonsLairPlayerController::SwitchItem1);
     InputComponent->BindAction("UseShield", IE_Pressed, this, &ADragonsLairPlayerController::SwitchItem2);
 }
 
 void ADragonsLairPlayerController::MoveForwardAxis(float AxisValue)
 {
     if (PlayerCharacterRef != nullptr)
     {
         FVector CharacterForwardVector = PlayerCharacterRef->GetActorForwardVector();
         FVector MovementInputVector = CharacterForwardVector * PlayerCharacterRef->MovementSpeed;
         PlayerCharacterRef->AddMovementInput(MovementInputVector, AxisValue);
     }
 }
 
 void ADragonsLairPlayerController::MoveSideAxis(float AxisValue)
 {
     if (PlayerCharacterRef != nullptr)
     {
         FVector CharacterRightVector = PlayerCharacterRef->GetActorRightVector();
         FVector MovementInputVector = CharacterRightVector * PlayerCharacterRef->MovementSpeed;
         PlayerCharacterRef->AddMovementInput(MovementInputVector, AxisValue);
     }
 }
 
 void ADragonsLairPlayerController::LookVertical(float AxisValue)
 {
 
     if (PlayerCharacterRef != nullptr)
     {
         FRotator DeltaRotation = FRotator(AxisValue, 0.0f, 0.0f);
         PlayerCameraRef->AddLocalRotation(DeltaRotation);
     }
 }
 
 void ADragonsLairPlayerController::LookHorizontal(float AxisValue)
 {
     if (PlayerCharacterRef != nullptr)
     {
         FRotator DeltaRotation = FRotator(0.0f, AxisValue, 0.0f);
         PlayerCharacterRef->AddActorLocalRotation(DeltaRotation);
     }
     else 
     {
         PlayerCharacterRef = Cast<APlayerCharacter>(GetPawn()); 
     }
 }
  

Product Version: UE 4.9
Tags:
more ▼

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

avatar image

DesertEagle_PWN
187 10 26 25

avatar image ak|zed Oct 06 '15 at 02:25 AM

AYourCharacter* Pawn = Cast(GetCharacter());

GetCharacter() not GetPawn(), it will work :)))

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

2 answers: sort voted first
     UGameplayStatics::GetPlayerChracter(const UObject * WorldContextObject, int32 PlayerIndex);

where WorldContextObject is usually this (a pointer to object it is called from) and PlayerIndex is usually 0. It will work if player posesses a APlayer or child class object, and not a Pawn.

more ▼

answered Sep 17 '17 at 08:48 PM

avatar image

maximilian758
18 2 2 6

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

AYourCharacter* Pawn = Cast(GetCharacter());

GetCharacter() not GetPawn(), it will work :)))

more ▼

answered Oct 06 '15 at 02:55 AM

avatar image

ak|zed
1 1

avatar image DesertEagle_PWN Oct 06 '15 at 02:26 PM

Already tried it, still doesn't work. I think my problem may be elsewhere because a spectator keeps being spawned by the GameMode instead of my an instance of PlayerCharacter (meaning Q and E raise and lower the camera even though I haven't defined and Q and E inputs in my project). Any ideas what can cause this?

avatar image DJMidKnight Jan 16 '17 at 02:58 AM

Yes if you have a function or call to spawn the spectator on death or after death as this is not normally inherited and should not be caused by your player controller.

Again unless you specified your player controller to spawn a pawn again.

(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