Hey everyone.
Anyone who has subclassed KX_GameObject will have noticed that it is rather tricky to pass arguments to the constructor. Here is a metaclass which performs some operations to get the init function to work as you’d expect.
If you want to write other metaclasses, simply inherit from this metaclass and call the super().new as normal.
Now it’s even easier!
Using a new constructor you can avoid this hackery!
from bge import types, logic
class GameObject(types.KX_GameObject):
def __new__(cls, *args, **kwargs):
own = logic.getCurrentController().owner
obj = own.scene.addObject(cls.mesh, own)
return super().__new__(cls, obj)
Added newer, far simpler version.
Beware that if you do not specify an initialiser, you may instantiate the object with any number of ignored arguments.