Version 3 (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
- Include the appropriate header file:
#include "network/Synchronisable.h"
- Make sure your class inherits from Synchronisable:
class XYZ : public network::Synchronisable { ... };
- 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_); }
- Make sure registerAllVariables() gets called in the constructor:
XYZ::XYZ(){ registerAllVariables(); // do not work with synchronisable variables in your constructor (except initialisation) }
- 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):
- 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) }
- 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); ... }