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. 

[C++] How do you get APlayerController from ACharacter?

I have been looking for resources on obtaining APlayerController from ACharacter. So far, I see a few questions about enabling input, and answers to those questions saying to override SetupInputComponent method.

But I don't see how to get APlayerController from ACharacter, even though ACharacter should be able to retrieve APlayerController in some way.

Does anyone know how to do this, and if possible, explain it in detail? Thanks in advance.

Product Version: Not Selected
Tags:
more ▼

asked Mar 22 '15 at 05:26 AM in C++ Programming

avatar image

asperatology
293 31 30 91

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

2 answers: sort voted first

I haven't tried this with the default APlayerController, I'm using my own APlayerController(named ATBPlayerController) and get it like this in the ACharacter class(named ATBCharacter)

.h

 ATBPlayerController* player_controller_;

.cpp

 void ATBCharacter::BeginPlay()
 {
     Super::BeginPlay();
         player_controller_ = Cast<ATBPlayerController>(Controller);
 }

this works for me(with a custom PlayerController)

But, ACharacter also has an Controller variable, this variable should be the controller/playercontroller which possesses the ACharacter, so this should work if u are using the default one.

more ▼

answered Mar 22 '15 at 09:47 AM

avatar image

Sleicreider
160 35 33 46

avatar image mpie Mar 22 '15 at 09:54 AM

The controller may change during the game. Instead of adding a new member variable I would add a method like this and call it when needed

 ATBPlayerController* GetATBPlayerController() const
 {
   return Cast<ATBPlayerController>(Controller);
 }
avatar image asperatology Mar 22 '15 at 03:21 PM

I never thought of casting it to APlayerController. Thanks to both of you.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
 APlayerController* player_controller = static_cast<APlayerController*>(this->GetController());
 
 //you might want to use dynamic_cast if you are not sure if it's a APlayerController.
more ▼

answered Apr 08 '18 at 10:40 PM

avatar image

Arty-McLabin
893 20 42 62

(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