As more people start to play around with Blender 2.5x, I’ve seen a increase in questions which basically boil down to: how do I get my 2.49 BGE scripts to work in 2.5? After having ported a few scripts, I’ve seen that the process breaks down to these three steps:
- Replace deprecated methods
- Update to changes in 2.5 API
- Update to changes in Python 3.x
Replace deprecated methods
The first step is to make sure your script works in Blender 2.49 without any deprecation warnings. All methods deprecated in 2.49 have been removed in 2.5. Make sure Game -> Ignore Deprecation Warnings is not checked and then run the script/game. The deprecation warnings take the following form:
old_way is deprecated, please use new_way instead.
For example, if you use the deprecated getOwner() method of SCA_PythonController, you’ll get the following:
Method getOwner() is deprecated, please use the owner property instead.
In this case, replace the getOwner() method with the owner property. Thus this:
GameLogic.getCurrentController().getOwner()
becomes:
GameLogic.getCurrentController().owner
Another change in 2.49 was how custom game properties are stored on objects. Prior to 2.49 users would use attribute style access for these properties (my_ob.foo). However, in 2.49 this was deprecated in favor of dictionary style access for properties (my_ob[‘foo’]). By having the properties in a dictionary we avoid potential conflicts with the built-in attributes. For example, I can now use my_ob[‘position’] without having to worry about the built-in my_ob.position, or safely use my_ob[‘velocity’] knowing that if a my_ob.velocity were ever added, my property wouldn’t conflict. Another thing this changes is how a user checks to see if a game object has a certain property. Prior to 2.49, the following was used:
# Check to see if my_ob has the property 'foo'
if hasattr(my_ob, 'foo'):
bar()
Some might remember the use of has_key() to find properties in 2.49. However, since this was already deprecated in Python, it was quickly dropped in favor of using the “in” keyword like so:
# Check to see if my_ob has the property 'foo'
if 'foo' in my_ob:
bar()
After everything is working fine in 2.49 without any deprecation warnings, we are ready to move on to 2.5.
Update to changes in 2.5 API
While Blender’s API underwent a major script-breaking change, the goal for the BGE was to try really hard not to break old scripts. However, there is one change that does break scripts. Prior to 2.5 when referencing objects by name, you had to use the “OB” prefix. For example:
my_ob = GameLogic.getCurrentScene().objects['OBmy_ob']
While the prefix is still used internally it’s no longer used in the API. This makes things a little nicer, but it does break old scripts. Fortunately it’s easy to update; all you have to do is remove the prefix like so:
my_ob = GameLogic.getCurrentScene().objects['my_ob']
Another change in the 2.5 api is the introduction of aliases for all of the BGE modules. Both the old and the new names work in 2.5. However, GameLogic is no longer automatically imported; if you were relying on that, you’ll need to add “import GameLogic” to the top of your scripts. The aliases are as follows:
| 2.5 | 2.49 |
| bge | N/A |
| bge.logic | GameLogic |
| bge.render | Rasterizer |
| bge.events | GameKeys |
| bge.constraints | PhysicsConstraints |
| bge.types | GameTypes |
| bge.texture | VideoTexture |
Update to changes in Python 3.x
Blender 2.5 uses Python 3.1, which scripts need to be updated for. For a complete list of the changes, look here http://docs.python.org/py3k/whatsnew/3.0.html. I wont go over all of them, however, there are two changes that I see which most often affect BGE scripts. First off,ย print is a function instead of a statement. So instead of:
print "foo"
use:
print("foo")
Also, the division operator (/) now uses “real” division for two integers as opposed to integer division. So, / will always give you a float now. To use integer division use // instead.
BGE 2.5 Python API
Docs on the BGE’s Python API can be found here:
http://www.blender.org/documentation/250PythonDoc/
From the BGE you have access to the “Standalone Modules” and the “Game Engine Modules”.
And that should do the trick! See, not as bad as you thought, right?
Cheers,
Moguri

bge.types?
Fixed, thanks for pointing it out. ๐
Hi,
Thanks very much for your article. This makes me want to get to the GE in Blender 2.5. Also I did not know at all how much changed the API was, and the changes seem to be very little so that’s encouraging.
I’ve spent some time developing in Blender 2.4x whithout ever using GameTypes. I don’t think that beginners at the GE use them a lot. An article on that would be cool !
You should also add that instead of hasattr() or has_key() you need to use the in operator instead ๐
So instead of,
if hasattr(i, ‘prop’):
it also works if you do,
if ‘prop’ in i:
I don’t know but hasattr() doesn’t work the same way and has_key has been removed
This is part of the 2.49 changes. User created game properties changed from attribution style access (foo.bar) to dictionary style access (foo[‘bar’]). This is why hasattr() no longer works and also throws a deprecation warning in 2.49. has_key() was deprecated in Python, so we instead used the in keyword. Since this might be a source of confusion, I’ll add it to the post.
Is there a Blender 2.5 GameLogic api reference??? If so, can you post the link too
Added a link to the docs.
nice, just what a wanderer needs
hi how can i convert this :
import bge
controller = bge.logic.getCurrentController()
#main variables
owner = controller.owner
scene = bge.logic.getCurrentScene()
objects = scene.objects
#actuators
move = controller.actuators[“Motion”]
move2 = controller.actuators[“Motion2”]
track = controller.actuators[“Track”]
walk = controller.actuators[“WalkAct”]
idle = controller.actuators[“IdleAct”]
run = controller.actuators[“RunAct”]
#sensors
near = controller.sensors[“near”]
flag = objects[“flag”]
distance = owner.getDistanceTo(flag)
#user variables
rundist = 20
controller.activate(track)
print (distance)
if not near.positive:
flag[“visibility”] = True
if distance > rundist:
controller.activate(run)
controller.activate(move2)
controller.deactivate(move)
controller.deactivate(walk)
else:
controller.deactivate(move2)
controller.deactivate(run)
controller.activate(move)
controller.activate(walk)
if near.positive:
controller.deactivate(move)
controller.deactivate(walk)
controller.deactivate(run)
controller.activate(idle)
flag[“visibility”] = False
to blender 2.49 version?
thanks in advance,
Romy
If you want that working in 2.49, just convert this line:
flag = objects[“flag”]
to:
flag = objects[“OBflag”]
And I think it should work.
Great post!
My export script does nto show up in blender 2.6. where do i have to put it? I want to ocnvert an old 2.44 script to 2.63a
please help! ๐ Thanks a lot!
This guide is for BGE scripts not bpy/Blender scripts. Blender’s bpy API went through a lot more changes for 2.5 than the BGE API did.
Hey there! Someone in my Facebook group shared this website with us
so I came to give it a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this
to my followers! Great blog and excellent design and style.