Introduction Manual Class Reference Header Reference

Classes And Class IDs

Identify Object Types

In order to be able to replicate objects, Zoidcom needs to be able to distinguish them. This is done by registering each type of the application's objects.

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!

Register Object Types

For each object type, one call to ZCom_registerClass() is needed. The method is defined in ZCom_Control. We are going to call it through the 'Server' and 'Client' class from previous chapters, as they are subclasses of ZCom_Control. It is necessary to register the same set of classes for the server and all clients. It is not necessary to register them in the same order, though.

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.

Attention:
Class IDs are not identical on client and server if their classes are not registered in the exact same order. If you need to send class IDs yourself for some reason, you either have to register all classes in the same order on all stations, or you have to send the class names (i.e. "Rock" and "Tree") as strings and then call ZCom_getClassID() on the other side. These are the only two ways to make sure, that server and client are talking about the same class.

Sample Base

What follows is a basic class design that _could_ be used to handle registration of your object types aka classes to Zoidcom, and which stores the acquired class ids for later use. The following chapters will extend this code somewhat.

// 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.


This file is part of the documentation for Zoidcom. Documentation copyright © 2004-2008 by Jörg Rüppel. Generated on Sat Aug 16 15:26:51 2008 for Zoidcom by doxygen 1.4.6-NO