python modules only work on one new object

Anything in the highlighted parts are only run once when the script is loaded by the Python Module controller.

Edit: seems this has already been marked as solved

I have tried to make this code act in a way that is object-oriented but it does not.

I tested some outputs and found that when I output ‘PlayerBulletScript1.update’ with an activate:True sensor ‘init()’ is being called every frame.

I want ‘init()’ to be called once, I want PlyerBullet.update() to be called every frame and I want to be able to change class level variables from update().

The variable count always prints out ‘1’ which tells me that its always getting reset back to its defualt ‘0’ value.


import bge
from bge import logic, types, events


cont = bge.logic.getCurrentController()
obj = cont.owner
count=0


keyboard = logic.keyboard


class PlayerBullet:
    def __init__(self, owner):
        self.owner = owner
        self.speed = 0.5
        self.count=count
        print('bulletScript')
     
    def update(self):
        global count 
        self.owner.applyMovement((0, self.speed, 0), False)
        self.count+=1
        print(self.count)
    
    def die(self):
        self.owner.endObject()


logic.globalDict["PlayerBulletScript1"] = PlayerBullet(obj)


def update(cont): #is the 'cont' an automatic argument for the python module controler
    own = cont.owner    
    bullet = PlayerBullet(own)    
    bullet.update()

thanks.

What do you mean with

?

You create a PlayerBullet object each time you trigger update(). Btw. the first time you call this function you create two objects.

This code look like it has a few design problems.
What is it supposed to do?

Hi monster. Thanks for the response. This is a bullet class. I want every bullet to have its own count. I a use to Java style programming where you create a class and use the ‘new’ keyword to generate new instances of that class where variables are initialized once and can change during the game. This is what I am trying to achieve with one script.

The only purpose of this class is to count something?

It looks like it is counting the lifetime of this object assuming update is called at each frame. Is it used somewhere?

I do not think you need a class for such a primitive operation. You can use a module function to read increase and write a property of the KX_GameObject representing the bullet. At this point there is really no need for an additional Python object. It adds unnecessary complexity to a very trivial task.

You will benefit from an additional object when you have more than one data to manage. Even then a container (e.g. a list or dict) will be sufficient enough.

A class might be worth when you need logic and data that is outside of the game objects. E.g. a selector object, save/Loader object, inventory object. The KX_GameObjects act as interface to the scene.

Somehow this is difficult to explain.

is there a way to have a Bullet class where each bullet has its own properties independent of the other bullets (using classes)?

You class already does that. But you create a new object every time you call update(). I think I already wrote that.

I am asking if I can code properties in a class that behave the same way as adding properties to an object in the editor.

A (bge) property is just a key/value pair in a container. The container is the KX_GameObject.

You can create a class that acts the same way. This is pretty simple - you inherit from dict.

o yeah. Thanks Monster.