3
$\begingroup$

When a script is run via Script mode through the Python Controller there is no need to have import bge in the script.
Why does running a script via Module mode require importing bge?

Documentation on the blender wiki of the differences between script and module modes.

$\endgroup$
1

1 Answer 1

2
$\begingroup$

Why does running a script via Module mode require importing bge?

Because the import statement is the proper way of writing Python code.

The auto-import within script mode is a dirty shortcut. It is still there for backward compatibility. But it is no good design.

Pro

  • You always need it to access the the BGE context anyway. (In module mode you do not really need it as you can get access via function arguments.)
  • Bge is not a real Python module. It is a wrapper around the BGE native code.

Contra

  • It violates the Python convention to show where objects are coming from (defined by the code or imported from other modules)
  • It hides the fact that this module is imported at all. So a reader will never see an explicit assignment to bge.
  • The bge module should behave like regular Python modules, so it should be imported as any other Python module too.

I strongly suggest to add the import statement in script mode too and ignore the auto-import.

I hope it helps

$\endgroup$
2
  • $\begingroup$ ok, so why does bge get auto imported when run as a script, but not when run as a module? $\endgroup$
    – David
    Mar 27, 2015 at 17:16
  • $\begingroup$ I can't tell the reasons, because I do not know them. The auto-import was there as I met the BGE the first time (~2.37). The module was called GameLogic at that time and only script mode was available. $\endgroup$
    – Monster
    Apr 1, 2015 at 6:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .