TUTORIAL: Beginning BGE Python

The tutorial is hosted here: http://socialstorage.googlepages.com/beginningbgepy

The reason I had to host it off-forum was because of the 10,000 character limit that’s in effect. I could have broken it off into two posts, but it just didn’t feel right to do so, and then there is the rush to do it before someone posts “Why does it just cut off, etc”. So I decided to go the “link to tut” route instead.

As the name implies this is a tutorial on Python, and how to use it with the BGE. It’s a little longer than my previous tutorials, but that’s to be expected with something of this nature. It’s not a waste read for anyone who has yet to start some BGE-PY scripting. (At least that’s how I see it)

As always all C&C is welcome, and I look forward to your opinions on the matter.

I hope this tutorial can help someone out.

Social.

Thanks alot for sharing Social:D
Just begun Python last night. Lol. You would know since you helped me out anyway. And enjoying Python quiete well:)

Cheers

Good job Social. Nicely written. The only thing I would add would be some links for further reading in python. Perhaps
Dive Into Python
and
GameLogic

Hope I didn’t miss them, I read through it pretty fast.
Also, one other stickler for me is recommending using “c” for controller. It’s so shortened it’s lost all meaning, and one letter variables are just plain dangerous except in a short block where it’s obvious their scope won’t go any further. A one letter variable is usually a clue that the scope doesn’t go beyond the block.

This is very helpfull, thanks Social.

Hey Social,

I’m currently working on a blender site. Would you mind if I incorporate your tutorial into it?

Hey! Good idea. Do you think I could put it on my sitetoo??? I’ll give you full credit!

If it looks good, give it a run. If it works and you feel like you understand most of it, you have successfully completed this tutorial. If not, you didn’t.

I just love the ending

Cool!

I am gonna try it too, thanks :slight_smile:

After trying your tutorial, these are the the things I’d add or change:

  1. mention the syntax highlighting and line count buttons, since seeing the syntax highlighted makes it easier to learn (for me)

  2. “What we assign to “cont” is a function called “getCurrentController()” which returns the current controller (as the name implies) and gives us access to it, and various other functions.” Perhaps you could define what you mean by “Returns the current controller”. I’ve seen this in a lot of Py tuts, and still don’t know what it means.

  3. stress that python is case sensitive

  4. throughout the tutorial, we’re using many variables, but you never seem to actually call them that, and identify them as variables. It’d make it easier, especially when you get to “forward=cont.getSensor(“Up”)”. I spent some time trying to figure out what “forward” was referencing, before I figured out you were defining a variable.

  5. It’d be nice to, after a while, come to a point that says “Your code should now look like this:” with the code then listed.

  6. there’s a problem with the code (but perhaps me, though I believe I proofed it…), the right arrow key applies the left arrow key’s rotation, and the backarrow applies the up arrow’s movement.

  7. Further description of why each step was taken.

I just want to thank you for taking the time to write this Social, it has really helped me out a lot.

P.S. Simple fix to you code: add variable “nspeed=-5” and “nrspeed=-0.02”, then, in the if statements, change
“if backward.isPositive():
walk = -speed”
to
“if backward.isPositive():
walk = -nspeed”
and
“if turnright.isPositive():
turn = -rspeed”
to
“if turnright.isPositive():
turn = -nrspeed”

fireside>

Thanks,

In the “prerequisites” section there are links to all the relevant documentation. Only instead of “Dive into python” I choose my favorite “A byte of python”

The link to the GameLogic docs is at the point where I explain what “print dir(GameLogic)” does. I have it listed as “Link”, so I don’t know if that makes it harder to find on the page, but it is there.

Also, one other stickler for me is recommending using “c” for controller. It’s so shortened it’s lost all meaning, and one letter variables are just plain dangerous except in a short block where it’s obvious their scope won’t go any further. A one letter variable is usually a clue that the scope doesn’t go beyond the block.
Well, that’s a matter of personal preference, and coding style. However it really doesn’t matter in the end, because both will work equally as well, and users usually find their own style soon after the initial introduction.

