Tutorial:Twisted Networking with FIFE
- 1 Introduction
- 2 Making FIFE work with Twisted
- 3 Download Twisted
- 4 The demo
- 5 Questions, Features Requests, etc...
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 :
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.
All the available downloads are centralized here :
Twisted depends on :
- zope.interface : http://pypi.python.org/pypi/zope.interface#download
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:
- Introduction for Twisted : http://krondo.com/blog/?p=1209. This is maybe the best way to start with Twisted.
- The official documentation is also really useful : http://twistedmatrix.com/trac/wiki/Documentation
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
You can download the demo as:
- tarball : http://wiki.fifengine.net/File:Twisted_fife_demo.tar.gz
- zip archive : http://wiki.fifengine.net/File:Twisted_fife_demo.zip
Note : you will have to copy the 'objects' directory from <fife>/demos/rio_de_hola into the twisted_fife_demo directory.
To run the server:
To run the client:
Questions, Features Requests, etc...
You can contact me at nathan DOT open AT gmail DOT com or on the fife irc #fife @FreeNode