About External Modules in the BGE

Hey. As some of you may know, I have been making modules for use in your BGE projects. I intended to make them modular, so that they are easy to use and implement into your own projects (as opposed to my earlier projects, where they were just scripts in a blend file). In this fashion, I’ve found a nice, easy way to store your information for use across your BGE projects.

The reason my modules (or any external module) work is because the BGE searches for scripts on a certain path. For Windows, one of these places is Blender’s executable folder (e.g. C:\Program Files\Blender Foundation\Blender). If you put external module files there, then you can easily use them across any of your BGE projects.

Note that this won’t work out of the box with Mac (I haven’t tried Linux, and I only tried a Release Candidate for Mac). This is because the path of the local Python distribution doesn’t search for scripts in the Blend folder when I checked. To rectify this, ensure that you add the Mac Blender directory to the sys.path variable (it’s just an array in the sys module - append the directory to that path). Alternatively, you could store the data anywhere, and then append that directory to the path. Anyway, that’s an easy way to have a nice little archive of BGE helper modules.

If the module is in the Blender folder, for example, then

import Sprites
import Waves

should work across your projects.

Would it even work for a finished runtime game that people download that don’t have the script? Because the script won’t be saved when you do Pack Data so it would be a bit problematic…
Luke

It will work indeed - all you have to do is include the script (or the compiled version, if you don’t want to have it be open) in the same directory (or relative directory) as you did in the original blend file. Let me explain:

On Windows, one entry in the sys.path array points to the Blender executable directory - you can easily put the files in the same directory, or add an entry pointing to a specific folder where you store external modules. Another entry in the sys.path array points to the blend file’s position, which means that when you compile your game to a runtime, simply putting the script files in the same directory as the executable (or any directory in the path) will work.

EDIT: You can even expand the path with the logic module’s expandPath function to add a relative directory - for example, you may like putting your scripts relative to your blend file to Resources/Python. Use expandPath(’//Resources/Python/’) to point to the Python directory as where to find the scripts.

When I try to import XEmitter or Particles, I get the following error Message:

Traceback (most recent call last):
File “<blender_console>”, line 1, in <module>
File “C:\PROGRA~2\BLENDE~1\BLENDE~1.56A\2.56\scripts\modules\XEmitter.py”, line 139, in <module>
from bge import logic
ImportError: No module named bge

I’m a total noob at this and have no experience at coding at all, so please bear with me…

It looks like ‘mo dules’ is where you put the files? Try a couple of different things:

Put the X-Emitter or Particles scripts directly in the Blender directory - it should work fine. After that, try putting it elsewhere and adding the directory to the sys.path variable (it’s a string). Make sure it looks basically the same as the rest of the strings in the path variable.

EDIT: I’m not sure about why ‘bge’ wouldn’t work when you try to import the module… Anyway, try putting the scripts in the Blender directory, and see if that works.

Scripts specific for your project:

Script folder management

No hidden scripts, no paths in python controllers (except the init one).
At least that is what I do.
Monster

That’s basically the idea, yeah. Whatever will get your scripts folder accessible where you need it to be.