so the idea here, is build 3 lists,
Lights,
Hooks,
Distances,
and run checks every so often, and shuffle the lights to the closest hooks,
a dictionary would work, as if one item is sorted so is the other?
import bge
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
lights = []
hooks=[]
Dlist=[]
scene=bge.logic.getCurrentScene()
if 'Lights' not in own:
own['Cam']=scene.objects['Camera']
for objects in scene.objects:
if 'Lights' in objects:
lights+=objects
if 'Hook' in objects:
hooks+=objects
Dlist+=objects.getDistanceTo(own['Cam']])
own['Lights']=lights
own['Hooks']=hooks
own['DList']=Dlist
else:
index=0
for hooks in own['Hooks']:
own['Dlist'][index]=hooks.getDistanceTo(own['Cam'])
index+=1
x=0
for light in own['Lights']:
x+=1
##sort lists here
index=0
for lights in own['LightList']:
lights.worldPosition=own['Hooks'][index].worldPosition
lights.worldOrientation=own['Hooks'][index].worldOrientation
lights.setParent(own['Hooks'][index])
index+=1
main()
Also a dictionary is a much better approach than separate lists.
Hooks = { hook:{“d”:0,“someotherfeature”:[]}}
And sort it the same way with a key function.
I created this approach for my script (I don’t use bge but the idea is the same), I used classes instead because I wanted to access fields in a more meaningful way, plus that I wanted to provide support to sort items in more than 10 different fields. So using a tuple was not good for me.
import bge
list_distance = []
list_sorted = []
class DistanceData:
def __init__(self, name, dist):
self.name = name
self.dist = dist
def print_distance_data_list(list):
c = 0
for i in list:
print("%i. %s: %.2f" % (c, i.name, i.dist))
c += 1
def main():
scene = bge.logic.getCurrentScene()
cube = scene.objects['Cube']
keyboard = bge.logic.keyboard
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
if keyboard.events[bge.events.ONEKEY] == JUST_ACTIVATED:
print("Building Distance List")
global list_distance
list_distance.clear()
for i in scene.objects:
if type(i) is bge.types.KX_LightObject:
list_distance.append(DistanceData(i.name, i.getDistanceTo(cube)))
print_distance_data_list(list_distance)
if keyboard.events[bge.events.TWOKEY] == JUST_ACTIVATED:
print("Sorting Based On Distance")
sort = sorted(list_distance, key=lambda x: x.dist)
print_distance_data_list(sort)
if keyboard.events[bge.events.THREEKEY] == JUST_ACTIVATED:
print("Sorting Based On Name")
sort = sorted(list_distance, key=lambda x: x.name)
print_distance_data_list(sort)
Notes: Instead of key presses you can use a time elapsed interval. Also instead of just printing the sorted list you can store it for caching.
And yes more objects = more candidates = more to do.
With some clever assumptions a lot of calculations can be avoided. But I do not know how it is implemented. I know that the near sensor is heavy and it will not detect lights.
Geo is making a kit to build a large city, and objects at far LOD will not have light hooks,
they will use emissive textures, however closer will have light hooks.
another thing, I have to somehow change what mesh is used during the night , so lights are off during the day…
the city will be powered down initially, but this is still a major concern.
well , others have written already all replies,
yes , sorted() , is decisely “THE TOOL” (is useful also in other 1000 cases)
what lack in this TD , is the mention of >>scene.lights<< !!!
maybe not solve all problems, but a good amount!
since you had a little list to iterate , not huge and wasting!
so , a lot of code or setup is avoidable thanks to it!
Download/look at python from https://www.python.org/
That is the regular python.
It has an interactive shell, which is like a command line where you can run python line by line.
It is only decoration, saying this is the line the user wrote.
Like, haven’t you looked up any python video tutorials before?
yes, the syntax is a bit strange,
it is a function that run internally a loop and want a function to iterate the items.
anyway if you try to make the same work with normal code , you see that is better “memorize” how work sorted() :yes:
supposing you want know the nearest enemy:
player = cont.owner
enemies = own[“enemies”]
enemies_sorted = sorted(enemies, key=player.getDistanceTo)
for performances reason better this (EDIT: thinking better probably is the same!)
using scene.lights basically you not need to the hooks
you can use lights inactive as hooks, then the lights real can replace some attribute.(not all)
he concept is the same, seem just more flexible