Yes, you have a common problem, you are using module mode, that means all your code should be contained in functions or classes. There shouldn’t be any code at the root level, except import BGE.
#define
cont = logic.getCurrentController()
own = cont.owner
scene = logic.getCurrentScene()
objects = scene.objects
sens = cont.sensors
col = sens["collide"]
space = sens["space"]
slotsList = []
Here’s you problem area. Each time your script runs, it is setting slots list to [].
You should define your variables only in the module you are running:
def items(cont):
own = cont.owner
scene = logic.getCurrentScene()
objects = scene.objects
sens = cont.sensors
Notice that the active controller is passed automatically as an argument of the function.
You need to make slotsList either a property of own (own[‘slotslist’]) or a part of the globaldict (GlobalDict[‘slotsList’])
If you want to define something once, try writing an initiation section for your script:
if "ini" not in own:
own['slotsList'] = []
own['ini'] = True
This will run only once, since you add the “ini” property to your own object during this chunk of code.
The initiation code is a good place to define default properties or to generate walkmeshes, place enemies, swap skins, activate 2d filters etc… as you can be sure it will be the first thing the object does and it won’t do it again.
When you uses classes in python they already have an initiation function, but if not this is a good workaround.
You can even make it in to a function:
def player_ini(own):
if "ini" not in own:
own['slotsList'] = []
own['ini'] = True
def player(cont):
own = cont.owner
player_ini(own)
to keep your code separate.