Calling client side
by Jendrik Posche · in Technical Issues · 05/20/2008 (6:52 am) · 3 replies
Hi all,
I have an object added in the world editor and named it Ball.
I added a custom c++ class in the torque engine called getFoo() and a ConsoleMethod( MyBall, foo, ...) which calls object->getFoo();
Now how would i do this? I tried clientCmdFoo() and commandToClient() but somehow i always get an error that the function doesn't exist.
regards,
I have an object added in the world editor and named it Ball.
I added a custom c++ class in the torque engine called getFoo() and a ConsoleMethod( MyBall, foo, ...) which calls object->getFoo();
Now how would i do this? I tried clientCmdFoo() and commandToClient() but somehow i always get an error that the function doesn't exist.
regards,
#2
In short: I created a function foo() inside the class Ball and put it somewhere int he engine. I also created a Consolemethod to expose this Ball::foo() function to the console and thus the script.
// Script
function serverCmdFooBar(%client){
// Grab the client ID from a server object named "Ball"
%ballID = %client.getGhostID( Ball ) ;
// Now send it to the client.
commandToClient(%client, 'foo', %ballID );
}
function clientCmdFoo(%ballID){
// Do you something on the client object of "Ball"
echo ( %ballID.foo() );
}
// C++
ConsoleMethod(MyBall, foo, S32, 2,2, "your function documentation"){
return object->foo();
}
int Ball::foo(){
if( isClientObject() ){
// yes we are on the client side
return 1234;
}
assert( false && "Not a Client Object");
}
This should do the trick, well it did for me at least.
Thanks for the help :-)
05/20/2008 (7:32 am)
Thanks. Here is what I've done...In short: I created a function foo() inside the class Ball and put it somewhere int he engine. I also created a Consolemethod to expose this Ball::foo() function to the console and thus the script.
// Script
function serverCmdFooBar(%client){
// Grab the client ID from a server object named "Ball"
%ballID = %client.getGhostID( Ball ) ;
// Now send it to the client.
commandToClient(%client, 'foo', %ballID );
}
function clientCmdFoo(%ballID){
// Do you something on the client object of "Ball"
echo ( %ballID.foo() );
}
// C++
ConsoleMethod(MyBall, foo, S32, 2,2, "your function documentation"){
return object->foo();
}
int Ball::foo(){
if( isClientObject() ){
// yes we are on the client side
return 1234;
}
assert( false && "Not a Client Object");
}
This should do the trick, well it did for me at least.
Thanks for the help :-)
#3
How would you then call Ball::foo() from the console?
Does serverCmdFooBar(XXXX) do it? And what would you put for XXXX?
I'm trying to call a method on a client object from the console and this seemed like the way to do it. If I just typed object_name.foo(...) it only executes on the server object... is your way the right way?
03/01/2010 (3:15 pm)
Hey Jendrik,How would you then call Ball::foo() from the console?
Does serverCmdFooBar(XXXX) do it? And what would you put for XXXX?
I'm trying to call a method on a client object from the console and this seemed like the way to do it. If I just typed object_name.foo(...) it only executes on the server object... is your way the right way?
Torque 3D Owner DALO
If you have a custom object created from this special class that you made and it's in the world editor named Ball, then it's going to be kept track of on the server and so you'd simply call Ball.engineFoo(); ,assuming that the engine function is like this:
ConsoleMethod(MyBall, engineFoo, void, 2, 2, "call to foo function in engine,blah,blah,blah"){
object->getFoo(); // this will now call a class member function called getFoo()
return;
}
The void means what your returning from the console function, 2 means the minimum number of parameters being passed into this console method and the second 2 means the maximum number.
commandToClient() is something you send to the client (in script), clientCmdFoo() is the function being called from the server. So if I wanted to call the function 'Foo' then it'd be:
commandToClient(%clientIdNumber, 'foo', %whatever1, %whatever2);
This will then go to that client's machine, based on which client (decided from %clientIdNumber), and in the client side scripts it will look for a function like this:
function clientCmdfoo(%whatever1, %whatever2){
...
...
}
If the client wants to send something back to the server then you'd type:
commandToServer('allDoneWithFoo', %returnThis1, %returnThis2);
Now that were back on the server side, we'll be looking for this function:
function serverCmdallDoneWithFoo(%client, %returnThis1, %returnThis2){
// because a specific client called this function the first parameter will always be the client
// that called the server function, the remaining parameters will be whatever info that needs to be sent
....
....
}
%returnThis1 & 2 and %whatever1 & 2 are optional parameters that you might need to send.
Hope this helps clear some things up, if not, it maybe useful for someone else.