__init__ in BGE

Hi,

it seems like i can not do whatever i want on the init… am i right?
What can and what cant i do?

here is the code that somhow ignores some operations in the init causing errors

from bge import types, logic, events, render
from mathutils import Vector, Matrix
from math import radians, sqrt
from sup import *




scene = logic.getCurrentScene()

class bullet(types.KX_GameObject):
    
    def __init__(self,own):
        #get the very main controller
        cont = self.controllers[0] 
        
        self.randomZ = 0
        self.randomZ = logic.getRandomFloat()  ############### ignored
        self.randomZ = (round(randomZ, 2) -.5) * 20 ########### ignored
            
        self.collisionSen = cont.sensors["Collision"] ############# ignored
        
        
    def main(self):
        if not "prototype" in self: ######## and this below also
            self.setLinearVelocity([5.0, 0.0, self.randomZ], True)
            
            if self.collisionSen.positive: ####### therefor this gives ERROR
                print(self.collisionSen.hitObject.name)
                self.endObject()
                
            if self["lifetime"] > 10:
                self.endObject()

        


def main(cont):
    
    own = cont.owner
        
    if 'init' not in own:
        own['init'] = True
        own = bullet(own)
    
    own.main()

You need to initialise the GameObject instance.
Add ‘super().init(object)’ before you run your code inside the init function

Looks like i solved it …
was a typo error
i forgot the “self.” somwhere.

Isnt the lower part the instanciation ?
where i call the new class.

Yes, but it doesn’t instantiate the KX_GameObject instance.

Would you mind to explain this to me?
I would also be very happy about a link.
This sound like definitly somthing i need to learn or should know about : )

You can never do whatever you want ;).

You do not need to call super. This is done automatically.

I do not know how the method is called maybe re-instantiation?
Regardless how it is called it replaces an existing Python wrapper (e.g. KX_GameObject) with another one.
To do that you instantiate a direct subclass of KX_GameObject. As the one and only parameter you provide the object to be re-instantiated. Thy class of this object must match the base class of the new class (e.g. providing a KX_FontObject will not be accepted even if it is a KX_GameObject - sounds like a BGE bug).

You do that with this code:

bullet(cont.owner)

You do not need to assign this to a variable. You do not even need the silly “if ‘init’ not in own”

BTW.
I suggest to name classes with leading Uppercase to distinguish them from normal functions.

Try this code:


import bge


class Bullet(bge.types.KX_GameObject):
    def __init__(self,own):
        self.collisionSen = self.sensors["Collision"] # better read when needed
        self.randomZ = (round(bge.logic.getRandomFloat(), 2) -.5) * 20


    def dealCollision(self, cont):
        if "prototype" in self:
            return
        self.setLinearVelocity([5.0, 0.0, self.randomZ], True)
        if not cont.sensors["Collision"].positive:
            print(self.collisionSen.hitObject.name)
            return
        if self["lifetime"] <= 10:
            return
        self.endObject()


def setupBullet(cont):
    Bullet(cont.owner)


def dealCollision(cont):
    cont.owner.dealCollision(cont)

I suggest you add a list of required properties. So you can easily track what properties are necessary.
The same belongs to sensor names. It is really hard to dig all the names from unknown code.

Another suggestion: do not store sensors or controllers somewhere. Better read the current references when needed. This way your code is save from unexpected changes (e.g. called by other objects, controllers, ended objects or reinstantiated objects.

I hope it helps
:wink:

Monster see, how can be clean and complex one code without “return”

return is a exception , if you write ever return break all structure logic of python


if 1:
    daasd
    asda
    adad
    asdasd
    asd
    if 2:
        fdewf
        sfsf
        if 3 :
            sdff
            sfs
            sdfs
        if 3:
            fsdfsaf
            sdfsdfs
            sdfs
    elif 2b:
        sfsdfsd
        sfsfd
        if 2a:
            efwfwf
            wfefw
            wefwef
            wefwf
            if 1:
                if 2:
                    if 3:
                        fwefweòfk
                        wfwefw
                        wefwef