Game Development Community

Client Server 101

by Wayne Russell · in Torque Game Engine · 02/03/2007 (4:02 pm) · 2 replies

So, I create an object in GameConnection::onClientEnterGame(), like so:

%this.myObject = new MyObject() {
dataBlock = someDatablock;
};

I then want to call some function on this object from my client code when a particular key is pressed. But how? I have seen code that tells me to create a global variable on the server side and access it that way, like so:

// On server
$myObject = %this.myObject();

// On client
function myKeypress (%val) {
$myObject.myFunction(%val);
}

This works fine, but only if I have client and server on the same machine - it's not a proper solution.

I've also seen it suggested (vaguely and in passing, so maybe my interpretation is wrong) that I could replace the global var with something like this:

function myKeypress (%val) {
%this.myObject.myFunction(%val);
}

For this to work, it would seem to imply that the client code is implicitly running within the context of a GameConnection object, and also that there is some built-in scheme for remote method invocation. But in fact, in my experiments it just complains about a missing object...

Could somebody please give me some guidance as to what needs to be done here? I can see how I could cobble together a way do this using commandToServer, but is there a cleaner, more object-oriented way to access server objects/methods from client code?

Also, if there's a resource that discusses the detail of client/server issues in Torque script coding, I'd be delighted if someone could point me at it - I've hunted around in vain.

Thanks,

Wayne

#1
02/05/2007 (10:21 am)
There is no way to directly access server side objects and their methods from a remote client. You'll have to create your own approach, and it will be something similar to commandToServer, which uses a special NetEvent.

Most client side objects are represented as a ghost of a server side object. The basic approach is to get the ghost id of the client side ghost, pass that in a NetEvent to the server side, where it can look up the server side object from the ghost id, and then access it's members and methods.

Look for classes derived form NetEvent for example usage.
#2
02/06/2007 (3:29 pm)
Thanks for that Kirk