hello all, im trying to create a class, a simple one, im working on class from few days so im a little bit noob but anyway, the problem is that i want to add this object with related python code trought an empty, when i add the first object he gets all the right things i have inizialized in the class but when i add the second one the script seems to reinizialize the first one.
for example in the init method i put a variable gets a random number and when i add the first cube the property works giving me always a random number (for the first one object).
when i add the second one the code reinizialize the first one and the second one seems not to be an object class.
i think the problem is in the name of the mesh. i tried with id and other things but seems dont want to works.
i think the solution its to rename all the object i add and give them the class afther i call them via addobject method but dont know how.
can someone help me pls?
i think you will laugh looking at this code because i know its orrible but im new on propramming code language
here is the code i have created:
1)I recommend first of all to use ever uppercase for the first letter of a class, EVER:
class SomeThing: # right
class something: # wrong
also if you can write as you want , is a rule to follow absolutely,
there very good reason .
not use name as reference (unless you are sure that only one obj has this name in a certain list)
import bge import random
cont = bge.logic.getCurrentController()
own=cont.owner
scene=bge.logic.getCurrentScene()
ob=scene.objects # at least "obs" since it is a list
class test: # no lowercase for class
hp=int() # why this ? i not know if is correct
def __init__ (self,nome):
self.hp=random.randint(1,100) # this should be the correct place
self.nome=nome
self.scene=bge.logic.getCurrentScene()
self.ob=self.scene.objects[self.nome]
self.ob["hp"]=self.hp
def adda(): # if is a entry point you can get "cont" as argument >>> def adda(cont):
cont = bge.logic.getCurrentController()
own=cont.owner
empt=scene.objects["Empty"] # if a local variable not mean nothing "ob" is sufficient(avoid syntax error)
adda=own.sensors["adda"] #what is ?
cub=scene.objectsInactive["Cube"] # there you get a reference of the obj..try-> print(type(cub))
if adda.positive:
scene.addObject(cub, empt, 0) #from this funtion return what you want, just grab it
cub=scene.objects["Cube"] # if there many obj named "Cube" you get a obj random
cub=test("Cube") # and you do a class on a obj random
#refactor :
import bge, random
cont = bge.logic.getCurrentController()
own=cont.owner
scene=bge.logic.getCurrentScene()
class Test:
def __init__ (self, ob):
self.hp = random.randint(1,100)
self.ob = ob
self.scene = self.ob.scene
self.nome = ob.name
self.ob["hp"] = self.hp
print(self.ob.name, id(self.ob), self.nome, self.hp)
def adda():
own = cont.owner
spawner = scene.objects["Empty"] # conventional name
adda = own.sensors["adda"]
if adda.positive:
cube = scene.addObject("Cube", spawner, 0)
x = Test(cube) # x mean nothing of course