Problem with class...

So, in my game I have enemies that the player must fight.

Original, I know.

I made a class, and in that class, will be different functions for different enemy movement patterns, as well as some universal things in init that can be set when it’s instanced. (max HP, ext…)
Right now there’s only one movement pattern; go to the player and shoot regularly.

Now, part of the code outside the class - in the function the enemy is running - makes it so that the class is only instanced when the player gets to a certain point - AKA the enemy “wakes up”

(the game automatically scrolls, so the enemies will be “encountered” in the same order every time.)

I started running into issues when I made multiple enemies in the level - but here’s the catch - the order the enemies appear in affects which one messes up in what way, and not consistently either - while the 4th or 3rd or whatever enemy always has the same problem, every time you add another enemy, who knows what it’s do? it’s not like “4-5 do X, and every odd numbed one past 7 does Y…”

All I can think is that somehow each enemy is affecting the one that comes next, like the HP value being transferred or whatever, but, if one object is running a script, and another is running the same script, they should be totally independent of one another, right?

I don’t need to know exactly-what-line-of-code-is-messing-this-up, just why this may be happening, so I can fix it now and forever-more, but if you do need the .blend, just say so, and I’ll attach it to a PM or something.

Thanks.

I have an idea of what the problem could be, but you would probably do well to post your code that defines the class and instantiates it in each enemy.

I think I know why this is happening, but I can be wrong. If you are initializing values that supposed to be applied to one object, you should store the class as a duplicate into a property on that object so it will behave locally.

So instead of:

class MyClass:
    
    def __init__(self):
        # initializing
        
    def main(self, object):
        # main function of the class
        
my_class = MyClass()
        
def my_module(cont):
    own = cont.owner
    my_class.main(own)

You can do this:

class MyClass:
    
    def __init__(self):
        # initializing
        
    def main(self, object):
        # main function of the class
        
def my_module(cont):
    own = cont.owner
    if 'my_class' not in own:
        own['my_class'] = MyClass()
    own['my_class'].main(own)

Perhaps the correct way to do this (or at least another way to do this) is to subclass the game object.

It’s pretty easy to do, and can make some things easier (eg you don’t have to store the class in a property, the object IS the class).

Quite simply, you co it like this:


import bge

class CustomGameObject(bge.types.KX_GameObject):
    def __init__(self, old_owner):
        '''Runs when you create/mutate the game object'''

    def update(self):
        '''some specific function you want to add to the game object'''

# Called first
def make_custom_game_object(cont):
    old_object = cont.owner
    mutated_object = CustomGameObject(cont.owner)

    # And you can check to make sure this all worked:
    assert(old_object is not mutated_object)
    assert(old_object.invalid)
    assert(mutated_object is cont.owner)

def use_custom_game_object(cont):
    cont.owner.update()

(Based heavily off the example on the blender API page)

Once the AI object is mutated, there is no way it can interfere with other objects because, well, it doesn’t know they exist, it doesn’t share any variables etc.

The description soinds like the enemies share some data or in other words they do not have encapsulated (separate) data. They should not know the internals of the other objects.

Unfortunatelly without more details e.g. Code or demo blend it is hard to tell what the cause is.

I suggest to add some print satements. To find identical or separate obects you can print the id with the id() function.

E.g


print( interestingObject, id(interestingObject) )

Maybe it helps.

*** Moderation ***
Threadsplit:

This thread turned away from dragonspammer’s initial request. Nevertheless it is an interesting discussion which can be followed in -Subclassing-game-objects.

*** End of Moderation ***