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"

Storing Pawn once for all in Player Controller

Hello !

I didn't find the answer to that, so here is my question : Can I cast and store the pawn in his controller once for all ? I'm using my Controller to handle player's inputs, and the Pawn should NOT change during the game. At first, I was handling everything directly in the Pawn itself and it was working fine, but for training purposes, I'm willing to switch everything to the Controller.

So basically what I'm trying to do is to call the moving function that is inside the Pawn from the Controller. The following code works, but force me into casting the pawn for EVERY functions. Plus, this is going to be called everytimes the player try to move or rotate the camera. I tried to make a ACommander * variable in the .h of the Controller and set it like below in the constructor so it's set once for all, but it doens't seem to work.

 void ACommanderController::Test()
 {
     auto pawn = Cast<ACommander>(this->GetPawn());
     if (pawn)
     {
         pawn->TestFunction();
     }
 }

Any idea how I could cast+store the pawn once ? So I could write something like :

     void ACommanderController::Test()
     {
         pawn->TestFunction();
     }

Thanks !

Product Version: UE 4.14
Tags:
more ▼

asked Dec 21 '16 at 02:40 PM in C++ Programming

avatar image

Gnougnou
141 17 16 28

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

The Character Pawn contains all of the movement functions. For most functions such at movement you will have to cast to the Character Pawn repeatedly unless you store a reference to it inside of an variable.

Personally I would not recommend that you store your movement control data in the Player Controller as it is not designed for that.

It can be done with a bit of work but it is unnecessary.

more ▼

answered Dec 21 '16 at 04:51 PM

avatar image

TheSecondSense
150 6 9 17

avatar image Gnougnou Dec 22 '16 at 06:20 PM

https://docs.unrealengine.com/latest/INT/Gameplay/Framework/Controller/PlayerController/ The doc says that the Controller is made for complexe cases and for persistence purposes.

"unless you store a reference to it inside of an variable." This is exactly what I'm asking for. Thing is, at the moment of Controller creation, the Pawn or the link between the 2 entities does'nt seem to exist. So if I cast and store the Pawn in the Controller's constructor, it doesn't work.

(comments are locked)
10|2000 characters needed characters left
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