-
Notifications
You must be signed in to change notification settings - Fork 5
Replicated Attributes
Replicated attributes offer fast, simple server to client data transfer. Attributes can be marked to trigger a callback when modified. A conditions generator determines when attributes should be replicated to the remote peer. An optional hash comparison can be performed on a set of variables that have complex conditions; meaning that expensive checks can be cut out.
First we must import some classes from the network library
from network.replication import Serialisable
from network.replicable import ReplicableNow let's create a network class
class NetworkObject(Replicable):
passAnd give it a Replicated Attribute called name
class NetworkObject(Replicable):
name = Serialisable("Alex")Sometimes we might not want to define a starting value. However, we need to define the value type, so that it can be serialised later on. So let's define its type
class NetworkObject(Replicable):
name = Serialisable(data_type=str)We need to define when the server will send this to relevant clients. To do this we use a conditions generator function.
A basic replication conditions generator looks like this. The super class Replicable defines some conditions, so don't forget to yield from the super() method
def can_replicate(self, is_owner, , is_initial):
yield from super().conditions(is_owner, is_initial)Let's tell the generator to send the name attribute
def can_replicate(self, is_owner, is_initial):
yield from super().conditions(is_owner, is_initial)
yield "name"Sometimes, it might be useful to be notified when an Serialisable was changed over the network. To do this, we ask the Serialisable to notify the class when modified by the network. This notification occurs once all attributes have been set (for all network objects). To enable notification, we set the notify_on_replicated argument to True.
class NetworkObject(Replicable):
name = Serialisable(data_type=str, notify_on_replicated=True)
def can_replicate(self, is_owner, is_initial):
yield from super().conditions(is_owner, is_initial)
yield "name"And then we define our own function which is called when a notifier Serialisable is changed.
class NetworkObject(Replicable):
name = Serialisable(data_type=str, notify_on_replicated=True)
def can_replicate(self, is_owner, is_initial):
yield from super().conditions(is_owner, is_initial)
yield "name"
def on_replicated(self, name):
if name == "name":
print("Name was changed!)
else:
super().on_notify(name)The three arguments infer context for replication.
-
is_ownerindicates whether the target for this replication is the "owner" over this object. -
is_initialindicates whether this client has been replicated to before
#Network
##Replication Overview
##Serialisation Data handlers
##Communication Messaging
#Game Framework ##Bindings Creating bindings