I’ll attempt to do some more explaining.
The game object (your enemy) is represented as a python class. What we can do is we can ‘merge’ this with a class that you write, so that the enemy has special enemy-only functions. This helps to keep information self-contained inside the enemy.
To do this we use the procedure known as sub-classing.
class enemyObject(bge.types.KX_GameObject):
Here we have created a class that has all the same functions and properties as a regular game object (eg worldPosition etc)
Then we can add whatever functions, variables anything else we like into the class, remembering to add the ‘self’ parameter into functions, and before class variables.
But we also have to tell the game engine that we want our object to become an enemyObject, and that is where the function ‘make_custom_game_object’ comes in. A better name in this case is ‘make_enemy’:
def make_enemy(cont):
old_object = cont.owner
mutated_object = enemyObject(cont.owner)
After we’ve run it, our game object is an ‘enemyObject’ and the various assert statments were there to check that it had happened properly.
So let’s say that we want to create an enemy with a function ‘kill_player’
Our class will then be:
class enemyObject(bge.types.KX_GameObject):
def __init__(self, old_owner):
self.target = player #presumably player is a global variable? Or you can get is some other way
self.health = 1000
def kill_player(self):
self.target(self.player)
self.fire()
def target(self, targetObj):
self.alignAxisToVect(.... and so on)
def fire(self.targetObj):
...
def activate(self):
'''This function will be run every frame by the function written a bit later'''
self.kill_player()
We then need two functions. One to turn the object into an enemy, and one to activate it:
def make_enemy(cont):
'''Call on the first frame only'''
old_object = cont.owner
mutated_object = enemyObject(cont.owner)
def activate_enemy(cont):
'''call every frame. This will make sure the enemy's code is run'''
cont.owner.activate()
Note that inside the class, instead of using ‘obj = cont.owner’ or anything like that, we simply use ‘self’ Remember that the class IS the enemy.