Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Version 2 (modified by scheusso, 16 years ago) (diff)

Howt to make an object class Synchronisable

This is a step-by-step guide to make a class XYZ synchronisable.

  • Synchronisable classes are registered to the network engine to be transfered/synched between client and server.
  • If you want your class to get transfered, you have to register every important variable to the network (e.g. position, speed, health, …). But only register variables that are absolutely neccessary (e.g. no local variables).
  • The default transfer direction of variables (and objects) is from server to the clients only. If you want to change this for a certain variable (and object), call REGISTERDATA_WITHDIR(varname, direction) or REGISTERSTRING_WITHDIR(stringname, direction) instead of REGISTERDATA or REGISTERSTRING.

See also network/Synchronisable? for more information.

Basic steps

  1. #include "network/Synchronisable.h"
  2. make sure your class inherits from Synchronisable:
    class XYZ : public network::Synchronisable { ... };
    
  3. create the function void registerAllVariables() and call REGISTERDATA or REGISTERSTRING functions in order to register these variables for synchronisation:
    void XYZ::registerAllVariables(){
    REGISTERDATA(somevariable);
    REGISTERSTRING(meshSrcName_);
    }
    
  4. make sure registerAllVariables() gets called in the constructor:
    XYZ::XYZ(){
    registerAllVariables();
    // do not work with synchronisable variables in your constructor (except initialisation)
    }
    
  5. implement the create() function and put all the code that needs some synchronisable variables to be set inside it:
    bool XYZ::create(){
    this->ogreMesh_.setMesh(meshSrcName_);
    }
    

Multidirectional synchronisation

If you want to have a variable synchronised back to the server, follow these steps (additional to/instead of the above):

  1. register the variable with REGISTERDATA_WITHDIR:
    void XYZ::registerAllVariables(){
      REGISTERDATA_WITHDIR(someVar_, network::direction::bidirectional); // synchronises in both directions
      REGISTERSTRING_WITHDIR(someString_, network::direction::toserver); // only transfers from client to server (not recommended)
    }
    
  2. Change the default object synchronisation direction in order to enable the above variables to get transfered to the server:
    XYZ::XYZ(){
      registerAllVariables();
      setObjectMode(network::direction::bidirectional);
      ...
    }