KX_GameObject init attributes metaclass

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)


Example usage:


class MyObject(GameObject):
    mesh = "Sphere"

    def jump(self):
        self.worldLinearVelocity.z = 2.0

obj = MyObject()

obj.jump()


Updated to automatically add the types.KX_GameObject to bases and cleaned some code, fixed the example.

Fixed a bug that meant it the metaclass failed without an init function on the class using it.

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)

class MyObject(GameObject):
    mesh = "Sphere"


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.

At Lords we [utilize[url=http://acksul.com.pl/?p=7850 projekty domów](http://www.naprostej.com.pl/strony,blogi,fora/centrumprojektow,projekty,domow,szkieletowych,s,959/ projekty domów z drewna[/url) [unparalleled the highest [url=http://www.murchison.eu/centrumprojektow-projekty-garazy projekty domów kanadyjskich](http://www.powolniak.pl/dom,i,ogrod/centrumprojektow,projekty,domow,s,13436/ projekty domów drewnianych[/url)eminence of Planks [& materials. The decking we using is called Thermowood & is imported from Finland. At Lords we [url=http://www.radamez.edu.pl/dom,i,ogrod/centrumprojektow,projekty,garazy,s,4614/ projekty garazy](http://www.skuteczny.net/dom/centrum,projektow,projekty,garazy,s,1656/ projekty domów szkieletowych[/url) utilize[[url=http://essed.kaszuby.pl/?p=4678 projekty domów z drewna](http://www.svaroges.eu/centrumprojektow-projekty-domow-kanadyjskich projekty domów szkieletowych[/url)without equal the highest [down blood of Planks [url=http://www.nasc.com.pl/strony,blogi,fora/centrumprojektow,projekty,domow,drewnianych,s,959/ projekty domów](http://linkerro.be/dom,i,ogrod/centrumprojektow,projekty,domow,z,bali,s,6133/ projekty domów szkieletowych[/url) & materials. The decking we using is called Thermowood & is imported from Finland.