List help,

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()




here is a working file *

sans the remove item from list…

Attachments

LogicHighlightList.blend (461 KB)

This is how I’d do it:


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.

Thanks Dave, I ended up needing the first version :smiley:

I will add a alpha transparency version as well, so people can use ray + this (instead of mouse over)

to make it so you can always see your player, even through walls without any clipping plane nonsense.

Attachments

LogicHighlightListWorking.blend (624 KB)


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

like

own[‘List’].remove(own[‘List’][L])

:smiley:

I did, that for one list, and

own[‘List2’].remove(target)
is referencing a game object :smiley:

List = List of timers
List2= List of game objects

You’ve missed my point entirely.

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.

it works pretty well,

I have a filter so the item only gets added one time,

I also remove the item from the list after 60 frames,

I have not been able to break it yet…

Did you check file?

you can debug and print the lists…

I add a item to two lists, or reset the value of the timer,

or I remove both the timer and the gameobject.

Add 2

or

Reset value of 1

or

Remove 2

so the list is always in sync (I think)

I think they call this approach a dictionary?

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!).

is there any way to check to see if both lists are correct before proceeding?

if target not on own[‘List2’]?

Not really.

If you’re that attached to having a list of only objects then why not attach the timer to the object itself?
own[‘List2’][L][‘timer’]

that is not a bad idea, :smiley:

I think this is almost as fast as it could be, as it does not run python if the list is empty, and there is no ray target.

this is to be used in isometric games in roof tiles, or in walls in 3d games so you can see the player in 3rd person in

obscured areas.

.01ms logic when the list returns to “Empty” :smiley:

Attachments

AlphaLogicWithRecast.blend (495 KB)