this code highlights a object, for 60 frames, then is to remove it from the list of highlighted objects,
all works, except I am silly and can’t remember how to use pop list?
this could be used for highlighting objects, or making them go transparent temporarily, (camera goodness)
import bge
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
sens = cont.sensors['Mouse1']
if 'List' not in own:
own['List']=[]
own['List2']=[]
if sens.positive:
alpha=sens.hitObject.color
sens.hitObject.color=(sens.hitObject['ColorX']+.5,sens.hitObject['ColorY']+.5,sens.hitObject['ColorZ']+.5, alpha[3])
if sens.hitObject not in own['List2']:
own['List2']=own['List2']+[sens.hitObject]
own['List']=own['List']+[60]
else:
iter=0
for items in own['List2']:
if items==sens.hitObject:
own['List'][iter]=60
iter+=1
length=0
for items in own['List']:
length+=1
if length > 0:
L=0
for item in own['List']:
Time=own['List'][L]
if Time>1:
own['List'][L]-=1
else:
target=own['List2'][L]
target.color=(target['ColorX'],target['ColorY'],target['ColorZ'],target['Alpha'])
## how do I remove a item from a list again?
L+=1
##print(str(own['List']))
main()
x = 0
while x < len(list_o_stuff):
if list_o_stuff[x] == target:
list_o_stuff.remove(target)
else:
x += 1
Or…
x = 0
while x < len(list_o_stuff):
if list_o_stuff[x] == target:
throwaway_var = list_o_stuff.pop(x)
else:
x += 1
-edit-
Completely unrelated, but you can replace this code:
length=0
for items in own['List']:
length+=1
if length > 0:
With…
if len(own['List']):
Try using “while” loops instead of “for” loops for this script. Much better if you need to work with the index number. If you start popping things off a list while you’re going over it in a “for” loop then nasty things can happen.
import bge
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
sens = cont.sensors['Mouse1']
if 'List' not in own:
own['List']=[]
own['List2']=[]
if sens.positive:
alpha=sens.hitObject.color
sens.hitObject.color=(sens.hitObject['ColorX']+.5,sens.hitObject['ColorY']+.5,sens.hitObject['ColorZ']+.5, alpha[3])
if sens.hitObject not in own['List2']:
own['List2']=own['List2']+[sens.hitObject]
own['List']=own['List']+[60]
else:
iter=0
for items in own['List2']:
if items==sens.hitObject:
own['List'][iter]=60
iter+=1
length=0
for items in own['List']:
length+=1
if length > 0:
L=0
for item in own['List']:
Time=own['List'][L]
if Time>1:
own['List'][L]-=1
else:
target=own['List2'][L]
target.color=(target['ColorX'],target['ColorY'],target['ColorZ'],target['Alpha'])
own['List2'].remove(target)
own['List'].remove(own['List'][L])
L+=1
##print(str(own['List']))
main()
.remove(target) removes the first instance of target. Since one of your lists is just a set of integers, it’s probably going to remove the wrong one at some point.
Try having just one list, and storing the timer and the object reference in a nested list, like so:
[[60, obj], [56, obj2],…]
Then when you iterate through the list, you test the timer like this:
x = 0
while x < len(list_o_stuffs):
list_o_stuffs[x][0] -= 1
if list_o_stuffs[x][0] < 0:
list_o_stuffs.remove(list_o_stuffs[x])
else:
list_o_stuffs[x][1].changeColourAndStuff()
x += 1
Having two different lists is begging to have them fall out of sync, particularly when your list of timers is going to be full of duplicates. Store the timer and the game object together in one list.
While you’re only decreasing the numbers it -probably- won’t fall out of sync. In a few months time, you might update that script where you increment some of the timers for whatever reason. Then, it might only fall out of sync once every blue moon, but by that time you’ll have absolutely no idea what could be causing it. It’ll be so rare that you’ll have trouble even replicating the issue.
Do it properly the first time and you won’t be haunted later on. This advice comes from having maintained a commercial project for over 7 years (and still going!).