Game Development Community

CommandToClient not working at all

by Tasty Green Pees :-, · in General Discussion · 03/31/2006 (9:27 am) · 19 replies

I don't know why but it seems that the commandToClient function is not working anywhere in my game and however I try to apply it. I've spent the whole day scouring these forums and trying various different things but nothing seems to work.

I'm wondering, is there something that is missing somewhere that is needed to make the commandToClient function work?

Just incase u were wondering. the commmandToServer is working fine.

#1
03/31/2006 (9:28 am)
Are you sure you're passing it a valid client?
#2
03/31/2006 (9:48 am)
Right. maybe you can tell me.

for example:

function PlayerShape::onCollision(%this, %obj, %col, %vec, %speed)
{
	%obj_state = %obj.getState();
	%col_className = %col.getClassName();
	%col_dblock_className = %col.getDataBlock().className;
	%colName = %col.getDataBlock().getName();
	
	if (%obj_state $= "Dead")
		return;

	if(%col_className $= "Item"||%col_className$="Weapon"||%col_className$="Ammo" )
	//Deal with all items
	{
		//%obj.pickup(%col);
		// otherwise, pick the item up
	}

	if ( %col_className $= "WheeledVehicle" || %col_className $= "FlyingVehicle")
	{
		%node = 0; // Find next available seat
		%col.mountObject(%obj, %node);
		%obj.mVehicle = %col;
//
//
// HERE IT IS!!!
                commandToClient(%obj.client, UpdateGameGUI, "vehicleGui");
//
//
//
	}

	else
	{
		%pushForce = %obj.getDataBlock().pushForce;	// Try to push the object away

		if (!%pushForce)
		%pushForce = 20;
		%eye = %obj.getEyeVector();			// Start with the shape's eye vector...
		%vec = vectorScale(%eye, %pushForce);
		%vec = vectorAdd(%vec, %obj.getVelocity());	// Add the shape's velocity
		%pos = %col.getPosition();			// then push
		%vec = getWords(%vec, 0, 1) @ " 0.0";
		%col.applyImpulse(%pos, %vec);
	}
}

as i understand this SHOULD work.
#3
03/31/2006 (9:51 am)
In some client-side script such as client\client.cs:
function clientCmdWorld(%arg)
{
   echo("clientCmdWorld got" SPC %arg)
}

in some server-side script such as server\scripts\commands.cs:
function serverCmdHello(%client, %arg)
{
   echo("serverCmdHello got" SPC %arg SPC "from client" SPC %client);
   commandToClient(%client, 'World', %arg);
}

then in the console enter the command:
commandToServer('hello', "blah blah blah");

you should see this in your console log:
[3/31/06 09:53:27][Inf] ==>commandToServer('hello', "blah blah blah");
[3/31/06 09:53:27][Inf] serverCmdHello got blah blah blah from client 2485
[3/31/06 09:53:27][Inf] clientCommandWorld got blah blah blah
#4
03/31/2006 (9:53 am)
Just read your post.

1. make sure that %obj.client is valid:
echo(%obj.client.getClassName());
- it better be GameConnection.

2. put UpdateGameGUI in single-quotes. 'UpdateGameGUI'.
#5
03/31/2006 (10:44 am)
Orion, i did exactly what you said. the result was as follows...

==>commandToServer('hello', "blah blah blah");
Mapping string: hello to index: 3
serverCmdHello got blah blah blah from client 1329
Mapping string: World to index: 12
clientCmdWorld: Unknown command.
#6
03/31/2006 (10:49 am)
Ah: i forgot a semi-colon at the end of the clientCommand echo().
try with that.
#7
03/31/2006 (10:58 am)
It worked:

==>commandToServer('hello', "blah blah blah");
Mapping string: hello to index: 3
serverCmdHello got blah blah blah from client 1329
Mapping string: World to index: 12
clientCmdWorld got blah blah blah

which means that my commandToServer function DOES work.

lemme try implementing it again with your code to see what happens.
#8
03/31/2006 (11:28 am)
Why is it that when i type in "commandToClient(%client, 'World', "something")"

all i get is
==>commandToClient(%client, 'World', "sdfasdf");

rather than
Mapping string: World to index: 12
clientCmdWorld got something


i think this is my problem
#9
03/31/2006 (11:35 am)
By "type in" do you mean type in at the console ?
eg the way you type in ==>commandToServer('hello', "blah blah blah"); ?

