Create Player - Work half the time

Hi everybody,

So, I’m currently trying to make a local two player shared screen setup.
Player 1 is all good, no problem.
The problem is with player 2, it work … half the time. When I play (new editor windows). half the time I do get my second player/character, half the time I do not (and it come down to the “Is Valid” being negative in that case)
I have tried to delay the execution of SpawnP2, or delay right after CreatePlayer. Still got it to work half the time.
It seems random, I guess it’s not, is it related to my computer resources ? The order the game process something ?

PS : the current setup make use of an BlankPawn as default, that is destroy and replaced by the DF-BaseCharacter. Reason being later I will get the player to choose witch character they want to play with.

Thanks.

Stupid question: When you expand the “Create Player” node, is “Spawn Pawn” checked?

Yes it is. When not, it just never work (of course :D)

More information :
I started the game 49 times and got :

  • 21 Valid
  • 28 Is Not Valid

I tried to add Delay at various moment, doesn’t change anything.


Here a record for the 49 runs. (Sorry for the slower connection)

[link text][2]

I tried to reproduce this failure but it works on my machine. I tried it like 20 times but everytime both actors were spawned. Can you share how your spawning events are called?

Sure ! thanks for trying to help.
So the spawn event are called from the level blueprint on begin play.
I figured that, in my gamemode, I can branch the “Is Not Valid” to the “Spawn actor” directly, without the “destroy actor”
At the end it work, but it still bugs me to not understand why half the time the “Create player” got no valid pawn.

I have checked with the debug tool to watch the return value of :

  • Create player
  • Get controlled pawn

The “create player” always return :

DF-PlayerController_C'/Game/Levels/UEDPIE_0_ProtoOne.ProtoOne:PersistentLevel.DF-PlayerController_C_1'

The “get controlled pawn” return half the time None and the other half a similar reference as above.

Okay, although your program runs as wanted I digged a bit into the source code to find the source of this behaviour. So what I think is happening is that your second player controller is created as a spectating one half the time. For spectating controllers no pawn is spawned. To override this strange behaviour consistently, I would check “Spawn Players as Spectators” in your game mode details. Then, you can override the event OnPostLogin which should guarantee that you have a valid player controller. I came up with something like this:

This of course only works if you have enough player starts to iterate through :wink: And I don’t know the behaviour in multiplayer games, because creating players, controllers, spawning default pawns, etc. is bound to network code, so chances are this only executed on the server, but I really don’t know.

And to create the second player, you can simply call “Create Player” after Begin Play (e.g. in your level blueprint).

Alternatively, you can override “Handle Starting New Player” in your game mode. This is the original implementation:

void AGameModeBase::HandleStartingNewPlayer_Implementation(APlayerController* NewPlayer)
{
	// If players should start as spectators, leave them in the spectator state
	if (!bStartPlayersAsSpectators && !MustSpectate(NewPlayer) && PlayerCanRestart(NewPlayer))
	{
		// Otherwise spawn their pawn immediately
		RestartPlayer(NewPlayer);
	}
}

So you can override this in your blueprint and call “Restart Player” everytime instead. Should also work, but I havn’t tried.

Thanks for your answer !
I’ll try that first time in the morning tomorow and let you know more. :wink:

So.
The “spawn players as spectator” was off already.

Adding the “Event OnPosLogin” seems to get rid of the problem.

Didn’t try to override the code as I don’t know how to do so.
Accepted answer.
Thanks again dude :wink:

Worked nicely for me on 4.19! Thanks! :slight_smile: