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:

  1. Replace deprecated methods
  2. Update to changes in 2.5 API
  3. 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

Advertisements