![]() |
||||||||
|
|
||||||||
If the game world consists of rocks and trees, we need to register two types: "Rock" and "Tree". This needs to be done on client and server. When a new tree object is created on the server, it will send the object type to the client, and only through the received object type it is possible for the client to actually know which object it is supposed to create. Object names will get mapped to ids internally, and even better, the ids have a dynamic size. If the world consists of four different object types, the type id sent with the object announcements uses only two bits of bandwidth!
ZCom_ClassID rockid = server->ZCom_registerClass("Rock"); ZCom_ClassID treeid = server->ZCom_registerClass("Tree");
It is important to keep the returned ids, as they are needed when new objects are created on server and client. They can also be retrieved at a later point by calling:
ZCom_ClassID rockid = server->ZCom_getClassID("Rock");
This is not encouraged, though, as it is slower to query the ids everytime when they are needed than to store them somewhere.
// this code is not meant to be functional as it is /******************* Base Object ********************/ // base game object class GameObject { }; /******************* Tree Object ********************/ // tree game object class Tree : public GameObject { // the class id for zoidcom static ZCom_ClassID m_classid; public: // need this later Tree(ZCom_Control *_control); // class registration static void registerClass(ZCom_Control *_control) { m_classid = _control->ZCom_registerClass("Tree"); } static ZCom_ClassID getClassID() { return m_classid; } }; // declare static member ZCom_ClassID Tree::m_classid = ZCom_Invalid_ID; /******************* Rock Object ********************/ // rock game object class Rock : public GameObject { // the class id for zoidcom static ZCom_ClassID m_classid; public: // need this later Rock(ZCom_Control *_control); // class registration static void registerClass(ZCom_Control *_control) { m_classid = _control->ZCom_registerClass("Rock"); } static ZCom_ClassID getClassID() { return m_classid; } }; // declare static member ZCom_ClassID Rock::m_classid = ZCom_Invalid_ID; /******************* Main ********************/ int main() { // <- insert code from server example here Server *server = new Server(); // <- insert code from server example here // register the object types with zoidcom Tree::registerClass(server); Rock::registerClass(server); // more to come }
As you can see, the class registration and class id getter is the same for all classes, so code is duplicated alot. For sake of clarity, let us refrain from using C++ templates or macros to circumvent that code duplication.
Please note that the above code will not work when client and server run in the same process, i.e. when the same class gets registered to two different ZCom_Control objects. There is only one m_classid per C++ class, and if you register the class with the server and the client, you'd need a second one to store the id for that, too.
class Tree { protected: static ZCom_ClassID m_netclass_server_id; static ZCom_ClassID m_netclass_client_id; public: static ZCom_ClassID getNetClassID(bool _server) { if (_server) return m_netclass_server_id; else return m_netclass_client_id; } };
The code in the following examples will be based on the simpler version using only one classid.
1.4.6-NO