Homade LOD and Light hook manager


import bge




def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner
    LODlist=[]
    len=-1
    
    
    ## get list of all objects with LOD
    if 'LODList' not in own:
        for objects in bge.logic.getCurrentScene().objects:
            if 'LOD' in objects:
                
                LODlist+=[objects]
                len+=1
                
        own['LODList']=LODlist
        own['Len']=len
        own['index']=0
        own['Lights']=[]
        
        own['LightNum']=-1
        for child in own.children:
            if 'Lights' in child:
                own['Lights']+=[child]
                own['LightNum']+=1
    else:
        
        
        s = own['index']
        x=0
        ##proccess 10 piece of the list per frame
        while (x<10):
            item = own['LODList'][own['index']]
            
            V= own.getVectTo(item)
            Distance = V[0]
            
           
            ##print(str(own['index'])+" Distance is "+str(Distance))
            ## set LOD if the unit is less then 100 units
            lights =[]
            if Distance<100:
                ## get angle offset from player axis if its a light
                if 'Light' in item:
                    Ang = V[1].angle(own.worldOrientation.col[1])
                    print(Ang)
                    ## store the angle and the distance and the light object in a list
                    lights += [[Ang, V[0], item]]
                if Distance > item['2']:
                    if item['LOD']!=3:
                        item.replaceMesh(item['Mesh3'],1,0)
                        item['LOD']=3
                if Distance > item['1'] and Distance <= item['2']:
                    if item['LOD']!=2:
                        item.replaceMesh(item['Mesh2'],1,0)
                        item['LOD']=2
                if Distance < item['1']:
                    if item['LOD']!=1:
                        item.replaceMesh(item['Mesh1'],1,0)
                        item['LOD']=1       
            else:
                if item['LOD']!=4:
                    item.replaceMesh('Empty',1,0)
                    item['LOD']=4            
            own['index']+=1
            if own['index']==s:
                x=25
            if own['index']>own['Len']:
                own['index']=0      
            x+=1
        
        if lights!=[]:
            ## this is where I need to sort the list somehow with distance and angle where
            ## the more lights are placed near the player, the less distance matters and the more
            ## angle matters (So there are lights near the player, and in the midground)
            ## and I need to do it in a manner where it is not too... twitchy....
            print(str(lights))
            for Hooks in lights:
                if Hooks[1]>10:
                    Hooks[1]=Hooks[1]*2
                if Hooks[0]<.1:
                    Hooks[1]*=.9
            lights = sorted(lights[2], key=lambada)
            print(str(lights))            
            
            print('Lights')
            index=0
            x2 = own['LightNum']
            print(x2)
            while x2>-1:
                print('Light is'+own['Lights'][x2].name)
                
                own['Lights'][x2].worldPosition=lights[index][2].worldPosition
                
                print(own['Lights'][x2].name+" was placed at "+lights[index][2].name+"'s world position")        
                x2-=1
                index +=1
                
            
                
                                


main()



is what I have, but it’s not right,
I don’t really understand all of the stuff in list.sorted(stuff)

I want the to sort the list based on the second value of each tuple.

lights[index][1] = Distance after manipulation by scoring.

lights = sorted(lights[2], key=lambada)

Where are sorted() function?

Have you mean?

lights=lights.sorted()

Yeah, I only have little snippets to look at online, and I just don’t get how to use it.

I need to sort the tuples by there second value…

No sorted is a function that accepts an iterable. It’s not the method of a list.

Can someone show me the proper syntax?

I don’t understand how to use this, and the example
snippets don’t make much sense to me.

I am one of those fools that learns from digesting others code…

(it’s worked well so far) :stuck_out_tongue:

updated File

still no sorting, just some stuff to reduce over snappy ness of light management

I am stuck unless I can get a little help here.

Attachments

LightLodWorkingButNotSorted3.blend (1.71 MB)

Explanation on python.org wiki

Proper syntax would be:


lights = 
[list,of,all,lights]
lights_sorted = sorted( lights, key=lambda light: light[1] )

This sorts your lights from lowest to highest, using each light’s [1] value as the key for sorting.

If you want to reverse the order, add ‘reverse=True’ as a third argument.

Thanks Nines!

Almost there, but it’s still not quite right,
going to have to debug.

o well tommarow.

Attachments

LightLodWorkingSortedButSomethingIsWrong.blend (1.71 MB)

Some notes:

  • Don’t compare a list against an empty list to check if it’s empty. Just use the boolean nature of the container, i.e:
if not container:
  • Don’t append to a list by adding another list! Just use the append function - much more efficient and sensible.
  • Whenever you use multiple-depth subscripts more than once, make a variable:
some_obj = own['Lights'][x2]

Ok, found a huge speed up compared to get vectTo and compare angle etc,

just localize the objects position to the camera! the the angle and distance are using thr same set of numbers!

magnitude of localized position vector =Distance

if Distance<10 or (if Vector.z<0 and Vector.x<(10-Vector.z) and Vector.x>(-10+Vector.z):

this will make a pie slice type swath of the screen as well a circle around the player, as
the -Z axis of the camera faces forward,

(I however am using a object as the center for the lod rather then the camera, so you can zoom in easily.