BigTruck's Fan Page

BOD Python Scripting

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]
Now you must import MyMapTypes to your enemy.py file. Instead of the
EnemyTypes.EnemyDefaultFuncs code you must point the enemy to your new class:
			ork1.Data=MyMapTypes.MyMapOrk(ork1)
			#and assign them the function to be called on death
			
			ork1.Data.Deathfunc=Ork1DED
			# you can pass arguments with this
			
			ork1.Data.Deathargs=(ork1.Name,)
The class will remember all the Deathfuncs/args for all members of the class. (1000's of them if you want)

This is just a quick (but useful) example. Use the same principle to add/modify any aspect of the class. It is complicated stuff, but study some of the classes in the files to get an idea of how it all works.