Home - Forums - Documentation - Gallery - Bugs

Contents

Introduction

This is documentation for the future python property class system.

A Simple Python Property Class

from pycel import *
 
class RotatePropClass(pyPcCommon):
        def __init__(self,oreg):
                pyPcCommon.__init__(self,oreg)
                PhysicalLayer.CallbackEveryFrame(self,CEL_EVENT_PRE)
        def TickEveryFrame(self):
                mov = celMesh(self.GetEntity()).Mesh.GetMovable()
                rot = csYRotMatrix3(0.1)
                mov.Transform(rot)
                mov.UpdateMove()
 
CEL_IMPLEMENT_FACTORY(RotatePropClass,"pcrotate")

Where "pcrotate" would be the actual name to use when creating such a property class.

Receiving Messages

class RotatePropClass(pyPcCommon,pyMessageReceiver):
        def __init__(self,oreg):
                pyPcCommon.__init__(self,oreg)
                pyMessageReceiver.__init__(self,oreg)
                PhysicalLayer.CallbackEveryFrame(self,CEL_EVENT_PRE)
        def ReceiveMessage(self,msg, sender, ret, params):
            if msg == getid("cel.foo.bar"):
                 print "do something"
            elif msg == getid("cel.foo.anotherbar"):
                 print "do something else"
            return 0
        def TickEveryFrame(self):
                mov = celMesh(self.GetEntity()).Mesh.GetMovable()
                rot = csYRotMatrix3(0.1)
                mov.Transform(rot)
                mov.UpdateMove()


Sending Messages

entity.MessageChannel.CreateMessageDispatcher(self,"cel.foo.bar").SendMessage(pars)
 
or
 
entity.MessageChannel.SendMessage("cel.foo.bar",self,pars)

Properties

Python property classes support a mechanism to transparently treat python class attributes as cel properties, this means any property you set on the map or elsewhere (like quests) will directly translate to a python attribute change in your instance.

You can override SetProperty to intercept property setting from the outside and react appropriately on your code. Otherwise you have to assume your property can be changed anytime from the outside.

class AnotherPropClass(pyPcCommon):
        def __init__(self,oreg):
                pyPcCommon.__init__(self,oreg)
        def SetProperty(self,id,val):
                print "property set from outside world!",fromid(id),val
                return pyPcCommon.SetProperty(self,id,val)

Actions

Actions are also transparently translated to instance method calls. This methods need to have args as first parameter, which will be a celParameterBlock.

class TalkingPropClass(pyPcCommon):
        def __init__(self,oreg):
                pyPcCommon.__init__(self,oreg)
        def InitTalk(self,args):
                print "InitTalk"


Loading your property classes

Usually you will place the .py file somewhere in pythonpath, and add some config options to celstart.cfg (or your own app).

CsPython.Module.1 = mypcclasses

(where the file would be mypcclasses.py in this case).

You can add as many files as you want using this mechanism.

For this to work you need cspython loaded, either manually in some programming language or by placing the following in config file (like celstart.cfg for celstart):

System.Plugins.iScript = crystalspace.script.python

| Article | Discussion | View source | History |