Tutorial:Twisted Networking with FIFE

From FIFE development wiki
Jump to: navigation, search

This article is outdated and is just stored for archive purposes. Archived.png

This article became outdated and is just stored for archive purposes in this wiki. There are several reasons why an article could become outdated. The development team may have decided to use a different concept or even the author itself felt that the article is not really up-to-date with the current development status of the project anymore.


Introduction

This little tutorial will cover the basics of using FIFE with the Twisted framework to write networked games.

Making FIFE work with Twisted

The Twisted Framework operates asynchronously and when you write an application using Twisted you can't have synchronous calls such as the while instruction for example.

Making FIFE work asynchronously

The python client base class ApplicationBase is setup to use FIFE synchronously. The mainLoop function in ApplicationBase needs to be overridden to make it work asynchronously with Twisted.

Why the current mainLoop method does not work with Twisted

Before making any change to the mainLoop implementation lets understand why Twisted would not work with the basic mainLoop method provided by FIFE.

Consider the following diagram :

Fife twisted.png

As you can see twisted works synchronously, so you can't run another process in same thread.

The new main loop

def mainLoop(self):
    '''
    Override the mainLoop method to
    make it works with twisted framework.
    '''
 
    try:
        self.engine.pump()
 
    except RuntimeError, e:
        print str(e)
 
    self._pump()
 
    if self.breakRequested:
        self.breakRequested = False

And in the __init__ method of the main application:

self.main_loop = LoopingCall(self.mainLoop)
self.main_loop.start(0.032) #30 FPS

FIFE is now working asynchronously and is compatible with the Twisted Framework.

Download Twisted

All the available downloads are centralized here :

http://twistedmatrix.com/trac/wiki/Downloads

Dependencies

Twisted depends on :

Learn more about Twisted

This tutorial is not a Twisted tutorial, it just provides a way to implement it with FIFE and the demo is the illustration of this example. If you are not familiar with twisted and if you are interested in developing networked games, I recommend looking at:

The demo

This demo is a really simple network game using FIFE and Twisted. To run it you need FIFE, Python 2.6 (at least) and the latest version of Twisted (see 'Download Twisted' section). The game uses the rio_de_hola map and implements the following features:

  • GUI : allows user to choose a nickname and hostname, also manage widgets to inform the user when connection fail etc...
  • Basic online interactions : walking around the beach with other players

Download

You can download the demo as:

Note : you will have to copy the 'objects' directory from <fife>/demos/rio_de_hola into the twisted_fife_demo directory.

Test it

To run the server:

python rpg_server.py

To run the client:

python run.py

Questions, Features Requests, etc...

You can contact me at nathan DOT open AT gmail DOT com or on the fife irc #fife @FreeNode