Game Development Community

Problem with adding field to GameConnection

by Joshua Jewell · in Torque Game Engine · 03/15/2006 (8:02 am) · 6 replies

I have scoured the forums and cannot quite find the answer I am looking for. Basically I added my own class and then added a field to GameConnection so that it could hold a pointer to that object. I wrote the proper code to allow this method to be used in the script but I am having a problem. When I try to access this field in a GUI element it always returns NULL. I did not write any code to communicate to the client that that field changed so I am guessing my problem lays there. My question is how would I achieve this. I see that the player class does it with write and read packet data. However GameConnection does not seem to have this method, my best guess is the similar readPacket and writePacket. Anyway if anyone can let me know if that is my problem, and/or point me in the direction of how to solve my problem I would greatly appreciate it.

#1
03/17/2006 (9:23 am)
Hopefully somone has some usefule information on this. I have so far been unsuccessful in figuring this out. I want the gui object to be able to use this extra field added to GameConnection but so far it does not seem to have a value on the client side.
#2
03/17/2006 (9:34 am)
GameConnection directly tied to a GUI element?
Might be helpful if you'ld explain what exactly you are trying to pass, that way maybe someone who has done something similar could chime in.
As it stands now your request is pretty vague.

Anyways any time you want to make a new variable accesible from script you do so by using InitPersistFields in the .cc file of whatever you are using.
Sorry I can't be more helpful, but I'ld need more info from you to be any more specific.
An endgoal or the type of information you are trying to access etc.
#3
03/17/2006 (10:22 am)
I added an extra field to GameConnection. This field is used to store the id of a SimObject. In ShapeNameHud I would like to retrieve this id from GameConnection and use it in displaying text to the screen. It is being used as a wayPoint type thing that appears on the clients screen. Anyway the field I added never has anything other then NULL which I initialized it with so I am guessing that the server is not sending that information to the client. My question is how is this done in GameConnection.
#4
03/17/2006 (11:17 am)
Greetings!

Two different methods come to mind on how you could approach this.

The first method is if this is information that doesn't change on a regular basis. ie: doesn't need to be updated every tick. Then you can make use of a NetEvent. See SimpleMessageEvent in netTest.cc for an example. In your case you'd probably want to set it up with the IMPLEMENT_CO_CLIENTEVENT_V1 marco to indicate that the event may only go to the client. In the NetEvent's process method you can do what ever you'd like, such as copy the passed information onto the client's GameConnection, or put this information directly onto the GUI control.

If you'd prefer to do this in script, there is the commandToClient() mechanism (uses NetEvent's under the hood). It would be easy to set this up to pass along your SimObjectID, and then could pass it directly to the GUI control (or your connection too). Search around the forums or the docs on how to use commandToClient().

The second method would be to change GameConnection itself to pass along your new variable. In this case you'll need to change GameConnection::writePacket() and GameConnection::readPacket(). The advantage of going this route is that the information may be easily passed every tick. Check out how the mMoveList information is sent to the client as an example. If you're not familiar with the Torque bitstream, I'd recommend you do some searches and documentation reading.

---

Now, having written the above, I've just realized that what you're after isn't as simple as that. The above techniques will work for passing along generic information, but won't directly work for SimObjectID's. The reason is that SimObjectID's are almost never the same between server and client -- datablocks being the exception to this.

What you'll need to do is look up the SimObject's ghost ID on the client's connection, pass along this ghost ID to the client, and on the client the ghost ID is translated back into a valid SimObject. This isn't hard, but is tricky if you haven't done this before or aren't familiar with Torque's networking -- and I don't know if you are.

On the server side, you'll need to do something like:

gIndex = getGhostIndex(mControlObject);
bstream->writeInt(gIndex, NetConnection::GhostIdBitSize);

in the client connection's GameConnection::writePacket() on the server side, and the following:

S32 gIndex = bstream->readInt(NetConnection::GhostIdBitSize);
SimObject* obj = static_cast<SimObject*>(resolveGhost(gIndex));

on the client side's connection to the server in GameConnection::readPacket(). The above are just example code snipets, of course. You'll need to determine exactly how to implement it for your needs and where to place it in the network stream.

It is also possible to have a NetEvent do all of the above ghost ID translation rather than tying it directly into GameConnection if you want. Again this is great for information that doesn't need to change every tick.

---

In the end, it may just be easier to pass along the way point's 3D position to the client using one of the other methods I've mentioned and not worry about passing along the actual SimObjectID/ghostID. Unless there is a game specific reason to do so.

I hope that helps.

- LightWave Dave
#5
03/17/2006 (12:15 pm)
Dave thanks a bunch for your post it was very informative. I am not familiar with Torques networking at all which is probably why I was not sure how to go about this. Hopefully with the information you gave me I can get this working, thanks again.
#6
03/17/2006 (1:00 pm)
Ok I think I may still be a bit confused so let me try to re-explain my problem with my new found knowledge. Basically I created my own objects derived from SimObject. So I am guessing that when there are first created the onAdd method is called and they exist on both the client and server side. Now If I were to change a value of the Object on the server side(through script) would it then be different from the one on the client side? I am guessing yes but I am not quite sure. I guess I should explain exactly what I have done to make it easier to visualize. I created 2 of my own classes Squad and WayPoint both derived from SimObject. The Squad class is the important one that has an array of Players and an array of WayPoints that belong to it. I am going to use this information in guiShapeNameHud.cc to display them to the HUD. The objects I created will be constantly changing but probably not on a tick basis. Now passing the object ID for the Squad object aside, would I also have to create and send an update stream within my new objects to keep them updated with the correct info on both the client and server? I apologize if my problem is simple, but I have been having a hard time solving this problem with documentation and threads. Also if I update the GameConnection object through script, which seems like the easiest way will it convert the id's so that the client gets the ghost id correct?