I learned python by working in the bge, and asking questions here,
I don’t do well with tutorials,
I learned python by working in the bge, and asking questions here,
I don’t do well with tutorials,
How about assigning a score to each light hook?
I’m assuming you can already get the list of light hooks and lights, so I won’t add the code for that, but to sort based on score try:
import bge
def light_manager(cont):
own = cont.owner
scene = own.scene
### you already got the list of light hooks somehow
light_hooks = own['light_hooks']
light_list = own['light_list']
### we are going to append to a scored list
score_list = []
### this is the player's front axis, usually the "Y" axis
front_vect = own.getAxisVect([0.0,1.0,0.0])
### we need a key function for sorting the hooks, unless you want to use a lambda
def get_key(item):
### item is (light_hook,score) so we need the second index, or [1]
return item[1]
### let's get a score for each light_hook
for light_hook in light_hooks:
score = 20000
distance, global_vect, local_vect = own.getVectTo(light_hook)
angle = front_vect.angle(global_vect)
### with close lights distance is more important than angle
### you could get the camera angle if you want to be acurate, else lets just say around 90 degrees
### there's lots you could do here to make the score calculation more complicated
if distance > 12.0 and angle > 0.78:
score -= distance * 2.0
else:
score -= distance
### you could use a dictionary, or a class here, but a list or tuple is fine as we know
### the score will be the second object on the list
score_list.append([light_hook,score])
### now we sort the list, using get_key() as the sorting key
### you don't have to write get_key(item), as the item is automatically the thing passed to get_key
sorted_hooks= sorted(score_list, key=get_key)
### you now have a list sorted by score
### if you want only the hooks, not the score (it's not important now) you can rebuild the list
sorted_hooks = [ob[0] for ob in sorted_hooks]
### if you wanted a list the same length as your light list you could slice it by using a slice [:] = [start:end]
### we want the list to end at the same point as the light list so we specify the end point [:len(light_list)]
### but the start point stays at 0, so we don't need to touch that, i.e. not = [0:len(light_list)]
sorted_hooks = sorted_hooks[:len(light_list)]
And here’s a really nice python sorting tutorial:
The >>> thing threw me at first too, as I’m used to writing a script and then having it run instead of entering commands line by line.
Thank you smoking mirror, I will try and get it running in game tonight,