Game Development Community

Getting lost with IDs, handles and pointers

by Gordon Marsh · in Torque Game Engine · 08/23/2006 (1:09 pm) · 3 replies

Guys,

I have implemented a class to be run on the server that entails taking in player pointers and manipulating those players. I have now found that I need to access the class via script, so I have implemented the following script function:


ConsoleMethod( TeamCircleManager, playerJoinCircle, void, 4, 4, "Creates a new circle or adds player to existing one")
{
U32 ghostID1 = 0;
U32 ghostID2 = 0;

dSscanf(argv[2], "%i", ghostID1);
dSscanf(argv[3], "%i", ghostID2);

NetConnection* conn = GameConnection::getConnectionToServer();

Player* play1 = dynamic_cast(conn->resolveObjectFromGhostIndex(ghostID1));
Player* play2 = dynamic_cast(conn->resolveObjectFromGhostIndex(ghostID2));

return object->joinCircle(play1, play2);
}


To start with, if the two inputs (arg[2] and arg[3]) are ghost indexes, am I converting them properly into player pointers?

Secondly, When I call this via script, what exactly should be passed from the client? Would the following pass the right handles in?


// Client side call
function clientfunction()
{
commandToServer('serverFunction' player1->getID(), player2->getID();
}


// Server side call
function clientfunction(%play1, %play2)
{
TeamCircleManager.playerJoinCircle(%play1, %play2);
}


I hope this code is correct, but my mind is beginning to spin with all these different pointers! Any help appreciated!

Thanks, Gords

#1
08/24/2006 (1:45 am)
You cannot use C++ object syntax in Torque Script. Thus, player1 is not correct to begin with, and -> is also incorrect.

$player1 would be: a global variable, accessible from anywhere.
%player1 would be: a local variable, accesible from inside the function it was defined in.

%player1.getID(); would be the correct way to call the function in question, but the problem is that getID() is not exposed to script, and %player1 is not passed from somewhere.

I cant make out the rest of your example above, it is very confusing with the naming convention and you really should take a look at the documentation where all this is explained.
#2
08/24/2006 (9:50 am)
Thanks Stefan (and Sam Redfern although your post seems to have disappeared!!).

Sam's suggestions have cleaned up my consolemethod somewhat. I think I have overcomplicated my own question here, which really should have been:- What is the type of the player pointers in script?

If I assume they are of type SimObject, then the following should work.


ConsoleMethod( TeamCircleManager, playerJoinCircle, void, 4, 4, "Creates a new circle or adds player to existing one")
{
U32 ghostID1 = dAtoi(argv[2]);
U32 ghostID2 = dAtoi(argv[3]);

Player *play1 = dynamic_cast(Sim::findObject(ghostID1));
Player *play2 = dynamic_cast(Sim::findObject(ghostID2));

return object->joinCircle(play1, play2);
}


And a proper call from script should be:


function serverCmdPlayerJoinCircle(%client, %client2)
{
$circleManager.playerJoinCircle(%client, %client2);
}


My server is knackered at the moment so I can't test it - will post here again if it doesn't work - hope it helps someone out who is looking to make use of script objects in the engine.

Thanks,
Gords
#3
08/29/2006 (7:22 am)
Hmmm, unfortunately my code above doesn't seem to work. so I am still totally lost as to what type of pointers are being passed from script into my consolemethod.

Basically I'm just trying to get the clients player to click on another player, pass the details of both players through to the server script, which in turn passes them to the server engine.

The script command called on the server now looks like:


function serverCmdPlayerJoinCircle(%client, %client2ObjID)
{
%player2 = %client.resolveObjectFromGhostIndex(%client2ObjID);
%player1 = %client.player;

$circleManager.playerJoinCircle(%player1, %player2);
}


client2ObjID is a ghostindex on the client. player1 and player2 are assigned a number, so I assume they are correct at this point.

The consolemethod looks like:


ConsoleMethod( TeamCircleManager, playerJoinCircle, void, 4, 4, "Creates a new circle or adds player to existing one")
{
U32 ghostID1 = dAtoi(argv[2]);
U32 ghostID2 = dAtoi(argv[3]);

Player *play1 = dynamic_cast(Sim::findObject( ghostID1 ));
Player *play2 = dynamic_cast(Sim::findObject( ghostID2 ));

return object->joinCircle(play1, play2);
}


However, play1 and play2 above are always null! I'm obviously passing in the wrong pointer or dealing with it in the wrong way.