if that's the case, the reason it doesn't work is twofold:

1. local variables cannot be seen by console commands, only global ones,
and %whatever is local because of the %.
so you'll something more like $client.

2. $client needs to be defined. %client is defined inside serverCmdHello() only because it's been passed in as an argument.


try this -
function serverSideFunctionToIterateThruAllClientsAndSendThemACommand()
{
   %count = ClientGroup.getCount();
   for (%n = 0; %n < %count; %n++) {
      %client = ClientGroup.getObject(%n);
      echo("client" SPC %n SPC "is" SPC %client);
      commandToClient(%client, 'World', "yo.");
      }
}
#10
03/31/2006 (1:19 pm)
Yeah u were right. totally didn't think throught the "%" issue.

anyway. i tried the serverSideFunctionToIterateThruAllClientsAndSendThemACommand() function without success.

I basically called it in one of my server scripts like this...

function pingEm()
{
   %count = ClientGroup.getCount();
   for (%n = 0; %n < %count; %n++) {
      %client = ClientGroup.getObject(%n);
      echo("client" SPC %n SPC "is" SPC %client);
      commandToClient(%client, 'World', "yo.");
      }
}

pingEm();

it seems that i can only send commands to client through a command to server.

could that have anything to do with the fact that in single player the server and client are one?
#11
04/01/2006 (1:55 am)
Quote:
it seems that i can only send commands to client through a command to server.

Huh? You send commands to the client trough commandToClient, and commands to the server trough commandToServer, nothing else.

Looking trough your example above, I see absolutely nothing wrong with it - and there is no difference when you're in local mode (ie, singleplayer) because you still have a client and a server, they are just on the same machine.

What happens when you run the function above? Do you get the echo output in the console?
#12
04/01/2006 (7:31 am)
Quote:
it seems that i can only send commands to client through a command to server.

what i mean is that it seems that any commandToClient only seems to work when i call it in an a commandToServer
#13
04/01/2006 (10:34 am)
I'm sorry but you're not making sense to me. You can't call commandToClient "in a" commandToServer. I'm sure you mean something else, but I can't figure it out.

Hopefully someone else can. Good luck.
#14
04/01/2006 (11:07 am)
Stefan- I can see what he means- the code he has shown looks like it should work alright, and there's no good reason that a commandToClient would only work when inside a serverCmd function, but that is what he seems to be experiencing. Bizarre.
#15
04/01/2006 (11:11 am)
@Stefan - certainly you can. see my first comment in this thread.

@Tasty - not sure why this stuff ain't working for you.
I tried out your pingEm() as written and it worked fine.
Does it iterate thru the list of clients (which probably only has one member in your testing) and print
"client 0 is " ?

if not, then the problem is probably that pingEm isn't being called.
to test for sure, add another echo into pingem before the for() loop saying something like
"iterating over" SPC %count SPC "clients..".

if pingEm *does* show "client 0 is ",
then there's some weirder problem.

you say that our earlier test with ClientCmdWorld(%arg) worked ?
does it still work in the same environment at pingEm() ?
if so, then you know the clientCommand is set up correctly.

.. so if you get to the point where commandToServer('hello', "blah blah") works but pingEm doesn't work but pingEm does print "client 0 is " then stuff is weird.
in that case, compate the in pingEm to the in "serverCmdHello got blah blah blah from client ". - do they match? if so, i'm out of ideas. if not, i'm also out of ideas, but at least you'll have some more clues. ;)
#16
04/01/2006 (12:03 pm)
Quote:
@Stefan - certainly you can. see my first comment in this thread.

No, you can't. commandToServer is a console function, which was what I thought he was talking about. But he's talking about a normal script function with the serverCmd prefix in it's name. I got the impression he was talking about the former, but Sam explained it.

I still think the above given code examples should work out just perfect. All of mine does, and they look pretty much like that.
#17
04/01/2006 (1:04 pm)
Quote:
there's no good reason that a commandToClient would only work when inside a serverCmd function, but that is what he seems to be experiencing. Bizarre.

Bingo :(

I'm gonna try and do what Orion suggested and post back.
#18
04/01/2006 (11:46 pm)
If my scripts are misbehaving, the first thing I normally do is open up the console window and scroll back looking for any red... often it's simply a case of a typo that has caused a modified script not to compile at all.
#19
04/02/2006 (9:48 am)
Excellent point Sam.
at the place i work we even force the engine to halt w/ a message box whenever there's a script syntax error.