Spawning player in selectable locations in a network game 



Purpose of this small tutorial is to show how to select spanwpoints from the network client's GUI.

GUI sends data to the server with GameNetworkClient.Instance.CustomMessagesService.SendToServer()

Server receives messages in Dedicated server's MainForm.cs void CustomMessagesService_ReceiveMessage()

GameWorld.cs

change Unit ServerOrSingle_CreatePlayerUnit( PlayerManager.ServerOrSingle_Player player,
MapObject spawnPoint )

to

public Unit ServerOrSingle_CreatePlayerUnit( PlayerManager.ServerOrSingle_Player player,
MapObject spawnPoint )

Comment out creating units


//create units
foreach( PlayerManager.ServerOrSingle_Player player in
PlayerManager.Instance.ServerOrSingle_Players )
{
if( player.Intellect != null && player.Intellect.ControlledObject == null )
{
//ServerOrSingle_CreatePlayerUnit( player );
}
}


SpawnPoint.cs

make "instances" variable public

ActionGameWindow.cs

Add new Control for spawner window


//HUD screen
Control hudControl;
Control spawner;


add buttons that represent the ones in CutSceneDemoTerminal.gui buttons


Button first, second;


make changes to OnAttach() so the buttons accept clicks


//To load the HUD screen
hudControl = ControlDeclarationManager.Instance.CreateControl( "Gui\\ActionHUD.gui" );
spawner = ControlDeclarationManager.Instance.CreateControl( "Gui\\CutSceneDemoTerminal.gui" );

first = spawner.Controls["SimpleCamera"] as Button;
first.Click +=new Button.ClickDelegate(first_Click);

second = spawner.Controls["CurveCamera"] as Button;
second.Click += new Button.ClickDelegate(second_Click);

//Attach the HUD screen to the this window
Controls.Add( hudControl );
Controls.Add( spawner );


Create click-functions that send messages to the server


void first_Click(Button sender)
{
string id = "SPAWN_ME";

string data = "SpawnPoint_0;";
string user = GameNetworkClient.Instance.UserManagementService.ThisUser.Name;

GameNetworkClient.Instance.CustomMessagesService.SendToServer(id,data+user);
Controls.Remove(sender.Parent);
}

void second_Click(Button sender)
{
string id = "SPAWN_ME";

string data = "SpawnPoint_1;";

string user = GameNetworkClient.Instance.UserManagementService.ThisUser.Name;


GameNetworkClient.Instance.CustomMessagesService.SendToServer(id,data+user);
Controls.Remove(sender.Parent);

}


Finally we spawn the player in Dedicated server's Mainform.cs

MainForm.cs


void CustomMessagesService_ReceiveMessage( CustomMessagesServerNetworkService sender,
NetworkNode.ConnectedNode source, string message, string data )
{
//Warning! Messages must be checked by security reasons.
//Modified client application can send any message with any data.

if( allowCustomClientCommands )
{
//load map
if( message == "Example_MapLoad" )
{
string mapName = data;
MapDestroy( true );
if( !MapLoad( mapName ) )
return;
return;
}

//create map object
if( message == "Example_CreateMapObject" )
{
string[] parameters = data.Split( ';' );
string typeName = parameters[ 0 ];
Vec3 position = Vec3.Parse( parameters[ 1 ] );
Quat rotation = Quat.Parse( parameters[ 2 ] );

if( Map.Instance != null )
{
MapObject entity = (MapObject)Entities.Instance.Create( typeName, Map.Instance );
entity.Position = position;
entity.Rotation = rotation;
entity.PostCreate();
}



return;
}

if (message == "SPAWN_ME")
{
string[] parameters = data.Split(';');
string spawn = parameters[0];
string name = parameters[1];

SpawnPoint target=null;

foreach (SpawnPoint point in SpawnPoint.instances)
{
if(point.Name.Equals(spawn))
{
target = point;
}
}

foreach (GameCommon.UserManagementServerNetworkService.UserInfo info in GameNetworkServer.Instance.UserManagementService.Users)
{
if (info.Name.Equals(name))
{

GameWorld.Instance.ServerOrSingle_CreatePlayerUnit(PlayerManager.Instance.ServerOrSingle_GetPlayer(name),target);
}
}
}
}

}