Learning python scripting for GE

Recently I got into the blender scripting. I’m not great, but getting there. I’m curious about the scripting for the GE. It’s doesn’t seem to be the same, and it has to be tested in-game.

Is there some way to code while the game is running…and is there some way of learning how to code in the GE? I’ve grown quite fond of the “Report”, which copies any action you do in blender…but I presume that is not possible for the GE. So how does one actually improve one self if there’s no way to see how it’s done…except in other people’s code? Also, is the GE scripting hard (or harder than normal scripting in Blender) to learn?

Any tuts or advice would be helpfull and appreciated!

The BGE uses Python, which is the same as for Blender. The difference is:

  • the API
  • how it is setup
  • where it belongs into the game loop

You configure a Python controller to run your code. Everytime the controller is triggered the script is executed.
The code is part of the GameLoop and should return as fast as possible. There is no parallel processing!

There are two modes:
Script mode:
Param: <script filename>

The Python file is executed at indentation level 0 from top to button (similar to a python program). The code dies after execution with all defined variables and functions.

Module mode:
Param: <modulename>.<functionname> [no “.py” at modulename; no “()” at function name]

The Python function is executed as a usuall Python function call. The one parameter contains a refernce to the current controller.
Before the first execution, the initialization part of the module (indentation 0) will be executed. Exactly once!
The module lives as long as the BGE runs.

This is similar to:


import GameLogic
import &lt;modulename&gt;
&lt;modulename&gt;.&lt;functionname&gt;(GameLogic.getCurrentController())

of a script in script mode.

BTW: Module mode allows realtime code edit by placing the file external and enabling the little “D” at the module params. See the YoFrankie demo.

ALL triggered controllers (regardless of its type and positive or negative pulse) are executed after ALL the sensors are evaluated and before ALL active actuators are executed.

Refer to other scripts/modules as examples

Keep in mind Blender 2.5x uses Python 3 and does not support deprecated API calls.

I hope it helps

If you want to start trying things scripting in BGE I recomend looking at the following links:
http://www.tutorialsforblender3d.com/GameFunctions/ClassIndex_1.html
This is most of the classes in Blender game engine. If you can see it here, you can do it (they all have example scripts).
The classes include things like the game object class or the mesh class which allow you to do things with those objects.
Here is the game object class:
http://www.tutorialsforblender3d.com/GameModule/ClassKX_GameObject.html
It gives you a lot of control over what a game object can be made to do.

There is also an alphabetical idex which covers almsot every action you can do with the blender game engine code, with examples:
http://www.tutorialsforblender3d.com/GameFunctions/GameLogicPage1.html
Just look through it until you find what you need. If it isn’t there, you probably can’t do it.

The python scripting is one of the reasons I stick with BGE because it is so nicely documented.
Once you understand the basics of scripting, just look for the right class, test it and then implement it in your project. Simple as ABC.

You can use some of blender’s scripts in the 2.49 BGE engine, but not if you export it as a runtime. Not sure about 2.5…

Thanks for the reply guys! Been very helpful so far…even thought I’ve only had time to do a little bit of coding. I’m just curious… when coding for the GE, can you use the normal python(bpy) with that…or is it ONLY the GE modules.
Also…can the script (or so it looks to me) ONLY affect the object to which you apply the script…eg. you apply the script to the camera…you cant call another object(eg. cube) and affect that object?

Thanks in advance…questions still running in my head, but just trying to understand the basics for now. :slight_smile: