Skip to content

Replicated Attributes

Angus Hollands edited this page Oct 17, 2015 · 10 revisions

Introduction

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.

Replicated Attribute Definition

First we must import some classes from the network library

from network.replication import Serialisable
from network.replicable import Replicable

Now let's create a network class

class NetworkObject(Replicable):
    pass

And 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.

Replication Conditions

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 two arguments infer context for replication.

  • is_owner indicates whether the target for this replication is the "owner" over this object.
  • is_initial indicates whether this client has been replicated to before

Getting Started

#Network

Worlds & Scenes

##Replication Overview

Attributes

Functions

Gotchas

##Serialisation Data handlers

##Communication Messaging

#Game Framework ##Bindings Creating bindings

Clone this wiki locally