# Code Third Person - Multiplayer

**URL:** <https://forums.unrealengine.com/t/code-third-person-multiplayer/278672>\
**Category:** Multiplayer & Networking\
**Tags:** Networking, online, unreal-engine, Multiplayer, question\
**Created:** [March 27, 2014, 3:38pm UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672 "2014-03-27T15:38:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_62636dc0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_62636dc0](https://forums.unrealengine.com/u/anonymous_user_62636dc0)\
**Post date:** [March 27, 2014, 3:38pm UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672/1 "2014-03-27T15:38:59Z")

</div>

Hey Guys,

So I have started a new project with the Code Third Person startup kit, I was reading about replication and how the pawns are automatically synched if replication is enabled, so I start two clients on one client I can move with point and click fine, on the other client however no pawn is loaded and instead the camera just stays in the middle of the level, I know both clients are connected because when on the client which has the pawn loaded I move to a location in front of the camera on client B I can see that the pawn is being synched properly.

On another note disabling the playercontroller and defaultpawn override in C++ actually makes it work, both clients connect to eachother in first person.

So my question is, what am I doing wrong? And why isnt the same process that creates the pawn for the first client not creating the pawn on the second connect client?

I am fairly new to Unreal so this is probably something trivial I have missed out.

Thank you.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8bc0661f1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8bc0661f1](https://forums.unrealengine.com/u/anonymous_user_8bc0661f1)\
**Post date:** [March 27, 2014, 5:52pm UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672/2 "2014-03-27T17:52:16Z")

</div>

Elyanar - can you try this: move the first client away from the starting spawn point, then type ‘reconnect’ on the second client. I noticed a bug in the third person code template where respawning doesn’t retry if the initial attempts. This is a work around until that bug can be fixed.

---

<div class="post-metadata">

**Author:** ![Straits\_2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Straits\_2](https://forums.unrealengine.com/u/Straits_2)\
**Post date:** [March 27, 2014, 7:20pm UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672/3 "2014-03-27T19:20:05Z")

</div>

I was having this problem too, this does indeed fix it. Is there a way to setup a different spawn point for each client that connects?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8bc0661f1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8bc0661f1](https://forums.unrealengine.com/u/anonymous_user_8bc0661f1)\
**Post date:** [March 27, 2014, 7:22pm UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672/4 "2014-03-27T19:22:01Z")

</div>

At a quick glance, just adding a new spawn point doesn’t fix the issue. The problem is in the actual spawning / player controller state machine code (The server is never trying extra spawn points, or trying to respawn when the client clicks). I will need to dig a little deeper. This is working in some of our other games (Shooter game for example).

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_62636dc0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_62636dc0](https://forums.unrealengine.com/u/anonymous_user_62636dc0)\
**Post date:** [March 27, 2014, 9:18pm UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672/5 "2014-03-27T21:18:30Z")

</div>

That seems to be a problem, although I dont know how there must be a way to assign another spawnpoint or a spawn position. Thank you very much for the help!

---

<div class="post-metadata">

**Author:** ![Aaranaf\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Aaranaf\_1](https://forums.unrealengine.com/u/Aaranaf_1)\
**Post date:** [March 28, 2014, 1:50am UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672/6 "2014-03-28T01:50:01Z")

</div>

What happens I believe is that the actor fails it’s Pre-spawn checks: [What happens when an Actor is spawned - Unreal Wiki](http://wiki.beyondunreal.com/What_happens_when_an_Actor_is_spawned#Pre-spawn_checks) and since there is something there the actor will not spawn.

What I did do get this to work was to override ChoosePlayerStart() in the gamemode class and from there you can return an appropriate starting location. If you iterate over all SpawnPoints (this is an TArray where all the starting position are stored) and check if there is a collision, if not return that player start location else keep looking. There is an example of this in the Shooter game, check the functions ChoosePlayerStart(), IsSpawnpointAllowed() and IsSpawnpointPreferred() in ShooterGameMode.h.

Hope it helps.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [March 28, 2014, 2:16am UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672/7 "2014-03-28T02:16:01Z")

</div>

#ShooterGame Spawn Code

The entire code you need to add to your Game Mode class is right here:

This is from the shootergame example!

I use this code in my own project and I know it works for **real multiplayer open ip games**

#ShooterGame Game Mode Class

#.h

```
/** select best spawn point for player */
	virtual AActor* ChoosePlayerStart(AController* Player) OVERRIDE;

/** check if player can use spawnpoint */
	virtual bool IsSpawnpointAllowed(APlayerStart* SpawnPoint, AController* Player) const;

	/** check if player should use spawnpoint */
	virtual bool IsSpawnpointPreferred(APlayerStart* SpawnPoint, AController* Player) const;

```

#.cpp  
AActor\* AShooterGameMode::ChoosePlayerStart(AController\* Player)  
{  
TArray\<APlayerStart\*\> PreferredSpawns;  
TArray\<APlayerStart\*\> FallbackSpawns;

```
	for (int32 i = 0; i < PlayerStarts.Num(); i++)
	{
		APlayerStart* TestSpawn = PlayerStarts[i];
		if (IsSpawnpointAllowed(TestSpawn, Player))
		{
			if (IsSpawnpointPreferred(TestSpawn, Player))
			{
				PreferredSpawns.Add(TestSpawn);
			}
			else
			{
				FallbackSpawns.Add(TestSpawn);
			}
		}
	}

	APlayerStart* BestStart = NULL;
	if (PreferredSpawns.Num() > 0)
	{
		BestStart = PreferredSpawns[FMath::RandHelper(PreferredSpawns.Num())];
	}
	else if (FallbackSpawns.Num() > 0)
	{
		BestStart = FallbackSpawns[FMath::RandHelper(FallbackSpawns.Num())];
	}

	return BestStart ? BestStart : Super::ChoosePlayerStart(Player);
}

bool AShooterGameMode::IsSpawnpointAllowed(APlayerStart* SpawnPoint, AController* Player) const
{
	AShooterTeamStart* ShooterSpawnPoint = Cast<AShooterTeamStart>(SpawnPoint);
	if (ShooterSpawnPoint)
	{
		AShooterAIController* AIController = Cast<AShooterAIController>(Player);
		if (ShooterSpawnPoint->bNotForBots && AIController)
		{
			return false;
		}

		if (ShooterSpawnPoint->bNotForPlayers && AIController == NULL)
		{
			return false;
		}
	}

	return true;
}

bool AShooterGameMode::IsSpawnpointPreferred(APlayerStart* SpawnPoint, AController* Player) const
{
	ACharacter* MyPawn = Player ? Cast<ACharacter>(Player->GetPawn()) : NULL;
	if (MyPawn)
	{
		const FVector SpawnLocation = SpawnPoint->GetActorLocation();
		for (FConstPawnIterator It = GetWorld()->GetPawnIterator(); It; ++It)
		{
			ACharacter* OtherPawn = Cast<ACharacter>(*It);
			if (OtherPawn && OtherPawn != MyPawn)
			{
				const float CombinedHeight = (MyPawn->CapsuleComponent->GetScaledCapsuleHalfHeight() + OtherPawn->CapsuleComponent->GetScaledCapsuleHalfHeight()) * 2.0f;
				const float CombinedRadius = MyPawn->CapsuleComponent->GetScaledCapsuleRadius() + OtherPawn->CapsuleComponent->GetScaledCapsuleRadius();
				const FVector OtherLocation = OtherPawn->GetActorLocation();

				// check if player start overlaps this pawn
				if (FMath::Abs(SpawnLocation.Z - OtherLocation.Z) < CombinedHeight && (SpawnLocation - OtherLocation).Size2D() < CombinedRadius)
				{
					return false;
				}
			}
		}
	}

	return true;
}
```

---

<div class="post-metadata">

**Author:** ![Sam\_Hi\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sam\_Hi\_1](https://forums.unrealengine.com/u/Sam_Hi_1)\
**Post date:** [March 28, 2014, 10:10am UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672/8 "2014-03-28T10:10:20Z")

</div>

You can also include the **ShouldSpawnAtStartSpot** function override:

```
// ShooterGameMode.h

/** always pick new random spawn */
virtual bool ShouldSpawnAtStartSpot(AController* Player) OVERRIDE;

// ShooterGameMode.cpp

bool AShooterGameMode::ShouldSpawnAtStartSpot(AController* Player)
{
	return false;
}

```

---

<div class="post-metadata">

**Author:** ![DJMidKnight](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DJMidKnight](https://forums.unrealengine.com/u/DJMidKnight)\
**Post date:** [April 15, 2014, 7:06am UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672/9 "2014-04-15T07:06:40Z")

</div>

Was there any resolution to this problem?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_482d02a4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_482d02a4](https://forums.unrealengine.com/u/anonymous_user_482d02a4)\
**Post date:** [September 20, 2014, 6:59am UTC](https://forums.unrealengine.com/t/code-third-person-multiplayer/278672/10 "2014-09-20T06:59:35Z")

</div>

This is very helpful and i have to ask… could you show how to use this code to actually spawn a player with a controller in the world??? i mean it gets you the spawnpoints - ok. but i’m having problems to spawn actors? i wanted to spawn a ‘local’ second player.  
this would be very helpful 🙂