Dim>

  • I intentionally left that out, because I believe that starting programmers should not become so desperately dependent on “colored words” in order to write working code. Syntax highlighting is an option, but I won’t endorse it. (also, please keep in mind that this is not a blender interface tutorial. In order to get a full list of options available to you via the text editor, you should visit the wiki either on your own, or through the link that I have listed in the “prerequisites” section.)
  • In computer programming, subprograms (functions) will “return” to the higher-level programs, which called them; return ends the current task. You see Dim, this is one of the reasons that I made a “prerequisites” section, meaning that those three things I have in there have to be grasped by the reader at least on some basic level, in order for my tutorial to make sense. In other words, you’r looking for a general programming tutorial. This isn’t one.
  • You didn’t read it through. On the tutorial page do: [CTRL]+[F], then type “case sensitive” (no quotes). I refer to that fact at least twice.
  • Umm…you missed it again. Follow the same instructions I gave in 3, replace “case sensitive” with “variable” (no quotes). I refer to them as assignment variables many times throughout the tutorial.
  • Lol, wow you really rushed through it. Check at the bottom, it’s all there.
  • It’s you.
  • That would be redundant. I put a prerequisite list there for a reason.The original script on the tutorial page works fine. I checked it all before posting, and I checked it again now. You dropped the ball somewhere along the line it seems. From all the questions you posted, it sounds to me like you didn’t take the time to fully read through the entire thing.

Everyone>

Thanks for the compliments, and feel free to host this tutorial wherever you see fit.

cool! thanks.

this was a the best intro to python yet! I actually understand it :slight_smile:

Consider this tutorial “f00l pr00f” now :slight_smile:

Thanks again

Well, that’s a matter of personal preference, and coding style. However it really doesn’t matter in the end, because both will work equally as well, and users usually find their own style soon after the initial introduction.

Both will work equally well, but both won’t “read” equally well. The problem is it’s hacker’s crap code and your presenting a beginner’s tutorial, more or less reinforcing the situation. Then they have problems and post it and you have to get out your Captain Crunch secret decoder ring to figure out what they did wrong. At least you gave it as the last option.

Wow, thank you. It means alot to me that you would say that.

Heh, I guess I might have underestimated the impact of listing “c” as an option.

Thinking about this a bit more, I must say that I agree with you on the matter. Your points are all logical and valid. (just the way I like them) So I took the “c” out of the tutorial. “cont” is now the only suggested option.

Thanks alot fireside. Your intelligent input is a valued contribution. It’s exactly what I am looking for.

Glad to give input. I really appreciate what your doing, especially with this tutorial. I remember being afraid to code and having someone take a little time to explain a few things to me and really get me started on what I know is a lifetime hobby. I like games, but I really just do this to code and model. Two great hobbies that Blender works together perfectly. If I share a game or two, that’s just icing on the cake.

Response to your response:

I just want to make it known, that I do program, I was just presenting what I believe some people’s problems would be. Also keep in mind, this was not a critisism of your tutorial, just my obsevervations and suggestions.

  1. If that’s your preference, then keep it. I stated purely my preference for having highlighted text. I can write fine code with out it, but it is nice to know when you’ve misspelled “import” or what ever.

  2. My mistake.

  3. I’m well aware you refered to it, but what I said was that you didn’t stress it very well. It was mentioned several times in passing, but it never stood out to me as an important fact. You could put “Before we begin, remember that all python scripting is case sensitive” right under the heading “Your First Lines (of code):”

  4. Again, I know you referenced them as variables, but you never defined them as variables until about 3/4 of the way through the tutorial at first. It would be nice to know they’re variables as we first begin to type them.

  5. And again, I know you did do it, but only at the bottem. It’d be nice to have a point about in the middle where you could review all the code you should have written. You tell the reader to put in lines of code that are later erased, it’d be fairly simply to trip over these, especially if you don’t know what you’re doing.

  6. I reviewed the code four times to find out what I did, and it was terribly simple, but those damn minus signs are easy to miss. :slight_smile: My mistake. My solution to that was a bit too long winded for the problem.

  7. It would be a bit redundant, but a) redundancy is the best way to learn, and b) though going through much of the reference material, because it doesn’t refer to your script precisely, the reader has to draw a lot of their own conclusions which could easily be heironeous.

