Site hosted by Angelfire.com: Build your free website today!

You SHould get the gameBlender Python class reference from JAN!!! (The guy working on Blender python). NOTE: Some of my python descriptions may be a little old. Blender is up to 2.12 now! But the principles still apply.

Got get that HERE for now. It REALLY comes in handy!

Now that that's said, you will notice that there are only for now FIVE root classes: GameKeys, GameLogic, LogicBrick, Object,  Rasterizer. These are actually modules (classes) in python and are written inside of Blender.

GameKeys give you access to the keyboard constants.
GameLogic give you access to the current controller and ability to add active actuators.
LogicBrick is where all the action is - all of the actuator, sensor, and controller
classes inherit from it. 
Object gives you access to actual game objects.
Rasterizer give you access to the rendering screen area.

You should look through the GameLogic Class Hierarchy if you want to get a good idea of how the classes inherit from each other. It's really quite simple in gameBlender!

So let's just show one quick example. Normally, we hook up a sensor, be it a keyboard or collision or whatever, to run our python script when it triggers. So, nice. Now our script is running. Big deal. How do we get access now to see the object which called us? No problem! 
First off, we need to get access to the current controller which called us. To do this, we import a class from gameBlender called GameLogic, and we can call one of its methods, getCurrentController(). Now, since this is a python module, and not a class we made ourselves, we do not need to make a variable first. We can simply call the function and be okay here.  Anyway, getCurrentController does RETURN an object, a CONTROLLER object. And a controller object is inherited from LogicBrick..:) If we look through the Class reference, we will come upon the LogicBrick class. And wallah! LogicBrick has a function called getOwner()  which returns a python object which will be our actual game object (represented in python as the GameObject class). Guess what? We have our game object which called this script! SO, here's how the script looks:

import GameLogic

Control = GameLogic.getCurrentController()

MyObject = Control.getOwner()

ONE THING TO NOTICE here! You have to have the empty parenthesis "()" at the end of the function. For now, you do not pass any parameters to this function, but zero parameters you still have to use the parenthesis (this tells the interpreter that there are no parameters, basically). If you don't have these the script won't work. Another thing is: where did we get this variable called "Control" ? . Quite simple, really. In python, you do not have to explicitly declare a variable to be of a certain type before assigning to it the first time. Most variables are "objects", like I said before, and Python sees the object that is supposed to be returned from the getCurrentController function and makes your variable the appropriate object  type. If you try to assign a different object to this now, though, like shoving a string object into a controller object variable, Python will let you know you can't DO THAT! :)

That's all there is to that! Simple, no? OK now we have our game object. In fact, it is from the GameObject class in gameBlender. Check out all the functions the GameObject class has! You can get your object's position, it's velocity, its mass, and even suspend or restore its ability to be dynamic. SO, let's say we want to gets its position. We simply say:

MyPosition = MyObject.getPosition()

MyPosition is a variable by the way, and in this case it will now contain a list (kinda like an array) of the X, Y, and Z position of your game object.  If you look at the gameBlender python docs, under the GameObject class, in the getPosition function, it states "Returns a python list of three elements (x,y,z). To get to the X position, you simply have to state:

XPosition = MyPosition[0]

Lists start at index 0 in python. 0=X, 1=Y, 2=Z in the gameBlender position list.

Oh yeah, and notice how the GameObject class is inherited from the Object class? That means it gets all of Object's functions, too. And those functions are getOrientation() and setOrientation(). These functions allow you to get or set the rotation of your game object! 

Remember, if you want to know all of the functions available to you in a gameBlender class, just keep looking up to its parent (the class it inherits from). Your class will inherit all the functions the parent class has all the way up to the base class. In other words, GameObject inherits from Object and it stops there. BUT for example TouchSensor inherits from Sensor, which also inherits from LogicBrick. So the TouchSensor will contain its own functions, Sensor's functions, and LogicBrick's functions. LogicBrick is the base class and so it stops there.

SO, this is really all there is to the python in gameBlender as far as access to your game elements. Now if you know how to write expressions in python, you can do cool calculations on your objects data!

BAck to first page.