Game Development Community

NetEvent from Client to Server

by ArchieMD User · in Torque 3D Professional · 04/21/2010 (1:20 pm) · 4 replies

I need to do some client --> server communication for some custom objects. On the server side, in the NetEvent::unpack, I need to locate the server's copy of the object (then in NetEvent::process, I call a method on the object).

Based on some code I've seen on the forums, I tried passing the client object's id as part of the NetEvent message. On the server side, then I called NetConnection::resolveGhost() with this id. This fails miserably, because mLocalGhosts on this connection are always NULL. Must not be the right way to do this.

So, what is the right way?

for illustration...

Client-side, in MyObject::talkToServer
NetConnection* conn = NetConnection::getConnectionToServer();
if(conn)
	conn->postNetEvent( new MyNetEvent( "my message", this->getId() ) );

Server-side, in MyNetEvent...
virtual void unpack( NetConnection *ps, BitStream *bstream )
{ 
	S32 tempId = 0;
	bstream->read( &tempId );
	m_myObj = dynamic_cast<MyObject*>( ps->resolveGhost( tempId ) );
}
virtual void process(NetConnection *)
{ 
	if(m_myObj)
		m_myObj->doSomethingWithClientData();
}

#1
04/22/2010 (1:28 am)
On the server, I think that you need to use resolveObjectFromGhostIndex instead of resolveGhost, which is for use on the client.
#2
04/22/2010 (1:34 am)
On the client-side you should do:
NetConnection* conn = NetConnection::getConnectionToServer();  
if(conn)  
     conn->postNetEvent( new MyNetEvent( "my message", conn->getGhostIndex(this) ) );

On the server-side, you should do:
m_myObj = dynamic_cast< MyObject * >( ps->resolveObjectFromGhostIndex( ghostIndex ) );

That's the way I did it, and it is working.

Nicolas Buquet
www.buquet-net.com/cv/
#3
04/22/2010 (7:28 am)
Heh, I had noticed that mGhostRefs did have my object in it, but didn't know how to get the index for that object. I saw at least 3 threads saying that the getGhostXXXX methods were not for use on the client... so much for that logic.

It works! Many thanks for the help guys!

Now, I saw a thread that said to use Nicolas' code for the opposite direction: server->client. It definitely does not work in that direction, so now I need to figure out passing NetEvents from server->specific client :-P
#4
04/22/2010 (7:55 am)
That was simple. Same soln but as Guy pointed out, use resolveGhost on the client