need help understanding this code

Hey guys, I’ve started my trek into learning more advanced stuff like menus, and exceptions, but I need help. I’ll post a few codes and explain what I don’t understand.

# This handles the gates

import GameLogic

me = GameLogic.getCurrentController().getOwner()
cont = GameLogic.getCurrentController()

init     = cont.getSensor("Init")
nr       = cont.getSensor("Nr")
through  = cont.getSensor("Through")
gate     = cont.getActuator("GateMess")

# the (random number) of gates are registering themselves into a global variable
# be shure to name the gates Gate.000...Gate.XXX in the correct order!
if init.isPositive():
    # set the visible gate name
    nr.getOwner().Text = me.name[2:]
    try:
        GameLogic.gates[me.name]=me
    except:
        # first gate?
        GameLogic.gates={}
        GameLogic.gates[me.name]=me
    

# ship has passed the gate, send message to the ship    
if through.isPositive():
    gate.setBody(me.name)
    GameLogic.addActiveActuator(gate,1)

This was used in subracer, the game on the BGE kit. the part I’m not getting is this…

if init.isPositive():
    # set the visible gate name
    nr.getOwner().Text = me.name[2:]
    try:
        GameLogic.gates[me.name]=me
    except:
        # first gate?
        GameLogic.gates={}
        GameLogic.gates[me.name]=me

I do understand that nr.getOwner().Text=me.name[2:]

it’s basically setting the gate name without OB in it, but after that it just losses me. I’ve barely been about to find resources on try: and except: so I dont’ know very much on how it works…

GameLogic.gates[me.name]=me this completely makes me lost in what it does, and there is not anything in the scene, being object, material, properties or logic bricks with the name ‘gates’…

GameLogic.gates={} What are the {} brackets being used for or are in reference to what? I want to assume to clear GameLogic.gates, but I have no clue…

I do understand that nr.getOwner().Text=me.name[2:]

it’s basically setting the gate name without OB in it, but after that it just losses me. I’ve barely been about to find resources on try: and except: so I dont’ know very much on how it works…

GameLogic.gates[me.name]=me this completely makes me lost in what it does, and there is not anything in the scene, being object, material, properties or logic bricks with the name ‘gates’…

GameLogic.gates={} What are the {} brackets being used for or are in reference to what? I want to assume to clear GameLogic.gates, but I have no clue…

I will post more later.

Here’s is another code as well.

ITEM_PREFIX = 'OBitem_'
    
def menu_items(sce):
    '''
    Return all names starting with item_ - in a sorted list
    '''
    ls = [ob for ob in sce.getObjectList() if ob.getName().startswith(ITEM_PREFIX)]
    ls.sort(lambda a,b: cmp(a.getName(), b.getName())) # py 2.3
    return ls

Ok, I get that it returns a list of objects back, but whoa does it just blow me away…

I get that ls = [] is a list, but a few things… It starts of with ob, but ob isn’t defined anywhere in the code…

so… for ob in sce.getObjectList() which gets all the objects and

if ob.getName().startswith(ITEM_PREFIX)] Now for one I still don’t know what ‘ob’ is supposed to be defined as but I do understand that it only gets the objects with item_prefix in it.

Alright now to the sort

ls.sort(lambda a,b: cmp(a.getName(), b.getName())) # py 2.3

ok… it’s sorting it I know, but another big whoa wtf pops up for me.
I understand lambda is a certain way to oder them? I’m not to sure, but lets just say this whole line really does stump me.

So I help you guys could help me out with these, cause I’d hate to bump around in my python bible and take days and days of reading to find out exactly what this code does or perhaps have it explained to me. I’ll still search for the answers on my own as well, but I would accept any help if possible

Alright, I will take a look over it. Brain is on overload right now so I’m going to take a break.