Game Development Community

Scripting Server Objects From Datablocks Confusion

by Michael Hense · in Torque Game Engine · 06/17/2005 (10:14 pm) · 6 replies

I can declare a new ShapeBaseData object from a datablock in script on the client well enough, and call setTransform to position it...

... but when i try to do the same thing on the server, it responds with a 'can't find fuinction error.


i'm instantiating an object from a datablock, which has a shapefile defined, and it loads and i can see the object when i do this from client script... i'm trying to do this on the server so the object is seen by any client that connects...

do i have the concept straight???


thx

--Mike

#1
06/17/2005 (11:01 pm)
Are you creating the object on the client or when you test it are you creating it on the server?
#2
06/17/2005 (11:14 pm)
From the client using commandtoserver('CreateShip'); in the console...

then i verify that it exists by echo($testShip); and it returns a value...

a dump(); displays a few methods, but not anything near as many as when i created the object in client side script on startup...

--Mike
#3
06/18/2005 (11:46 am)
Try creating it as a staticShape with a staticShapeData datablock and see what happens.
#4
06/18/2005 (1:31 pm)
Okay... i've finally got it... i really was naive to think that i would be able to grasp anything unless i was up for over 40 hours, and had a zillion cups of coffee...

but it finally came to me... with a lil help from Dan (thx) and the forum threads, and the part of Ken's book that i should've read more slowly...

i finally got an object instantiated on the server and 'ghosting' down to the clients... i'm using the chap6 code from Ken's book as a basis... i just had to stop thinking like a brick and let go a lil... the engine gives you all you need if you let it... i felt as if i just turned a corner with this thing...

for the guys still struggling with this, one of the keys is to get a good editor... one that lets you see the project as a whole, and look at the events and the functions that respond to each event...

now to see if i can coax each of the clients to talk to the server object... that shouldn't be too hard (yeah, right :) )...

--Mike
#5
06/22/2005 (4:22 am)
Could you post what you had to do to get this to work? Perhaps some code?
#6
06/24/2005 (6:44 am)
Sure Kip...

first of all, though, it seems as if you've gotta put the server side code in the right place... where the server stuff is compiled... so i put the Testship script in /control/server (/control may be any name, the name of your project for example... i'm using the directory structure from the Ken Finney book since i'm using his examples as a basis for learning the engine)...

here is the script code... it's a simple definition just to test to see if i have this thing down...

// testship.cs

datablock ItemData(TestShip){
  shapeFile="control/data/shapes/TestShip/Hull2.dts";
  mass=1;
  friction=1;
};
  

function InsertTestShip(){
    $TestShip=new Item(){
    rotation="0 0 1 0";
    position = "-45.8946 -21.6984 201.921";
    scale="1 1 1";
    dataBlock= "TestShip";
    };
  MissionCleanup.add($TestShip);
   $TestShip.setTransform( "-45.8946 -21.6984 201.921");
}


function ServerCmdMoveTestShip(%client){
  %xfrm=$TestShip.getTransform();
  %lx=getword(%xfrm,0);
  %ly=getword(%xfrm,1);
  %lz=getword(%xfrm,2);
  %lx-=0.19;
  $TestShip.setTransform(%lx SPC %ly SPC %lz );
}

as you can see... just a simple datablock... and two functions... i have this code loaded in /control/server/server.cs in the onServerCreated event handler... right below all the other Exec statements by this line of code...

Exec("./testship.cs");

InsertTestShip() i also call from /control/server/server.cs, in the onMissionLoaded event handler... this function is defined in TestShip.cs which is loaded just a few lines before it is being called... and it creates the server global object $TestShip derived from the datablock...

the other function, serverCmdMoveTestShip() is a function to be called from the client... any client... it will make $TestShip move... this was the concept i was having trouble with... i thought i had to pass an id to the server, but as you can see, it isn't necessary... since $TestShip is a global on the server side, it appears to be accessible from any client that calls this function on their end...

okay... the server side stuff is done... lets look at how i communicate the command to move $TestShip to the server (where $Testship exists)...

to make things simple, i decided for now to issue the command from a keypress... so i opened up /control/client/misc/presetkeys.cs (or wherever you have your keybinds for your client) and added this line

PlayerKeymap.Bind(Keyboard, n, MoveTestShip );

and above it in the same file, i added a handler for the keypress...

function MoveTestShip(){
 commandToServer('MoveTestShip');
}

all it does is call the function on the server... the client variable is passed automatically...


now this is just a simple bit of code... something to get me to understand what is going on here... i'm not sure if doing this this way is the right way to do it... also, there is a consideration about security... making sure that the commandToServer is coming from a valid client...

... but those things will come later (basic security is covered in a few threads on the forum... i've read over em briefly, that's how i know that it was in fact a consideration that should be factored in :) )... right now i've gotta get the basic concepts down... i've had this thing long enough, and it's about time i actually did something with it...

and in this attempt to understand i hope that anyoe reading this and seeing anything that might be done better... anything... will chime in and post their suggestions and/or observations...

Kip... i hope this sorta explains what i did... and is of some help... if you have any questions, feel free to post em...


good luck

--Mike