2 | | The most important part of the Network API is the Synchronisable Class (see also [wiki:network/Synchronisable Synchronisable]): |
3 | | * This class provides the functionality for objects to get synchronised over the network: |
4 | | * REGISTERDATA/REGISTERSTRING: these functions register a variable (to the Network Engine) that needs synchronisation |
5 | | * setObjectMode: this function sets the direction of synchronisation (server->client / server<->client / server<-client) |
6 | | * Every class that needs synchronisation must fullfill some dependencies: |
7 | | * It must (public) inherit from Synchronisable |
8 | | * It must have a default constructor |
9 | | * All steps in object creation that need data (from the levelfile or the network) must be in the create() function |
10 | | * It must implement the following functions: |
11 | | * registerAllVariables: register all variables (currently only basic types and strings), that need synchronisation, to the network engine by calling REGISTERDATA and/or REGISTERSTRING |
12 | | * create: (as described above) |
13 | | * set appropriate synchronisation direction (default: server->client) by calling setObjectMode |
| 2 | == Purpose == |
| 3 | The most important part of the Network API is the Synchronisable Class. It provides the functionality for objects to get synchronised over the network. |
| 4 | == Contents of Synchronisable == |
| 5 | * objectID: this is a unique id identifying the object inside alls synchronisables |
| 6 | * classID: this is a unique id identifying the class of the object |
| 7 | == Synchronisation == |
| 8 | There are different modes of synchronisation: |
| 9 | * network::direction::toclient (== 0x1) sync only from server to client |
| 10 | * network::direction::toserver (== 0x2) sync only to server (not really recommended) |
| 11 | * network::direction::bidirectional (==0x3) sync in both directions |
16 | | ''' Synchronisable functions:'''[[BR]] |
17 | | ''REGISTERDATA(varname)''[[BR]] |
18 | | Use this makro to synchronise a membervariable of a class (like position, velocity, ...). This makro sets the sync-direction of the variable to server->client |
| 15 | In order to synchronise a variable in a class, these steps must be done: |
| 16 | * The class must (public) inherit from Synchronisable |
| 17 | * Every variable that needs synchronisation must be registered to the network engine (see below) |
| 18 | * Make sure the syncdirection of the object is set correctly (setObjectMode, see below) |
| 19 | |
| 20 | == Register Variables == |
| 21 | There are two methods of synchronisation: |
| 22 | * Data synchronisation: use this for every type of linear allocated object (e.g. no pointers) |
| 23 | * String synchronisation: use this for strings only ;) |
| 24 | |
| 25 | If you need to synchronise different types of objects, you can synchronise it's membervariables manually. But make sure, the object (to be synchronised) exists when creating an instance of your class. |
| 26 | |
| 27 | The registrations must be done in the function registerAllVariables(). Also call this function in your constructor. |
| 28 | |
| 29 | Example: Synchronisation of a basic type (membervariable) |
| 30 | {{{ |
| 31 | int SomeClass::i; |
| 32 | REGISTERDATA(i); //this sets the syncdirection to toclient |
| 33 | }}} |
| 34 | Example: Synchronisation of a Ogre::Vector3 |
| 35 | {{{ |
| 36 | Ogre::Vector3 SomeClass::v; |
| 37 | REGISTERDATA( v.x ); |
| 38 | REGISTERDATA( v.y ); |
| 39 | REGISTERDATA( v.z ); |
| 40 | }}} |
| 41 | |
| 42 | === Register linearly allocated object / basic type === |
| 43 | To register an object, that is linearly allocated (e.g. from 0x20 to 0x50, no pointers outside this range) you can use REGISTERDATA(varname) or REGISTERDATA_WITHDIR(varname, direction) (if you want a non-default sync. direction). |
37 | | ''REGISTERSTRING_WITHMODE(stringname, mode)''[[BR]] |
38 | | same as REGISTERDATA_WITHMODE but for strings |
| 62 | === Synchronisation Direction === |
| 63 | As mentioned above, the default direction of the Synchronisation is toclient. If you want to change this, you need to register the variable with either REGISTERDATA_WITHDIR or REGISTERSTRING_WITHDIR (normally per-class, see below). Additionally you have to set the global syncdirection (per-object, see below) to directional or toclient (again: not recommended |
| 64 | {{{ |
| 65 | int SomeClass::syncback; |
| 66 | REGISTERDATA_WITHDIR(i, network::direction::bidirectional); // put this inside registerAllVariables |
| 67 | this->setObjectMode(network::direction::bidirectional); // put this wherever you want (it is possible to do this only for specific objects specified by objectID) |
| 68 | }}} |
40 | | ''setObjectMode''[[BR]] |
41 | | this sets the sync direction of the whole object (default: server->client). |
42 | | '''Note:''' if you don't call setObjectMode with a value other than network::direction::toclient then the variables will only be synchronised to the client (if at all) |
| 70 | == The create function == |
| 71 | When synchronising an object the first time to a client (or server) the following steps occur: |
| 72 | * A new object gets created with the appropriate class |
| 73 | * by calling the constructor of the class also registerAllVariables gets called (make sure this is in your constructor) |
| 74 | * After construction of the object finished the data get updated (all variables previously registered by registerAllVariables) |
| 75 | * After that the create() function gets called. |
| 76 | |
| 77 | Because we don't have the data at construction time, all functions that need member variables (with data from the server) must be called inside create(). See example class for details. |
| 78 | |
| 79 | == Preconditions == |
| 80 | To make a class A synchronisable make sure these conditions are fullfilled: |
| 81 | * A has a default constructor A::A() |
| 82 | * A implements the function registerAllVariables which has all the calls of REGISTERXXX |
| 83 | * Make sure all you put all critical calls into the create function |
| 84 | * If you have nondefault sync directions make sure you called setObjectMode |
| 85 | |
| 86 | == Example Class == |
| 87 | |
| 88 | {{{ |
| 89 | #include "network/Synchronisable.h" |
| 90 | class ExampleClass : public network::Synchronisable, public SomeOtherClass{ |
| 91 | public: |
| 92 | ExampleClass(){ registerAllVariables(); } |
| 93 | ~ExampleClass(){} |
| 94 | |
| 95 | ....... |
| 96 | void registerAllVariables(){ |
| 97 | REGISTERDATA(i); |
| 98 | REGISTERDATA_WITHDIR(v.x, network::direction::bidirectional); |
| 99 | REGISTERDATA_WITHDIR(v.y, network::direction::bidirectional); |
| 100 | REGISTERDATA_WITHDIR(v.z, network::direction::bidirectional); |
| 101 | REGISTERSTRING(s); |
| 102 | } |
| 103 | bool create(){ |
| 104 | Synchronisable::create(); |
| 105 | SomeOtherClass::create(); |
| 106 | //do all the critical stuff here |
| 107 | //eg set meshsrc or whatever (see Model for details) |
| 108 | } |
| 109 | private: |
| 110 | int i; |
| 111 | Vector3 v; |
| 112 | String s; |
| 113 | }; |
| 114 | |
| 115 | }}} |
| 116 | |