# Replica Class Reference
[ReplicaManager]

The interface to derive your game's networked classes from. More...

`#include <Replica.h>`

Inheritance diagram for Replica:

List of all members.

## Public Member Functions
virtual ReplicaReturnResult | SendConstruction (RakNetTime currentTime, SystemAddress systemAddress, unsigned int &flags, RakNet::BitStream *outBitStream, bool *includeTimestamp)=0
virtual ReplicaReturnResult | SendDestruction (RakNet::BitStream *outBitStream, SystemAddress systemAddress, bool *includeTimestamp)=0
virtual ReplicaReturnResult | ReceiveDestruction (RakNet::BitStream *inBitStream, SystemAddress systemAddress, RakNetTime timestamp)=0
virtual ReplicaReturnResult | SendScopeChange (bool inScope, RakNet::BitStream *outBitStream, RakNetTime currentTime, SystemAddress systemAddress, bool *includeTimestamp)=0
virtual ReplicaReturnResult | ReceiveScopeChange (RakNet::BitStream *inBitStream, SystemAddress systemAddress, RakNetTime timestamp)=0
virtual ReplicaReturnResult | Serialize (bool *sendTimestamp, RakNet::BitStream *outBitStream, RakNetTime lastSendTime, PacketPriority *priority, PacketReliability *reliability, RakNetTime currentTime, SystemAddress systemAddress, unsigned int &flags)=0
virtual ReplicaReturnResult | Deserialize (RakNet::BitStream *inBitStream, RakNetTime timestamp, RakNetTime lastDeserializeTime, SystemAddress systemAddress)=0
virtual int | GetSortPriority (void) const =0

## Detailed Description

The interface to derive your game's networked classes from.

This is an interface of a replicated object for use in the framework of ReplicaManager You should derive from this class, implementing the functions to provide the behavior you want. If your architecture doesn't allow you to derive from this class, you can store an instance of a derived instance of this class in your base game object. In that case, use GetParent() and SetParent() and propagate the function calls up to your real classes. For an example where I do this, see Monster.h in the ReplicaManagerCS sample.

Note:
  All send functions are called one for every target recipient, so you can customize the data sent per-user.

## Member Function Documentation

virtual ReplicaReturnResult Replica::Deserialize | ( | RakNet::BitStream * | inBitStream,
|  | RakNetTime | timestamp,
|  | RakNetTime | lastDeserializeTime,
|  | SystemAddress | systemAddress |
| ) |  |  | `[pure virtual]`

Called when another participant called Serialize with our system as the target

Parameters:

  `[in]` | inBitStream | What was written to Serialize::outBitStream
  `[in]` | timestamp | if Serialize::SendTimestamp was set to true, the time the packet was sent.
  `[in]` | lastDeserializeTime | Last time you returned true from this function for this object, or 0 if never, regardless of systemAddress.
  `[in]` | systemAddress | The participant that sent this message to us.

virtual int Replica::GetSortPriority | ( | void |  | ) | const`[pure virtual]`

Used to sort the order that commands (construct, serialize) take place in. Lower sort priority commands happen before higher sort priority commands. Same sort priority commands take place in random order. For example, if both players and player lists are replicas, you would want to create the players before the player lists if the player lists refer to the players. So you could specify the players as priority 0, and the lists as priority 1, and the players would be created and serialized first

Returns:
  A higher value to process later, a lower value to process sooner, the same value to process in random order.

virtual ReplicaReturnResult Replica::ReceiveDestruction | ( | RakNet::BitStream * | inBitStream,
|  | SystemAddress | systemAddress,
|  | RakNetTime | timestamp |
| ) |  |  | `[pure virtual]`

This function is called when SendDestruction is sent from another system. Delete your object if you want.

Parameters:

  `[in]` | inBitStream | What was sent in SendDestruction::outBitStream
  `[in]` | systemAddress | The participant that sent this message to us.
  `[in]` | timestamp | if Serialize::SendDestruction was set to true, the time the packet was sent.

Returns:
  See ReplicaReturnResult. Only REPLICA_PROCESSING_DONE is valid, and will send the destruction message. Anything else will not send any messages.

virtual ReplicaReturnResult Replica::ReceiveScopeChange | ( | RakNet::BitStream * | inBitStream,
|  | SystemAddress | systemAddress,
|  | RakNetTime | timestamp |
| ) |  |  | `[pure virtual]`

Called when when we get the SendScopeChange message. The new scope should have been encoded (by you) into inBitStream

Parameters:

  `[in]` | inBitStream | What was sent in SendScopeChange::outBitStream
  `[in]` | systemAddress | The participant that sent this message to us.
  `[in]` | timestamp | if Serialize::SendScopeChange was set to true, the time the packet was sent.

Returns:
  See ReplicaReturnResult

virtual ReplicaReturnResult Replica::SendConstruction | ( | RakNetTime | currentTime,
|  | SystemAddress | systemAddress,
|  | unsigned int & | flags,
|  | RakNet::BitStream * | outBitStream,
|  | bool * | includeTimestamp |
| ) |  |  | `[pure virtual]`

This function is called in the first update tick after this object is first passed to ReplicaManager::Replicate for each player, and also when a new participant joins The intent of this function is to tell another system to create an instance of this class. You can do this however you want - I recommend having each class having a string identifier which is registered with StringTable, and then using EncodeString to write the name of the class. In the callback passed to SetReceiveConstructionCB create an instance of the object based on that string.

