importing the GameLogic module was being done by adding the text "import GameLogic" to the start of all scripts used in the game engine, this meant every error line number was off by 1 (quite annoying). better to do this to the dictionary that the scripts run with.

This commit is contained in:
Campbell Barton 2008-07-14 00:47:07 +00:00
parent 8a19adeb58
commit 38cfe9c1a2
7 changed files with 27 additions and 22 deletions

@ -332,6 +332,7 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
ketsjiengine->SetPythonDictionary(dictionaryobject);
initRasterizer(rasterizer, canvas);
PyObject *gameLogic = initGameLogic(startscene);
PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module.
initGameKeys();
initPythonConstraintBinding();
@ -399,7 +400,14 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
exitstring = ketsjiengine->GetExitString();
// when exiting the mainloop
dictionaryClearByHand(gameLogic);
// Clears the dictionary by hand:
// This prevents, extra references to global variables
// inside the GameLogic dictionary when the python interpreter is finalized.
// which allows the scene to safely delete them :)
// see: (space.c)->start_game
PyDict_Clear(PyModule_GetDict(gameLogic));
ketsjiengine->StopEngine();
exitGamePythonScripting();
networkdevice->Disconnect();
@ -591,6 +599,7 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
ketsjiengine->SetPythonDictionary(dictionaryobject);
initRasterizer(rasterizer, canvas);
PyObject *gameLogic = initGameLogic(startscene);
PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module
initGameKeys();
initPythonConstraintBinding();

@ -116,7 +116,7 @@ CValue* SCA_PythonController::GetReplica()
void SCA_PythonController::SetScriptText(const STR_String& text)
{
m_scriptText = "import GameLogic\n" + text;
m_scriptText = text;
m_bModified = true;
}
@ -354,8 +354,10 @@ SCA_PythonController::PyGetSensor(PyObject* self, PyObject* value)
return sensor->AddRef();
}
}
PyErr_SetString(PyExc_AttributeError, "Unable to find requested sensor");
char emsg[96];
PyOS_snprintf( emsg, sizeof( emsg ), "Unable to find requested sensor \"%s\"", scriptArg );
PyErr_SetString(PyExc_AttributeError, emsg);
return NULL;
}
@ -382,8 +384,10 @@ SCA_PythonController::PyGetActuator(PyObject* self, PyObject* value)
return actua->AddRef();
}
}
PyErr_SetString(PyExc_AttributeError, "Unable to find requested actuator");
char emsg[96];
PyOS_snprintf( emsg, sizeof( emsg ), "Unable to find requested actuator \"%s\"", scriptArg );
PyErr_SetString(PyExc_AttributeError, emsg);
return NULL;
}

@ -644,7 +644,7 @@ bool GPG_Application::startEngine(void)
PyObject* dictionaryobject = initGamePlayerPythonScripting("Ketsji", psl_Lowest);
m_ketsjiengine->SetPythonDictionary(dictionaryobject);
initRasterizer(m_rasterizer, m_canvas);
PyObject *gameLogic = initGameLogic(startscene);
PyDict_SetItemString(dictionaryobject, "GameLogic", initGameLogic(startscene)); // Same as importing the module
initGameKeys();
initPythonConstraintBinding();

@ -231,7 +231,10 @@ void KX_KetsjiEngine::SetRasterizer(RAS_IRasterizer* rasterizer)
}
/*
* At the moment the GameLogic module is imported into 'pythondictionary' after this function is called.
* if this function ever changes to assign a copy, make sure the game logic module is imported into this dictionary before hand.
*/
void KX_KetsjiEngine::SetPythonDictionary(PyObject* pythondictionary)
{
MT_assert(pythondictionary);

@ -828,20 +828,9 @@ PyObject* initGameLogic(KX_Scene* scene) // quick hack to get gravity hook
Py_FatalError("can't initialize module GameLogic");
}
return d;
return m;
}
void dictionaryClearByHand(PyObject *dict)
{
// Clears the dictionary by hand:
// This prevents, extra references to global variables
// inside the GameLogic dictionary when the python interpreter is finalized.
// which allows the scene to safely delete them :)
// see: (space.c)->start_game
if(dict) PyDict_Clear(dict);
}
// Python Sandbox code
// override builtin functions import() and open()

@ -47,7 +47,6 @@ PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur
void exitGamePlayerPythonScripting();
PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level);
void exitGamePythonScripting();
void dictionaryClearByHand(PyObject *dict);
void PHY_SetActiveScene(class KX_Scene* scene);
class KX_Scene* PHY_GetActiveScene();

@ -291,7 +291,8 @@ PyObject* KX_SoundActuator::PyGetFilename(PyObject* self, PyObject* args, PyObje
char* name = objectname.Ptr();
if (!name) {
Py_Return; /* internal error */
PyErr_SetString(PyExc_RuntimeError, "Unable to get sound filename");
return NULL;
} else
return PyString_FromString(name);
}