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"

Where are instances of Default PlayerControllers and Pawns created in GameMode?

Hello,

I'd like to control the spawning of the default PlayerControllers and Pawns that I'm setting in the GameMode constructor but I don't know which function would be optimal to override. The reason is that I want to do further setup on the one instance of Controller and Pawn I make. Is there a specific function that I should look into?

Product Version: UE 4.9
Tags:
more ▼

asked Oct 26 '15 at 04:05 PM in C++ Programming

avatar image

killerpinkhair
1 1 2 5

avatar image Moss Oct 26 '15 at 04:17 PM

Do you want to change the default classes or do you want to hook into the processes where those are getting spawned?

avatar image killerpinkhair Oct 26 '15 at 04:20 PM

I'd like to hook into the processes where those are getting spawned. Basically, looking for where something like this is called, (Note: don't know what it would look like but something like this)

playerController = NewDefaultPlayerController()

playerController->setPawn( NewDefaultPawn() )

note: can't seem to make template brackets.

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

1 answer: sort voted first

Hooking into the spawning process of a PlayerController

To hook into the process when the GameMode spawns you PlayerController you can override the following method in which the GameMode does the actual spawning:

 /** 
  * Spawns a PlayerController at the specified location; split out from Login()/HandleSeamlessTravelPlayer() for easier overriding 
  *
  * @param RemoteRole the role this controller will play remotely
  * @param SpawnLocation location in the world to spawn
  * @param SpawnRotation rotation to set relative to the world
  *
  * @return PlayerController for the player, NULL if there is any reason this player shouldn't exist or due to some error
  */
 virtual APlayerController* SpawnPlayerController(ENetRole InRemoteRole, FVector const& SpawnLocation, FRotator const& SpawnRotation);

You can also override the following which will get called just after the GameMode has spawned a new PlayerController or after a SeamlessTravel (I recommend hooking your logic into the next function):

 /**
  * handles all player initialization that is shared between the travel methods
  * (i.e. called from both PostLogin() and HandleSeamlessTravelPlayer())
  */
 virtual void GenericPlayerInitialization(AController* C);

Hooking into the spawning process of your Pawn aka Character

The spawning process will try to spawn a Pawn aka Character at a given PlayerStart, you will be able to modify the final class implementing the following BlueprintNativeEvent.

 /** returns default pawn class for given controller */
 UFUNCTION(BlueprintNativeEvent, Category="Game")
 UClass* GetDefaultPawnClassForController(AController* InController);

To override the base event just add the following to your header class and add the implementation into your cpp, remember to call the Super.

 virtual UClass* GetDefaultPawnClassForController_Implementation(AController* InController) override;


And to control the actual spawning process you have to implement the following BlueprintNativeEvent (just add the implementation but not the declaration into your header):

 /**
  * @param    NewPlayer - Controller for whom this pawn is spawned
  * @param    StartSpot - PlayerStart at which to spawn pawn
  * @return    a pawn of the default pawn class
  */
 UFUNCTION(BlueprintNativeEvent, Category="Game")
 APawn* SpawnDefaultPawnFor(AController* NewPlayer, class AActor* StartSpot);

To override the base event just add the following to your header class and add the implementation into your cpp, remember to call the Super.

 virtual APawn* SpawnDefaultPawnFor_ImplementationAController* NewPlayer, class AActor* StartSpot) override;

You should be good to got with those hooks.

more ▼

answered Oct 26 '15 at 05:30 PM

avatar image

Moss
6.3k 127 98 606

avatar image killerpinkhair Oct 26 '15 at 05:36 PM

Thanks so much. I really appreciate the answer and the speed :)

avatar image Moss Oct 26 '15 at 05:38 PM

If it works for you remember to accept the answer ^^

avatar image killerpinkhair Oct 27 '15 at 02:04 PM

Actually, after reading a bit more, I understand that SpawnDefaultPawnFor needs to be "implemented", like you said but it's never called. My controller function is called but my SpawnDefaultPawnFor_Implementation is never called. Any ideas would be much appreciated.

avatar image Moss Oct 27 '15 at 02:25 PM

That's strange, I'll prepare a test once I'm back home.

avatar image Moss Oct 28 '15 at 07:35 AM

Ok, I missed a small thing when overriding BlueprintNativeEvents, you have to declare the override of your implementation in your header. I'll update the answer according ^^.

avatar image killerpinkhair Oct 28 '15 at 11:38 AM

So that is what I did, but it doesn't seem to be called. I wrote the implementation function as an override in the header and defined it in the cpp but when I step through the editor at runtime, the SpawnPlayerController function is called but my breakpoint in the SpawnDefaultPawnFor_Implementation is skipped completely.

avatar image Moss Oct 28 '15 at 01:36 PM

Might it be that your BP is not calling the parent function?

avatar image killerpinkhair Oct 28 '15 at 01:58 PM

Very well could be. The only way this function is called is through blueprints?

avatar image Moss Oct 28 '15 at 02:34 PM

No but the call hierarchy is between C++ and BP, so if you do not call the parent/super method you will break the chain somewhere.

avatar image killerpinkhair Oct 28 '15 at 02:37 PM

In the c++, I call the super method inside the "implementation" like so...

 APawn* ALexusGameMode::SpawnDefaultPawnFor_Implementation( AController* NewPlayer, AActor* StartSpot )
 {
     auto pawn = Cast<AnotherPawn>( Super::SpawnDefaultPawnFor( NewPlayer, StartSpot ) );
         // further setup here.
     return pawn;
 }

avatar image Moss Oct 28 '15 at 03:50 PM

And you do not have any BP that implements that function?

(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