Download

Multiplayer runtime actor spawn and possession

Hi guys,

I have been attempting to learn multiplayer to develop myself, I think I understand RPCs and generally the basics.

I am stuck right now trying to dynamically spawn in a character (based on a selection) and make that player possess it. E.g:

Player 0: Selects Character A.
Player 1: Selects Character B.

I have a timer running on the server, once it reaches 0, it runs a function on the Server to update all the player controllers. This calls a function running on the clients player controller to hide their character select widget, once done, inside that function, it attemps to create a character and possess it. For the sake of getting the spawn and possess thing figured, I am not doing anything with the selected characters (just spawning a basic third person character).

The server successfully spawns a character for itself and possesses it, but not the client. I can’t figure out why?

Right now when reaching the spawn/possess it calls a function on the server that runs a client rpc which spawns/possess the character. I tried also with multi casts but it doesn’t work either.

Function on the game mode called when the timer is 0.

void ADuelingGameMode::RS_OnTimerOver_Implementation()
{
	for (auto Element : m_apcAllControllers)
	{
		Element->RC_TimerOver();
	}
}

which calls:

void ADuelingController::RC_TimerOver_Implementation()
{
         ... removed widget, sets input mode.

	RS_SpawnCharacter();	
}

which is:

void ADuelingController::RS_SpawnCharacter_Implementation()
{
MC_SpawnCharacter();
}

and finally:

void ADuelingController::MC_SpawnCharacter_Implementation()
{
	const FVector& spawnLoc = FVector(100, 100, 100);
	AProject_ArenaCharacter* Char = GetWorld()->SpawnActor<AProject_ArenaCharacter>( PCharacter, spawnLoc, FRotator::ZeroRotator );
	Possess( Char );
}

the location and character is all temporary for the time being.

Can anyone tell me where my logic fails and what I am doing wrong?

I think you might want to have a look through GameModeBase, GameMode, and any other subclasses of GameMode* which have plenty of functionality for spawning players into the world :slight_smile:

Are you talking about OnLogin? I am using that but the issue is the characters only get spawned AFTER a timer has began, so i can’t do it easily in that event.

Or do you mean something else?

It’s been a really long time since I’ve dug through this logic, and had to do anything with it (talking more than a decade, really…) but I’m pretty sure that Pawn creation is usually done in GameModeBase::RestartPlayer()

In the default PlayerController::StartFire(), if the player is not currently playing, it calls ServerRestartPlayer, which is a server replicated function call that calls GameModeBase::RestartPlayer(), which eventually figures out where to place a pawn, what pawn to place, spawns it in that location, and gives it to PlayerController:Possess() (or is it PossessedBy…)

so, i think what you want to do, is use the GameMode functions to spawn the player pawn, rather than rolling your own in the player controller… or at least, read the logic in RestartPlayer() and follow that around, so you can see how it’s done by default, and that might help you figure out why it’s not working for you

Will check that out, thanks!

1 Like