BGE Py API - GameKeys.EventToString() utility function, makes key configuration menu's easier to write.

own error with blenderplayer, wasnt decreffing the GameLogic module, probably didnt matter since python was restarted anyway, but is incorrect.
This commit is contained in:
Campbell Barton 2008-08-29 03:15:17 +00:00
parent 272132888f
commit f60992daae
3 changed files with 39 additions and 0 deletions

@ -716,6 +716,7 @@ void GPG_Application::stopEngine()
} else { } else {
printf("Error, GameLogic.globalDict could not be marshal'd\n"); printf("Error, GameLogic.globalDict could not be marshal'd\n");
} }
Py_DECREF(gameLogic);
} else { } else {
printf("Error, GameLogic.globalDict was removed\n"); printf("Error, GameLogic.globalDict was removed\n");
} }

@ -1048,9 +1048,38 @@ static char GameKeys_module_documentation[] =
"This modules provides defines for key-codes" "This modules provides defines for key-codes"
; ;
static char gPyEventToString_doc[] =
"Take a valid event from the GameKeys module or Keyboard Sensor and return a name"
;
static PyObject* gPyEventToString(PyObject*, PyObject* value)
{
PyObject* mod, *dict, *key, *val, *ret = NULL;
int pos = 0;
mod = PyImport_ImportModule( "GameKeys" );
if (!mod)
return NULL;
dict = PyModule_GetDict(mod);
while (PyDict_Next(dict, &pos, &key, &val)) {
if (PyObject_Compare(value, val)==0) {
ret = key;
break;
}
}
PyErr_Clear(); // incase there was an error clearing
Py_DECREF(mod);
if (!ret) PyErr_SetString(PyExc_ValueError, "expected a valid int keyboard event");
else Py_INCREF(ret);
return ret;
}
static struct PyMethodDef gamekeys_methods[] = { static struct PyMethodDef gamekeys_methods[] = {
{"EventToString", (PyCFunction)gPyEventToString, METH_O, gPyEventToString_doc},
{ NULL, (PyCFunction) NULL, 0, NULL } { NULL, (PyCFunction) NULL, 0, NULL }
}; };

@ -164,3 +164,12 @@ Example::
# Activate Right! # Activate Right!
""" """
def EventToString(event):
"""
Return the string name of a key event. Will raise a ValueError error if its invalid.
@type event: int
@param event: key event from GameKeys or the keyboard sensor.
@rtype: string
"""