End an added object problem


import bge, os

def firstload():
 g = bge.logic
 s = g.getCurrentScene()
 proxyobj = s.objectsInactive["proxyobj"]
 
 filename="Weapons"
 elenco = bge.obj_data
 
 #add the library
 path = g.expandPath("//") #the path of the runtime/blend
 sep = os.path.sep
 file = path + "Data" + sep + "Obj" + sep + filename + ".blend"
 try:
  g.LibLoad(file,"Mesh")
 except:
  pass
 
 current_list = elenco.keys()
 current_list = sorted(current_list)
 bge.obj_added= []
 for n in current_list:
  infos = elenco[n]
  if str(infos[1]) == str(s):
   objadded = s.addObject(proxyobj, proxyobj,0)
   objadded.worldPosition = infos[2]
   objadded.worldOrientation = infos[3]
   objadded.replaceMesh(infos[0])
   objadded["item"] = 1
   objadded["nome"] = str(infos[0])
   bge.obj_added.append(objadded)

this add a list of objects

instead, this other funtion should delete the nearest object…


for ob in prinobjlist: #search for all the possible object to add in the list that is created when Loader Module put all the objects in the scene
    #get the distance from the character
    distance = ob.getDistanceTo(o)
    try: #control if the object can be picked up
     ob["item"]
    except:
     ob["item"] = 0
    if distance <= 4.000 and ob["item"] == 1: #if the object is enough near 
     nearobjlist.append(ob)#add to the list of the legal objects
   
   if nearobjlist !=  []:
    #print(nearobjlist)
    ob = find_nearest(o.worldPosition,nearobjlist) #find the nearest object
... 
do things
... 
    print(bge.obj_added)
    print(ob)
    #obindex = bge.obj_added.index(ob)      (1)
    #del bge.obj_added[ob]                        (2)
    ob.endObject() #delete the object from the scene
    print(ob.endObject())


def find_nearest(point,list):
    nearest = None
    old_dist = None
    for obj in list:
        dist = (point - obj.worldPosition).length
        if not old_dist or old_dist > dist:
            nearest = obj
            old_dist = dist
    return nearest

The console prints


[proxyobj,proxyobj]
proxyobj
None

and the object is not deleted from the scene…
In addition, (1) and (2) gives error, without saying why it’s raised…
Any idea on how to solve?
Thanks

you could use

bge.obj_added.remove(ob)

to remove the reference from the list

ob.endObject()

should end the KX_GameObject within the next frame.

i found what was wrong.
I used addObject(proxjobj, proxjobj), but i read few hours ago that the object where added in the same layer of the reference(the second input), that was a invisible layer. So i changed to (proxyobj, owner) and now everything works fine(except that the loader module is run 2 times, dunno why, but i put a condition at the start so that every object is loaded only one time

check this thread: http://blenderartists.org/forum/showthread.php?t=200954

mmm… a bit like a matrioska, a link that point to another link, but i read the article… But since the sensor is an always sensor with tap enabled, i can’t get way it repeat himself… The article also says that if tap is enabled then the true pulse is followed by a false pulse…

The point is an controller is executed whenever it receives a Pulse regardless if true or not.
With tap you get
frame 1: True pulse activation
frame 2: False pulse activation
Sum = 2

The conclusion:

do not execute your code on false pulse!