BGE alternative run mode for python controllers.

Option to run a function in a module rather then a script from a python controller, this has a number of advantages.

- No allocating and freeing the namespace dictionary for every time its triggered
  (hard to measure the overhead here, but in a test with calling 42240 scripts a second each defining 200 vars, using modules was ~25% faster)

- Ability to use external python scripts for game logic.

- Convenient debug option that lets you edit scripts while the game engine runs.
This commit is contained in:
2009-04-29 12:43:09 +00:00
parent 988fbb88dc
commit f8656d3510
5 changed files with 193 additions and 76 deletions

View File

@@ -42,23 +42,35 @@ class SCA_IObject;
class SCA_PythonController : public SCA_IController
{
Py_Header;
struct _object * m_bytecode;
struct _object * m_bytecode; /* SCA_PYEXEC_SCRIPT only */
PyObject* m_function; /* SCA_PYEXEC_MODULE only */
bool m_bModified;
bool m_debug; /* use with SCA_PYEXEC_MODULE for reloading every logic run */
int m_mode;
protected:
STR_String m_scriptText;
STR_String m_scriptName;
PyObject* m_pythondictionary;
PyObject* m_pythondictionary; /* for SCA_PYEXEC_SCRIPT only */
PyObject* m_pythonfunction; /* for SCA_PYEXEC_MODULE only */
std::vector<class SCA_ISensor*> m_triggeredSensors;
public:
enum SCA_PyExecMode
{
SCA_PYEXEC_SCRIPT = 0,
SCA_PYEXEC_MODULE,
SCA_PYEXEC_MAX
};
public:
static SCA_PythonController* m_sCurrentController; // protected !!!
//for debugging
//virtual CValue* AddRef();
//virtual int Release(); // Release a reference to this value (when reference count reaches 0, the value is removed from the heap)
SCA_PythonController(SCA_IObject* gameobj,PyTypeObject* T = &Type);
SCA_PythonController(SCA_IObject* gameobj, int mode, PyTypeObject* T = &Type);
virtual ~SCA_PythonController();
virtual CValue* GetReplica();
@@ -67,10 +79,14 @@ class SCA_PythonController : public SCA_IController
void SetScriptText(const STR_String& text);
void SetScriptName(const STR_String& name);
void SetDictionary(PyObject* pythondictionary);
void SetDebug(bool debug) { m_debug = debug; }
void AddTriggeredSensor(class SCA_ISensor* sensor)
{ m_triggeredSensors.push_back(sensor); }
int IsTriggered(class SCA_ISensor* sensor);
bool Compile();
bool Import();
void ErrorPrint(const char *error_msg);
static const char* sPyGetCurrentController__doc__;
static PyObject* sPyGetCurrentController(PyObject* self);