Probably a very basic scripting question

I am in doubt, if some values should be held as member variables or passed as method parameters.
Here is a particular example for this (from my game project):

	def update(self):
		self.boardspeed = self.board["boardspeed"]
		self.height = self.kite["height"]
		self.boardAngle = self.board["angle"]
		self.unhooked = self.kite["unhooked"]
		self.crash = self.kite["gonnaCrash"]

		self.kiteSteering(self.crash, self.unhooked)

almost all the methods need the information about “boardangel, boardspeed, grinding, kite-speed, height, crash and unhooked”
Should those better be passed in the method parameters or just be accessed via self.variable?

Don’t store the information at all, just get it from the board/kite whenever you need it.

You can have a global variable with the board/kite object, or keep a reference to the board/kite on anything that needs it.

As a rule of thumb, you should never keep multiple copies of the same data.

1 Like

Would it be OK to have the following script in the main function and if an object needs that property it could simply access it by adding a property with the exact same name?

for obj in self.object.scene.objects: 
			if "height" in obj:
				obj["height"] = self.object["height"]

No, that for loop is very expensive to run every tic, instead grab the objects you need from it then loop trough that.

#make a list of objects who got property height, create this once on init, and only update this list when needed
if not 'height_objects' in own:
    own['height_objects'] = [obj for obj in own.scene.objects if 'height' in obj]

#now loop trough that list
for obj in own['height_objects']:
    #do something

else to add it just grab the item and assign a property or keep track of them, if you add an object trough python you can directly give it a property


That makes sense, thanks a lot.
So basically it then just loads the objects that have that given property.
I don’t understand the first line of the code though…
if not “height_obj” in own: -> What does it do? Does it just create the list?

if not 'height_objects' in own:
    own['height_objects'] = [obj for obj in own.scene.objects if 'height' in obj]

height_objects is a property in this case. If that property is not in own/self then create a property with a list value with every object that has a property height in them. If height_objects not in own, so at first run of the script, the object the script is run from has not the property, so we can create that property and fill it with the object we need lateron.

now own[‘height_objects’] has all the objects that got a property height in them. now you can loop trough that one to do specific stuff with an object that holds that property.

This is actually not useful for adding a property to it (your question), i read it wrong so the above is just to grab every object with a specific property and build a list out of them to be used later.

But for you question, self.variable is the best way. You simply add that property to it, and whenever you need it you can grab the object and use it’s property. however you can also put that property wich you need 100 times and pput it in an global object, something like a stats holder, then just call it in the script where you need it and use it. Or use globalDict to store some much neeed info, so that you can acces it.

so you can simply do:

#will only be created once
if not 'height_objects' in own:
    own['height_objects'] = [obj for obj in own.scene.objects if 'height' in obj]

#now loop trough that list
for obj in own['height_objects']:
    obj['height'] = blabla

the real diffrence here is, looping trough all objects (your method) or looping once trough all objects, build a list and then only loop trough that list. So if you have 1000 objects and 5 with height property, the script now loops only once trough all the objects, and then it loops trough only 5 objects every tic, while yours keeps looping trough all 1000 objects every tic.