need some help understand this.

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…

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

You might find it useful to spend a few days bumping around in your python bible since the questions you ask are pretty basic.

I’m not trying to put you down or anything but if you don’t know about for loops and lists/containers there isn’t a whole lot you can do with python.

Maybe do a tutorial or two then come back to trying to figure out more complex scripts and exception handling.

Really? O.O

It’s more like I haven’t experienced these codes rather then I don’t understand them initially. I figured out how to use exception handling and the thing about the list is that I never knew it had comprehensions like that, but I learned about them. Now the only thing left really for me to read about is what I do believe that {} is a way to set up dictionaries, but I’m still getting into them, I have about a chapter or two till it gets to them in any kind of detail, and also lambda, which makes no sense to me yet.

It’s not that I can’t understand these things, I’m pretty quick to catch on too these things. Hell I’ve been dabbling around programming for the longest time, but now I’m deciding to sit down and fully learn python, so I’m getting into new ground, and it doesn’t take long for me to learn something. It’s just that I experienced this code in the blendergame kit and in no way did it try to explain the least bit of what I’m needing to be explained.

I can learn from books no doubt, but having it explained to me confirms the usage for me, knowing that I’m using it properly helps me learn how it’s used.

And the other big spiel about it is that, the book I have is about 500 pages long with up to 50-60 lines… 500- 850 words per page. It does good in explaining everything, but its as if I have to read every word in order to understand it, and the the things I posted here, are listed on pages 125 and 175. Of course I want to learn the complete language, but I’m also trying to keep up with my goals, so why not have it explained to me so I don’t miss out on details?

But for now I haven’t touched the BGE nor it’s scripts because I’m trying to do something that’s relative to menu selecting items. I know it’s going to be complex, but the quick way seems so much more appetizing, but for right now my goals are on hold until I learn these things.

Thanks for the insightful comment though, but I beg to differ that this is more then just basic, because to you, it may seem simple, but to me, I know all the basic stuff and lambda is not basic because it’s used with basic stuff. If you add a whole bunch of basic stuff together you get something clearly complex, everything is simple if you break it down, but to me, what does lambda mean and do? But I’ll find out though.

Python docs are amazingly good.

Dude, my python bible is amazingly good. I understand everything now. Basically GameLogic.gates is a global varibal which holds all the gates name, then it creates a dictionary to hold all the gate names

GameLogic.gates = {} creates this effect, now all the gates are using this script, so when I did a print test, it printed all the gates in order
GameLogic.gates[me.name] = me registers all the gates into the dictionary

See, there you go.

Lot more satisfying to figure it out on your own, eh?

Yeah I guess so, it’s just makes me believe that I will get far into programming. Programming has always been my number one thing to go and study for my major, but haven’t yet started on it till now (well not in college just yet, still gotta get my student aid worked out).

And I also figured out the second script as well, no biggy, bout to right my own menu code is one moment.