# KX\_GameObject init attributes metaclass

**URL:** <https://blenderartists.org/t/kx-gameobject-init-attributes-metaclass/562507>\
**Category:** Game Engine Resources\
**Created:** [January 15, 2013, 3:39pm UTC](https://blenderartists.org/t/kx-gameobject-init-attributes-metaclass/562507 "2013-01-15T15:39:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agoose77](https://blenderartists.org/user_avatar/blenderartists.org/agoose77/32/442949_2.png) [@agoose77](https://blenderartists.org/u/agoose77)\
**Post date:** [January 15, 2013, 3:39pm UTC](https://blenderartists.org/t/kx-gameobject-init-attributes-metaclass/562507/1 "2013-01-15T15:39:28Z")

</div>

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!

```auto

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:

```auto

class MyObject(GameObject):
    mesh = "Sphere"

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

obj = MyObject()

obj.jump()

```

---

<div class="post-metadata">

**Author:** ![agoose77](https://blenderartists.org/user_avatar/blenderartists.org/agoose77/32/442949_2.png) [@agoose77](https://blenderartists.org/u/agoose77)\
**Post date:** [February 3, 2013, 8:55am UTC](https://blenderartists.org/t/kx-gameobject-init-attributes-metaclass/562507/2 "2013-02-03T08:55:53Z")

</div>

Updated to automatically add the types.KX\_GameObject to bases and cleaned some code, fixed the example.

---

<div class="post-metadata">

**Author:** ![agoose77](https://blenderartists.org/user_avatar/blenderartists.org/agoose77/32/442949_2.png) [@agoose77](https://blenderartists.org/u/agoose77)\
**Post date:** [February 16, 2013, 10:13am UTC](https://blenderartists.org/t/kx-gameobject-init-attributes-metaclass/562507/3 "2013-02-16T10:13:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![agoose77](https://blenderartists.org/user_avatar/blenderartists.org/agoose77/32/442949_2.png) [@agoose77](https://blenderartists.org/u/agoose77)\
**Post date:** [April 21, 2013, 4:09pm UTC](https://blenderartists.org/t/kx-gameobject-init-attributes-metaclass/562507/4 "2013-04-21T16:09:38Z")

</div>

Now it’s even easier!  
Using a new constructor you can avoid this hackery!

```auto

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"

```

---

<div class="post-metadata">

**Author:** ![agoose77](https://blenderartists.org/user_avatar/blenderartists.org/agoose77/32/442949_2.png) [@agoose77](https://blenderartists.org/u/agoose77)\
**Post date:** [May 12, 2013, 9:21am UTC](https://blenderartists.org/t/kx-gameobject-init-attributes-metaclass/562507/5 "2013-05-12T09:21:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TickRari](https://blenderartists.org/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@TickRari](https://blenderartists.org/u/TickRari)\
**Post date:** [June 14, 2013, 3:03pm UTC](https://blenderartists.org/t/kx-gameobject-init-attributes-metaclass/562507/6 "2013-06-14T15:03:20Z")

</div>

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/](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/](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/](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](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/](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.
