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"

[C++] Post-login get Character

Hi,

I'm trying to get the character controlled by the given APlayerController in the AGameMode::PostLogin function. But APlayerController::GetContolledPawn, as well as APlayerController::GetCharacter returns... Nothing

Is that normal ? How can I get the pawn owned by the APlayerController ?

Thanks for your help

Product Version: UE 4.10
Tags:
more ▼

asked Mar 08 '16 at 09:09 AM in C++ Programming

avatar image

Ismuur
118 11 13 18

avatar image Stockholm Mar 10 '16 at 09:05 AM

I'm having the same issue, I searched on the internet during hours and I found no solution, maybe we are doing something wrong

void AInvadersGameMode::PostLogin(APlayerController *NewPlayer) { Super::PostLogin(NewPlayer);

 AAInvadersAvatar *avatar = Cast<AAInvadersAvatar>(NewPlayer->GetPawn());

}

When I do something on "avatar" like avatar->SetTeam(TeamEnum::Blue); the game is crashing. It occurs only in Standalone Mode, I don't understand.

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

1 answer: sort voted first

Hey Ismuur-

I'm not sure exactly what your code looks like, but the following code works for me:

 void AMyGameMode::PostLogin(APlayerController *NewPlayer)
 {
     Super::PostLogin(NewPlayer);
     APawn* NewPawn = NewPlayer->GetControlledPawn();
     if (NewPawn)
     {
         if (GEngine)
         {
             GEngine->AddOnScreenDebugMessage(-1, 4.f, FColor::Yellow, TEXT("Testing"));
         }
     }
 }

When I play in editor or standalone mode I do see "Testing" printed to the screen. Since you are trying to get the pawn, you'll want to make sure that you are casting to the pawn class so that the cast will succeed. Let me know if this helps.

Cheers

Doug Wilson

more ▼

answered Mar 10 '16 at 01:55 PM

avatar image Ismuur Mar 14 '16 at 11:33 AM

Hi ! Thanks for the answer! Although this does not change our problem... This is the code I use for tests :

 void AInvadersGameMode::PostLogin(APlayerController *NewPlayer)
 {
     Super::PostLogin(NewPlayer);
 
     APawn*    pawn = NewPlayer->GetControlledPawn();
     AAInvadersAvatar *avatar = Cast<AAInvadersAvatar>(pawn);
     if (GEngine)
     {
         if (pawn)
             GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Cyan, "Pawn exists");
         else
             GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Cyan, "Pawn does not exists");
         if (avatar)
             GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Cyan, "Avatar exists");
         else
             GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Cyan, "Avatar does not exists");
     }
 }
 

And the result is "Pawn does not exists" and obviously "Avatar does not exists". This issue is happening only in Standalone Mode

I have no idea where this can come from

avatar image Doug E ♦♦ STAFF Mar 14 '16 at 03:03 PM

I copied your code into my game mode and "Pawn exists" printed in both Standalone and PIE modes. What result do you get in PIE mode on your end? Do you have the same result in a new project with this same code? Additionally, can you provide a copy of the project where you are having this problem?

avatar image Ismuur Mar 15 '16 at 12:13 PM

In PIE, "Pawn exists" and "Avatar exists" are both printed.

We created a new project, and we have the same result with this code, it works in PIE but not in Standalone Mode.

You can find a copy of our project here : https://www.dropbox.com/s/a5k0n25b04370w6/Invaders.rar?dl=1

avatar image Doug E ♦♦ STAFF Mar 15 '16 at 05:10 PM

Could you explain the steps you used to reproduce this in a new project? I copied the code from your PostLogin function (changing AAInvadersAvatar to ACharacter for the code to compile) into a new project . In stand alone mode it printed that both the pawn and avatar exist.

avatar image Ismuur Mar 16 '16 at 02:04 PM

Yeah sure : We created a new project, then a ACharacter based class (we tested in c++ and in blueprint). Then we created a new C++ GameMode, copy paste the code, and run. And it does not work... Now we just try our project on 4.11 preview 7, still the same problem.

avatar image Doug E ♦♦ STAFF Mar 17 '16 at 04:11 PM

I may not be following the steps correctly, I'm not seeing pawn and avatar set properly in PIE or standalone.

  1. Create a new project

  2. Add Class based on Character (MyCharacter)

  3. Add Class based on GameMode (MyGameMode)

  4. In MyGameMode.h I add void PostLogin(APlayerController *NewPlayer) override;

  5. In MyGameMode.cpp I copied the function you posted earlier, changing reference to InvaderGameMode to MyGameMode and changing references to AInvadersAvatar to MyCharacter

  6. In MyGameMode.cpp, also add include statements for MyCharacter.h and Engine.h

  7. After compiling, I set GameMode Override to MyGameMode

At this point when I PIE I see that the pawn exists but the avatar does not. Is there a step I'm missing or something I'm not setting as you are in your project? Let me know where my steps differ from what you're doing in your project.

avatar image Ismuur Mar 21 '16 at 04:34 AM

Hi Doug,

The steps seems similar to what we done in our project. In order to be more accurate, I just followed the exact same steps in a new project.

The result is still the same for me, Pawn exists but not Avatar when I PIE, and in Standalone Mode, none of them exists...

avatar image Sean L ♦♦ STAFF Mar 21 '16 at 03:33 PM

Hello,

Could you please provide the project that you are using when you are experiencing this error, as we have been unable to reproduce it on our end. Go ahead and zip up your project, upload it to dropbox, and provide a link. You can PM me the link to download the project on the forums: https://forums.unrealengine.com/member.php?160394-Sean-Flint

Thank you

avatar image Sean L ♦♦ STAFF Mar 28 '16 at 07:43 PM

After some investigation, it seems the reason that you are seeing your pawn and avatar not exist in standalone is because post login is being called before your avatar exists in the game. In PIE, a copy of your avatar is created initially, which is why you are seeing different results.

This is an order of operations issue, and in order to solve it you'll need to move your code over to a function that will be called after the avatar exists in the level.

Let me know if you have any further questions.

Have a great day

(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