BGE patch #18350: Add sendMessage() to GameLogic. Added sendMessage to both GameLogic and KX_GameObject.

This commit is contained in:
Benoit Bolsee 2009-04-08 16:57:08 +00:00
parent 370850146f
commit db33320df7
6 changed files with 78 additions and 2 deletions

@ -66,6 +66,7 @@ typedef unsigned long uint_ptr;
#include "SCA_IActuator.h"
#include "SCA_ISensor.h"
#include "SCA_IController.h"
#include "NG_NetworkScene.h" //Needed for sendMessage()
#include "PyObjectPlus.h" /* python stuff */
@ -1039,7 +1040,8 @@ PyMethodDef KX_GameObject::Methods[] = {
KX_PYMETHODTABLE(KX_GameObject, rayCast),
KX_PYMETHODTABLE_O(KX_GameObject, getDistanceTo),
KX_PYMETHODTABLE_O(KX_GameObject, getVectTo),
KX_PYMETHODTABLE(KX_GameObject, sendMessage),
// deprecated
{"getPosition", (PyCFunction) KX_GameObject::sPyGetPosition, METH_NOARGS},
{"setPosition", (PyCFunction) KX_GameObject::sPySetPosition, METH_O},
@ -2335,6 +2337,26 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCast,
return Py_BuildValue("OOO", Py_None, Py_None, Py_None);
}
KX_PYMETHODDEF_DOC_VARARGS(KX_GameObject, sendMessage,
"sendMessage(subject, [body, to])\n"
"sends a message in same manner as a message actuator"
"subject = Subject of the message (string)"
"body = Message body (string)"
"to = Name of object to send the message to")
{
char* subject;
char* body = "";
char* to = "";
const STR_String& from = GetName();
if (!PyArg_ParseTuple(args, "s|sss", &subject, &body, &to))
return NULL;
KX_GetActiveScene()->GetNetworkScene()->SendMessage(to, from, subject, body);
Py_RETURN_NONE;
}
/* ---------------------------------------------------------------------
* Some stuff taken from the header
* --------------------------------------------------------------------- */

@ -815,7 +815,7 @@ public:
KX_PYMETHOD_DOC(KX_GameObject,rayCast);
KX_PYMETHOD_DOC_O(KX_GameObject,getDistanceTo);
KX_PYMETHOD_DOC_O(KX_GameObject,getVectTo);
KX_PYMETHOD_DOC_VARARGS(KX_GameObject, sendMessage);
/* attributes */
static PyObject* pyattr_get_name(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
static PyObject* pyattr_get_parent(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);

@ -67,6 +67,8 @@
#include "KX_Scene.h"
#include "SND_DeviceManager.h"
#include "NG_NetworkScene.h" //Needed for sendMessage()
#include "BL_Shader.h"
#include "KX_PyMath.h"
@ -167,6 +169,28 @@ static PyObject* gPyExpandPath(PyObject*, PyObject* args)
return PyString_FromString(expanded);
}
static char gPySendMessage_doc[] =
"sendMessage(subject, [body, to, from])\n\
sends a message in same manner as a message actuator\
subject = Subject of the message\
body = Message body\
to = Name of object to send the message to\
from = Name of object to sned the string from";
static PyObject* gPySendMessage(PyObject*, PyObject* args)
{
char* subject;
char* body = "";
char* to = "";
char* from = "";
if (!PyArg_ParseTuple(args, "s|sss", &subject, &body, &to, &from))
return NULL;
gp_KetsjiScene->GetNetworkScene()->SendMessage(to, from, subject, body);
Py_RETURN_NONE;
}
static bool usedsp = false;
@ -436,6 +460,7 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
static struct PyMethodDef game_methods[] = {
{"expandPath", (PyCFunction)gPyExpandPath, METH_VARARGS, (PY_METHODCHAR)gPyExpandPath_doc},
{"sendMessage", (PyCFunction)gPySendMessage, METH_VARARGS, (PY_METHODCHAR)gPySendMessage_doc},
{"getCurrentController",
(PyCFunction) SCA_PythonController::sPyGetCurrentController,
METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::sPyGetCurrentController__doc__},

@ -34,6 +34,11 @@
#include "STR_HashedString.h"
#include <vector>
//MSVC defines SendMessage as a win api function, even though we aren't using it
#ifdef SendMessage
#undef SendMessage
#endif
class NG_NetworkDeviceInterface;
class NG_NetworkScene

@ -205,6 +205,19 @@ def addActiveActuator(actuator, activate):
@type activate: boolean
@param activate: whether to activate or deactivate the given actuator.
"""
def sendMessage(subject, body="", to="", from=""):
"""
Sends a message.
@param subject: The subject of the message
@type subject: string
@param body: The body of the message (optional)
@type body: string
@param to: The name of the object to send the message to (optional)
@type to: string
@param from: The name of the object that the message is coming from (optional)
@type from: string
"""
def getRandomFloat():
"""
Returns a random floating point value in the range [0...1)

@ -453,3 +453,14 @@ class KX_GameObject: # (SCA_IObject)
@type margin: float
@param margin: the collision margin distance in blender units.
"""
def sendMessage(subject, body="", to=""):
"""
Sends a message.
@param subject: The subject of the message
@type subject: string
@param body: The body of the message (optional)
@type body: string
@param to: The name of the object to send the message to (optional)
@type to: string
"""