It seams that I didn’t articulate myself well enough the first time I wrote this, and for that I apologize.

Also I just want to make a couple things clear (again). First, this is not meant to be critical of your tutorial in any way, they’re just suggestions, and second, I just want to thank you very sincerely for taking the time to write this, it has helped me to understand pythin much better.

OK here are my C&Cs the only thing I can say don’t stop and I’m not sure if it was in the tutorial but a comment as to which version of blender the tutorial was written for and the date so that when version 6.0 comes out it will be easier to keep the confusion down. The only reason I say this is that I spent a lot of time on very old tutorials that did not work and yes I understand that this will happen when a great program is always moving forward.

Excellent input,

Yes, establishing the “last updated” date and the blender version # with which the tutorial is guaranteed to work, is very important (I can’t believe I forgot about it myself). I made the appropriate modifications to the tutorial. The information that you noted is now presented in the title sub-heading.

Thanks on your input matro. It’s appreciated.

Dim>

I appreciate your opinions, I hope I didn’t make it seem otherwise. Please don’t hold back, and continue to speak your mind. Criticism doesn’t bother me, it is welcome, as long as it is within the limits of logic of course. Also I am glad that you think this is helping the community. Thank you.

Now, as for the still unresolved issues:

  • (3) I know I’m starting to sound like a broken record at this point, but I must state it again: The “prerequisites” section already covers that. More specifically it’s the 3rd prerequisite link which leads to “A Byte of Python”. The fact that Python is case sensitive is clearly presented there, among other general python facts.
  • (4) ?? I’m a bit confused as to what you mean here. I define them as assignment variables right after “cont” is introduced. I mean I “could” list it before presenting any code, but isn’t that kinda pointless? O, and once again: The general concept of variables is presented in “A Byte of Python”, which is a prerequisite link.
  • (5) ?? No disrespect, but that would just needlesly clutter things. The code is presented in its entirety at the end of the tutorial. This way if someone previously “tripped” over some code, they can catch it there. Listing the entire script more than once would be utterly useless.
  • (7) b) The “prerequisite” material was never written to refer to the tutorial script. It is there to inform the reader of what “general” knowledge is required prior to reading the tutorial. (like the general python stuff I talked about earlier. It’s for things like that, so that I don’t have to teach general python with the BGE PY API) a) In many cases redundacy is the best way to learn (in some, it just becomes annoying), but really what else is left for me to say? I presented functions, I explained what they do and how they are used, along with how they all connect up in the blender environment. General programming terminology and python facts are covered by the prerequisite links. Is there anything BGE PY API specific that you have in mind?

Thank you for taking the tim eto reveiw my comments and respond appropiately. I still belive in what I wrote, but trying to convince you otherwise wouldn’t help anyone out, an dI certainly don’t want to flame anything here. The tutorial you wrote is obviously your own words, so state them as you like, and in the manner that you believe would be best for you to help people to learn. Your tutorial helped me a lot, like I said, so I just wanted to say Bravo over all. It was well worth the read, and I think I probably learned more from your tutorial than any other python tutorial since it related directly to Blender, but didn’t introduce advanced concepts strait off the starting line. Thank you again.

I haven’t had much time to look at this, but I just took a look now, and it’s fantastic! I’ve been putting off learning python for a while now, mostly because it looked so confusing, but this tutorial really helped me out, and I’m beginning to understand how it works, although some ways the code works is still confusing to me.

Thanks Social very much, I really appreciate it. :slight_smile:

  	 		 		 		 		I haven't had much time to look at this, but I just took a look now, and it's fantastic! I've been putting off learning python for a while now, mostly because it looked so confusing, but this tutorial really helped me out, and I'm beginning to understand how it works, although some ways the code works is still confusing to me.

Yea! I feel the same way. I am finaly hacking python scripts into submission :slight_smile: