Skip to content

Replicated Functions

Angus Hollands edited this page Sep 1, 2014 · 8 revisions

Introduction

RPC calls (or replicated function calls) allow the system to invoke a code fragment on a remote target. The system does not permit executing raw code strings.

RPC calls, as a member of the replication system, cannot be used outside of classes that subclass Replicable

Replicated Function Definition

First we must import a few classes from the network library

from network.descriptors import TypeFlag
from network.enums import Netmodes

Now we create a typical function

def print_string(string):
    print(string)

Let's tell the library what type of data string is

def print_string(string: TypeFlag(str)):
    print(string)

And let's ask it to send this to the server, or execute it directly if called on the server

def print_string(string: TypeFlag(str)) -> Netmodes.server:
    print(string)

We might also want to ensure the server receives this

from network.decorators import reliable

@reliable
def print_string(string: TypeFlag(str)) -> Netmodes.server:
    print(string)

We can tell the network system how large a string we are sending, which is assumed 255 characters unless we specify otherwise. This helps save bandwidth, but it must always be greater than or equal to the maximum characters the string will consist of

def print_string(string: TypeFlag(str, max_length=2048)) -> Netmodes.server:
    print(string)

Sometimes, you may wish to inherit the Replicated Function, but change a parameter in the type information for an argument. We use the FromClass decorator (belongs in the same module as the descriptors).

This will look for an attribute named message_length in every class which has this function definition (including the implementing class).

from network.descriptors import FromClass

length_marker = FromClass("message_length")

def print_string(string: TypeFlag(str, max_length=length_marker)) -> Netmodes.server:
    print(string)

Behaviour of Replicated Functions

Invoking an RPC call on the same network target that it is defined for will call the function locally. Otherwise, the arguments of the function are packed and sent to the remote peer.

There are some criteria that govern if the latter step takes place, or whether the function is simply dropped:

  1. If the network object that the method is executed for is not a child of the connection network object, the call is not packaged.
  2. If the recipient doesn't have authority to execute the function, it will not be unpacked.
  3. If an error occurs whilst executing the function, an error message is logged on the side of execution and the program continues normally.
  4. If the function defines return arguments, they are simply ignored.

Getting Started

#Network

Worlds & Scenes

##Replication Overview

Attributes

Functions

Gotchas

##Serialisation Data handlers

##Communication Messaging

#Game Framework ##Bindings Creating bindings

Clone this wiki locally