Unlimited amount of lamps in scene

Dynamic lighting manager

if camera bounds sensor detects a lamp hook, and the lamp hooks is not the same as the last hook detected,
add a lamp and parent it to the hook, save as last lamp

iterate through all lamp hooks (a stored list) if any are using the lamp you just added, delete that lamp

limitations = don’t use the same lamp in a area less then the draw distance apart.

So almost unlimited lights!

any way to improve this?

import bge



def main():


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


    sens = cont.sensors['Collision']
    
    
    ## build a list of all lamp hooks in level
    if 'LampHooks' not in own:
        LH=[]
        for obj in bge.logic.getCurrentScene().objects:
            if 'LightHook' in obj:
                print('found')
                LH.append(obj)
        own['LampHooks']=LH    
        
        
    else:
        
        ##if your camera sensor collides with a light hook
        
        if sens.positive:
            
            ##if the light hook was not the same as the last light hook
            if sens.hitObject.name!=own['Last']:
                if sens.hitObject['On']==False:
                
                ## add the lamp, position and parent it
                    added = bge.logic.getCurrentScene().addObject(sens.hitObje  ct['LightHook'],sens.hitObject,0)
                    Pos = eval(sens.hitObject['AddPoint'])
                    added.worldPosition=Pos
                    added.setParent(sens.hitObject,0,1)
                    ## set last
                    own['Last'] = sens.hitObject.name
                    sens.hitObject['On']=True
                    
               
                for objects in own['LampHooks']:
                    ## remove any duplicates of the lamp
                   
                   
                   
                    if objects.children:
                        
                        if 'Lamp' in objects.children[0]:
                            
                            if objects.children[0]['Lamp']==added['Lamp']:
                                
                                if objects.children[0]!=added:
                                    
                                    objects.children[0].endObject()
                                    objects['On']=False
                                    
                ##Debug to make sure there is never more then 2 copies of a lamp
                ## one on the hidden layer, and 1 on a hook.
                                    
                ##count=0                                     
                ##for objects in bge.logic.getCurrentScene().objects:
                ##    if 'Lamp' in objects:
                ##        count+=1
                ## own['Count']=count ##Debug property
            


main()



Attachments

WrectifiedCandyLightHooks.blend (657 KB)

Edit - Rewrote for situations where you were viewing more then 1 hook at 1 time


import bge




def main():


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


    sens = cont.sensors['Collision']
    
    
    ## build a list of all lamp hooks in level
    if 'LampHooks' not in own:
        LH=[]
        for obj in bge.logic.getCurrentScene().objects:
            if 'LightHook' in obj:
                print('found')
                LH.append(obj)
        own['LampHooks']=LH    
        
        
    else:
        
        ##if your camera sensor collides with a light hook
        added='X'
        if type(own['Last']) is str:
            LastList=[]
        else:    
            LastList = own['Last']
        own['Last']=[]
        if sens.positive:
            
            for hitOb in sens.hitObjectList:
            ##if the light hook was not the same as the last light hook
                if hitOb not in LastList:
                    
                    if hitOb['On']==False:
                    
                    ## add the lamp, position and parent it
                        added = bge.logic.getCurrentScene().addObject(hitOb['LightHook'],hitOb,0)
                        Pos = eval(hitOb['AddPoint'])
                        added.worldPosition=Pos
                        added.setParent(hitOb,0,1)
                        ## set last
                        own['Last'].append(hitOb)
                        hitOb['On']=True
                  
                    if added!='X':
                        for objects in own['LampHooks']:
                            ## remove any duplicates of the lamp
                       
                       
                       
                            if objects.children:
                                
                                if 'Lamp' in objects.children[0]:
                                    
                                    if objects.children[0]['Lamp']==added['Lamp']:
                                        
                                        if objects not in own['Last']:
                                            
                                            objects.children[0].endObject()
                                            objects['On']=False
                                    
                ##Debug to make sure there is never more then 2 copies of a lamp
                ## one on the hidden layer, and 1 on a hook.
                                    
                count=0                                     
                for objects in bge.logic.getCurrentScene().objects:
                    if 'Lamp' in objects:
                        count+=1
                own['NumberOfPlacedLamps']=count ##Debug property
            


main()



Attachments

WrectifiedCandyLightHooks2.blend (691 KB)

going to take a look at having a list of used and unused lamps, and go about it that way, so you dont have to worry about what lamp is spawned where

and setting up grabbing the lamp values out of the lamp hook.

we really should have a dynamic light manager that does the same thing with compiled code?

Nice, I will take a deep look and study this after I’m done playing with the new 2.75 mist

is very good!