Game Development Community

Basic Networking Questions

by Nicolai Dutka · in Torque Game Builder · 05/12/2008 (11:07 am) · 11 replies

I have some basic questions and will likely have some more over the next month or so and will add those as they come up. Anyway, let's start with some simple stuff.

I have a server setup and 2 clients connected. Client 1 is running the server.
I am trying to move an object.

function serverCmdPlayerLeft(%player)
{
  //tell both clients to move %player to the left
  %count = clientGroup.getCount();
  for(%i=0; %i<%count; %i++)
  {
    %client = clientGroup.getObject(%i);
    commandToClient(%client,'PlayerLeft',%player);
  }
}

function clientCmdPlayerLeft(%player)
{
  //make %player move
  %player.moveLeft(1);
}

WORKS: commandToClient(1597,'playerLeft',player1);
This makes the player move left.

DOESN'T: commandToServer('playerLeft',player1);
This gives me the following error:
Unknown command moveLeft.

Why could I tell the client to do something and it works, but when I have the server tell it to do the same thing, it does not? There are no errors about 'cannot find object', so I must be referencing the object correctly and if that is true, then telling it to move shouldn't get me an 'unknown command' error....

#1
05/12/2008 (11:46 am)
I guess it might be easier to ask: How do I send variables back and forth from client to server and back to different client?

When I send a commandToServer with a variable, my variable always gets messed up and construed as something else. In the code above under the serverCmd, %player is a local connection even though I told it to be a string or integer.... Maybe I just don't understand how this works?
#2
05/12/2008 (12:02 pm)
The first parameter for a serverCmd function needs to be %client.

eg.
function serverCmdPlayerLeft( %client, %player )

That may work for you.


What is the %player var? Is that the simobject controlled by a client? If so, you cannot assume that its ID will be the same on the client and server. Since ghosting has been removed in TGB you also cannot use getGhostId and resolveGhostId. So that is another area you will need to figure out a solution for.



So, I assume, the player presses a key to move which calls..

commandToServer( 'PlayerLeft', %player );

Then, the server sends a message to all clients telling them the %player object moved?
#3
05/12/2008 (12:06 pm)
The first parameter for my serverCmd does NOT need to be %client because I am adressing ALL clients via a "for" loop.

Quote:
So, I assume, the player presses a key to move which calls..

commandToServer( 'PlayerLeft', %player );

Then, the server sends a message to all clients telling them the %player object moved?

This is exactly correct. Depending on who pushed the button, %player will either be player1 or player2.

Quote:
What is the %player var? Is that the simobject controlled by a client? If so, you cannot assume that its ID will be the same on the client and server. Since ghosting has been removed in TGB you also cannot use getGhostId and resolveGhostId. So that is another area you will need to figure out a solution for.

This is also correct and I don't know what to do about it....
#4
05/12/2008 (12:14 pm)
The first paramater is ALWAYS the client that issued the command. Thats the way it works and it is passed in from C++. In the way you have it written your %player paramater is really getting set equal to a client ID, that is probably the source of your problem.
#5
05/12/2008 (12:14 pm)
When I run:

commandToServer('PlayerLeft',"player1");

It DOES run:

clientCmdPlayerLeft(%player);


The problem is, I told the server "player1" and the server is telling the client "1597". So how/why did it convert my string variable of "player1" into a localConnection "1597"???

Basically, I am trying to send variables back and forth between client and server but they are getting converted into something else along the way...
#6
05/12/2008 (12:15 pm)
OK!! I see now! That last post you made explains the "conversion" of the variable. TY!
#7
05/12/2008 (12:40 pm)
Sheesh, that was easy! Ok, I have player controls working on the network! WOOT!!
#8
05/12/2008 (1:42 pm)
New Question:
//
//
//
//
//
////////
// How do I tell Client 1 he is $player1 and Client 2 he is $player2?
////////

Ok so, I have an intro video to the game. I have the escape key set to skip the video and doing so starts the game. Obviously, to keep things in sync for networking, I need the game to wait until both players are ready. So, I need some way of telling the server:

Player 1: "Ok server, I am ready."
Player 2: Cool video!..... ..... ..... .... "Ok server, I am ready"
Server: "Ok both of you are ready, so go ahead and start playing"

Whats the best way to do that?
#9
05/12/2008 (2:13 pm)
How about just assigning them a number in the order that they connect in. You would do this on the server side when a client connects. If the client also needs to know too, send them a command with their "id".

Use commandToServer to say when you are ready. Then the server starts the game but only if both clients are ready.
#10
05/12/2008 (2:15 pm)
Quote:
How about just assigning them a number in the order that they connect in. You would do this on the server side when a client connects.

That's exactly what I want to do, I just don't know how.
#11
05/12/2008 (2:25 pm)
In common/gameScripts/Server/clientConnection.cs:

function GameConnection::onConnect(%client, %name)

You might want to change it there, or override it in your game folder.