Note:
  If you return true from IsNetworkIDAuthority, which you should do for a server or peer, I recommend encoding the value returned by GetNetworkID() into your bitstream and calling SetNetworkID with that value in your SetReceiveConstructionCB callback.

  Dereplicate, SetScope, and SignalSerializeNeeded all rely on being able to call GET_OBJECT_FROM_ID which requires that SetNetworkID be called on that object.

  SendConstruction is called once for every new player that connects and every existing player when an object is passed to Replicate.

Parameters:

  `[in]` | currentTime | The current time that would be returned by RakNet::GetTime(). That's a slow call I do already, so you can use the parameter instead of having to call it yourself.
  `[in]` | systemAddress | The participant to send to.
  `[in,out]` | flags | Per-object per-system serialization flags modified by this function, ReplicaManager::SignalSerializationFlags, and ReplicaManager::AccessSerializationFlags. Useful for simple customization of what you serialize based on application events. This value is not automatically reset.
  `[out]` | outBitStream | The data you want to write in the message. If you do not write to outBitStream and return true, then no send call will occur and the system will consider this object as not created on that remote system.
  `[out]` | includeTimestamp | Set to true to include a timestamp with the message. This will be reflected in the timestamp parameter of the callback. Defaults to false.

Returns:
  See ReplicaReturnResult

virtual ReplicaReturnResult Replica::SendDestruction | ( | RakNet::BitStream * | outBitStream,
|  | SystemAddress | systemAddress,
|  | bool * | includeTimestamp |
| ) |  |  | `[pure virtual]`

The purpose of the function is to send a packet containing the data in outBitStream to systemAddress telling that system that Dereplicate was called. In the code, this is called in the update cycle after you call ReplicaManager::Destruct(). Then, if you write to outBitStream, a message is sent to that participant. This is the one send you cannot delay because objects may be deleted and we can't read into them past this.

Parameters:

  `[out]` | outBitStream | The data to send. If you do not write to outBitStream, then no send call will occur
  `[in]` | systemAddress | The participant to send to.
  `[out]` | includeTimestamp | Set to true to include a timestamp with the message. This will be reflected in the timestamp parameter of the callback. Defaults to false.

Returns:
  See ReplicaReturnResult

virtual ReplicaReturnResult Replica::SendScopeChange | ( | bool | inScope,
|  | RakNet::BitStream * | outBitStream,
|  | RakNetTime | currentTime,
|  | SystemAddress | systemAddress,
|  | bool * | includeTimestamp |
| ) |  |  | `[pure virtual]`

Called when ReplicaManager::SetScope is called with a different value than what it currently has. It is up to you to write inScope to outBitStream. Not doing so, and returning true, means you want to cancel the scope change call. If inScope is true, you return true, and data is written to outBitStream, then Serialize will be called automatically This is a convenience feature, since there's almost no case where an object would go in scope but not be serialized

Parameters:

  `[in]` | inScope | The new scope that will be sent to ReceiveScopeChange that originally came from SetScope
  `[out]` | outBitStream | The data to send. If you do not write to outBitStream and return true, then no send will occur and the object will keep its existing scope
  `[in]` | currentTime | The current time that would be returned by RakNet::GetTime(). That's a slow call I do already, so you can use the parameter instead of having to call it yourself.
  `[in]` | systemAddress | The participant to send to.
  `[out]` | includeTimestamp | Set to true to include a timestamp with the message. This will be reflected in the timestamp parameter of the callback. Defaults to false.

Returns:
  See ReplicaReturnResult

virtual ReplicaReturnResult Replica::Serialize | ( | bool * | sendTimestamp,
|  | RakNet::BitStream * | outBitStream,
|  | RakNetTime | lastSendTime,
|  | PacketPriority * | priority,
|  | PacketReliability * | reliability,
|  | RakNetTime | currentTime,
|  | SystemAddress | systemAddress,
|  | unsigned int & | flags |
| ) |  |  | `[pure virtual]`

Called when ReplicaManager::SignalSerializeNeeded is called with this object as the parameter. The system will ensure that Serialize only occurs for participants that have this object constructed and in scope The intent of this function is to serialize all your class member variables for remote transmission.

Parameters:

  `[out]` | sendTimestamp | Set to true to include a timestamp with the message. This will be reflected in the timestamp parameter Deserialize. Defaults to false.
  `[out]` | outBitStream | The data you want to write in the message. If you do not write to outBitStream and return true, then no send will occur for this participant.
  `[in]` | lastSendTime | The last time Serialize returned true and outBitStream was written to. 0 if this is the first time the function has ever been called for this systemAddress
  `[in]` | priority | Passed to RakPeer::Send for the send call.
  `[in]` | reliability | Passed to RakPeer::Send for the send call.
  `[in]` | currentTime | The current time that would be returned by RakNet::GetTime(). That's a slow call I do already, so you can use the parameter instead of having to call it yourself.
  `[in]` | systemAddress | The participant we are sending to.
  `[in,out]` | flags | Per-object per-system serialization flags modified by this function, ReplicaManager::SignalSerializationFlags, and ReplicaManager::AccessSerializationFlags. Useful for simple customization of what you serialize based on application events. This value is not automatically reset.

Returns:
  See ReplicaReturnResult

The documentation for this class was generated from the following file:

- C:/RakNet/Source/Replica.h