Callback that fires when a property chagnes?

Is there any way to add new callbacks?

Like when a property changes, or an action plays etc, or a property does not equal zero?

Thanks
BPR

No. Most games would use an event framework to do this.

If you write your own getter-setter then you can do so.
So if you mutate your game object and write something like:


class MutatedGameObject(bge.types.KX_GameObject):
    def __setitem__(self, key, item): 
        self.__dict__[key] = item
        if key == "property_to_fire_callback_on":
            fire_callback(self, key)

Then every time you set the value using:


obj['property_to_fire_callback_on'] = 5

The function ‘fire_callback’ will be called with the object and the key.

To be honest, I rarely find callbacks, interrupts or threads useful in a game situation. It adds a lot of semantic complexity without generally helping.
If you are having performance issues, find a different algorithm, don’t try to optimize your current one.

And their is a callback implemented by default on property change. Check the logic bricks! Notice a ‘property’ sensor? It calls python when a property has changed. Sounds like a callback to me.

I was looking into custom classes, and properties defined in them,

and it looks intresting, but I need to only run many code snippets when a timer is not zero.

so this was a means to create a class, and only trigger a callback when some class properties change like

self.jumpTimer etc.

instead of own[‘jumpTimer’]

I only run my weapon code when firing, or reloading, same for assembly code, keys, etc. the code is encapsulated inside a game object.

I was trying to get callbacks to use instead of property sensors, in a effort to move into classes

The BGE’s event system relies on sensors, which measure events. It can already measure property changes as you know. This is for KX_GameObject properties only.

Attributes of your custom class are not part of the BGE’s system as it does not know about it (nor about your class). But this is no big deal. Your code is the only one that can change this attribute. You can notify all interested listeners while changing.