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"

Associating a pawn with a player controller.

Hello, I was curious on how to go about creating or associating a pawn with a corresponding player controller. Should I create a "pawn" instance within the constructor of the PlayerController or should I create two separate classes, one being the PlayerController and the other being the pawn. According to the documentation on PlayerController, You definitely want some functionality in the PlayerController as opposed to the pawn, such as in a death match scenario.

I was wondering how to go about setting this up, for I want to be able to give commands to the Pawn through the player controller, and save data such as "score" in the PlayerContoller as opposed to the pawn, for I want that information to save.

Any direction would be much appreciated, Thanks!

Product Version: UE 4.9
Tags:
more ▼

asked Nov 07 '15 at 05:53 PM in C++ Programming

avatar image

LostScout
135 18 28 35

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

1 answer: sort voted first

Hello LostScout,

Whenever you are controlling a character or pawn at runtime, you can think of the character or pawn as a puppet and the player controller as the strings. Pawns that are being controlled are possessed by a PlayerController. In this sense, the PlayerController is linked to the pawn. You can easily store any information you like in the PlayerController and base events off the values listed there rather than looking at the Pawn's values. An example would be destroying the possessed Pawn when the PlayerController's health value reaches 0. It can be then be reset to max after 5 seconds, respawn the Pawn, and possess the Pawn.

Hope this helps!

more ▼

answered Nov 09 '15 at 06:42 PM

avatar image LostScout Nov 14 '15 at 08:09 PM

I this case, would I be calling the possess function in the playercontroller class like... [ this->possess(mypawn) ] or would a create in instance within mypawn and create a pointer object MyPlayerController and then do [ MyPlayerController->possess(mypawn) ] ?

The answer to this question will allow me to visualize the connection

avatar image Matthew J Nov 16 '15 at 03:19 PM

You would use possess(mypawn) in the PlayerController's class as the second method you mentioned would be creating a new PlayerController instance which wouldn't have any of the values that you had set up to that point, which could cause some odd behavior and loss of data. A PlayerController is automatically created for you as the start of play if you set your Default PlayerController Class to match your custom class.

avatar image LostScout Nov 16 '15 at 11:06 PM

would this mean that in my PlayerController`s constructor I would do MyPlayerController = DefaultPlayerController; or would you have to use the GetPlayerController() function somehow?

  • thanks in advanced

avatar image Matthew J Nov 17 '15 at 02:52 PM

When you set up your game mode, there is a field for your Default PlayerController which can be set to your specific PlayerController. Once this is done, your PlayerController class will be the default, and an instance of that class will be spawned when the session begins.

avatar image LostScout Nov 18 '15 at 01:04 AM

so, how would I reference that instance? In terms of some code, would I use the this-> keyword? Once I have set MyPlayerController as the default playercontroller within the gamemode, how would I reference and call the possess function in MyPlayerController`s constructor? How do I specify that instance of MyPlayerController to possess a pawn(code example would help).

avatar image Matthew J Nov 19 '15 at 03:35 PM

You would reference the same way that you would in Blueprints, which means using the GetPlayerController function which is located in the UGameplayStatics function library.

To include UGameplayStatics, add this to your includes:

 #include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"

To use the GetPlayerController function, you'd use this syntax:

 ACustomPlayerController* MyController = UGameplayStatics::GetPlayerController(this, 0);

If you expect that there may be different types of PlayerControllers, you could cast instead to be safe:

 ACustomPlayerController* MyController = Cast<ACustomPlayerController>(UGameplayStatics::GetPlayerController(this, 0));
 
 if(MyController)
 {
     *logic here*
 }

If the cast fails, MyController will be left with a null value which will cause the If statement to fail as well, avoiding any mistakes in the case of getting a playercontroller of a different class.

avatar image LostScout Nov 19 '15 at 07:34 PM

Thank you!

avatar image 1999 Nov 19 '15 at 08:22 PM

I am wanting to do a closely related thing, except I am still waiting for the C++ compiling error to be fixed. I have a number of pawn classes(Currently just 2) and I was using Blueprint Widget "Posses" to change classes.

(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