I'm having trouble removing objects from the game

I am trying to make an inventory system where items get added to a list when the E key is pressed while the character is within the item’s collider, and then the item’s model is removed from the game. I thought the way remove objects was with endObject(), but it doesn’t seem to be working. I have a Collision sensor on the character that looks for the “item” property, and it’s attached to a Controller with a python script loaded into it. The items have the “item” property, and here is my script so far:

import bge

keyboard = bge.logic.keyboard
item = bge.logic.getCurrentController().sensors["Collision"].hitObject
eKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.EKEY]
itemList = []

if eKey:
    itemList.append(item)
    item.endObject()

the collision is most likely over when you pres the E key and if so then item would not contain hitobject but None instead.
(check the console for errors)

1 Like

Hi Elsyndur! if you want to delete the object received by the sensors you need to add it to the local variable, do not use collision sensors for such an operation-use ray sensors or radars or beam throw to get the object, when I use regular sensors I do so -
if ray.positive:
obj = ray.hitObject
list.insert(0,obj)
obj.endObject()
to write, use a list or dictionary where you add or insert your object as a variable list.insert (0, obj) or list. append(obj) to check, use print(list) to check whether the specified object is in your lists

1 Like

you need a collision check and a property check, you can’t just go run around holding E key, it will try to append nothing without a collision and throwing errors.

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

keyboard = bge.logic.keyboard
col_sens = cont.sensors["Collision"]
itemList = []

if col_sens.positive:
    
    item = col_sens.hitObject
    
    if 'some_property' in item:

        eKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.EKEY]

        if eKey:
            itemList.append(item)
            item.endObject()
1 Like

Ahh, I see. Thanks, guys!