Game Development Community

dev|Pro Game Development Curriculum

Adding Ping to PlayerList.gui

by James Laker (BurNinG) · 02/11/2008 (7:03 am) · 4 comments

I felt like sharing a pretty useless resource and saving people some time, if they're interested.

The ingame Playerlist (F2) out-of-the-box only displayes the score of all the players, and I thought it would be nice to show the players' pings too.

However, this is really just a nice to have and really a waste of bandwidth, since you will have to send each player, all the players' pings.

So what I have done was add 2 more server variables that decide how often ($Pref::Server::PingUpdateTime) a certain amount of clients ($Pref::Server::PingUpdateClients) get updated. This way we save the server and network load a little.
So add the following to your ~/server/prefs.cs:
$Pref::Server::PingUpdateClients = 2;
$Pref::Server::PingUpdateTime = 2;

Now to fire off the function that updates the client Pings, in ~/server/game.cs, function startGame() we add the following at the bottom:
echo("Starting the Ping Scheduler...");
	$Game::PingSchedule = schedule($Pref::Server::PingUpdateTime * 1000, 0, "UpdatePings" );

We also need to Cancel the PingSchedule when the Game ends, so in function endGame() just add this:
//Cancel updating the pings
	cancel($Game::PingSchedule);

Now right at the end of ~server/game.cs we add the UpdatePings function:
// Get Pings
$PingCnt = 0;

function UpdatePings()
{
   
   $PingCnt += $Pref::Server::PingUpdateClients;
   
	for(%clientIndex = ($PingCnt - $Pref::Server::PingUpdateClients); %clientIndex < $PingCnt; %clientIndex++ )
	{
      if(%clientIndex >= ClientGroup.getCount())
      {
         $PingCnt = 0;
         break;
      }
      
      %cl = ClientGroup.getObject( %clientIndex );
		if (!%cl.isAIControlled())
		{
		   %ping = %cl.getPing();
		   %clid = %cl.getId();
		   
         for (%i = 0; %i < ClientGroup.getCount(); %i++)
         {

            %sendTocl = ClientGroup.getObject( %i );
            if (!%sendTocl.isAIControlled())
               commandToClient(%sendTocl,'UpdatePing',%clid,%ping);
         }
		}
			
	}
	
	//Reschedule to run this function
	$Game::PingSchedule = schedule($Pref::Server::PingUpdateTime * 1000, 0, "UpdatePings" );
	
}

If anything in the above function doesn't make sense drop me a line and I'll explain. But it should be straight forward. This runs on the Server, and to send data to the clients we use the ClientGroup collection to get the Clients, and commandToClient(netConnection,'function', var0... varn) to run a function on the client.

In UpdatePings we're telling the clients to run the function 'UpdatePing' with variables ClientId and that Client's ping.

So we obviously need to have the UpdatePing function on the client. So we add the following to ~/client/scripts/playerlist.cs:
function clientCmdUpdatePing(%clientId,%ping)
{
   PlayerListGui.updatePing(%clientId,%ping);
}

NOTE! As you can see above any Commands from the server using commandToClient requires the function to have "clientCmd" added to it.

Now in ~client/ui/PlayerList.Gui at the bottom add the following function:
function PlayerListGui::updatePing(%this,%clientId,%ping)
{
   %text = PlayerListGuiList.getRowTextById(%clientId);
   %text = setField(%text,2,%ping);
   PlayerListGuiList.setRowById(%clientId, %text);
   PlayerListGuiList.sortNumerical(1,false);
   PlayerListGuiList.clearSelection();
}

Finally we just have to add that extra column to the GUI itself. So UP a bit in ~client/ui/PlayerList.Guimake the following changes (:
new GuiTextListCtrl(PlayerListGuiList) {
               profile = "HudTextProfile";
               horizSizing = "width";
               vertSizing = "height";
               position = "1 1";
               extent = "145 16";
               minExtent = "8 8";
               visible = "1";
               helpTag = "0";
               enumerate = "0";
               resizeCell = "1";
               [b]columns = "0 120 150";[/b] //<---- HERE :)
               fitParentWidth = "1";
               clipColumnText = "0";
            };

Now you can just spice it up a bit yourself. Hope you foundnd this usefull :)

#1
02/12/2008 (9:41 am)
good resource!
#2
07/03/2008 (11:40 am)
nice but now after all changes, i can not go ingame anymore
when i press "launch" it just does nothing.. :/
#3
07/04/2008 (7:22 am)
Are there any Console errors?
Have you tried stepping through the code using Torsion?
#4
07/05/2008 (2:04 am)
yep i had a error : D not its fine ..
i found it with Torsion