Updated July 24, 2005
HOME PAGE > AUTHORS > PROSPERO > BOD PYTHON SCRIPTING (16)
Tut 16. Classes.
Heavy stuff here.
Classes are basically databases. When the data contained within a
class is assigned to something the something is controlled by the class. Any
number of things can be controlled individually by the same class. Whats more
you can create sub-classes that add extra features to the parent class. All
the Characters in the game derive their data from the class 'PlayerPerson' in
Basic_Funcs.py. This initializes all the core functions and variables.
Player
chars use their data directly from this class.
A sub-class. NPCPerson in
Enm_Def.py derives (or
'inherits') from this class and adds other data as well as
all the AI features for enemies.
Each enemy race then has it's own
sub-class of NPCPerson in the file EnemyTypes.py.
Now comes the
interesting bit. You can make your own classes to alter enemy behaviour (within
certain limits). In the last tut I demo'ed how to get a function called from
enemy death. Perfectly sound method, but say you had 100 orks in your map and
you wanted that code on each one. You would have a lot of repetitive code to
write. Better if you use a subclass with that feature built in:
Make a
new file. MyMapTypes.py
import Bladex
import EnemyTypes
import Enm_Def
import GameStateAux
class MyMapOrk(EnemyTypes.Ork):
DeathFunc=None
Deathargs=()
def __init__(self,me):
EnemyTypes.Ork.__init__(self, me)# initialize parent class
me.ImDeadFunc=self.NewImDeadFunc
def NewImDeadFunc(self, EntityName): # define new ImDeadFunc
Enm_Def.NPCPerson.StdImDead(self,EntityName)
if self.Deathfunc:
apply(self.Deathfunc,self.Deathargs)
# Saving and Loading code
def __getstate__(self):
NPCPerson_state= EnemyTypes.Ork.__getstate__(self)
if (NPCPerson_state[0]!=1):
return NPCPerson_state
NPCPerson_state[1]["MyMapOrk"]=(GameStateAux.SaveFunctionAux(self,Deathfunc),self.Deathargs)
return NPCPerson_state
def __setstate__(self,parm):
EnemyTypes.Ork.__setstate__(self,parm)
version=parm[0]
if version==1:
parms=parm[1]["MyMapOrk"]
GameStateAux.LoadFunctionAux(parms[0],self,"Deathfunc")
self.Deathargs=parms[1]