In NaN times I suggested a python function to get the subject of a

Message using a python function of the MessageSensor.

Thats a nice thing if you want complex message handling in one python
script. Just get all messages, check the subject and do what you
want. In the current situation you end up with several MessageSensors
connected to the python script, instead of one Sensor and a smart
script.

Some developer (cant remember who) did implement that function, but
however not the way I wanted (maybe I was not clear enough) ;-) So the
getSubject() function will return whats entered in the "Subject:"
filter field of the MessageSensor. Quite useless IMHO.

So I added a new function getSubjects() which is similar to
getBodies(), in fact I stole the code from there ;-)

I left the getSubject() alone, because of backward compatibility
(never saw someone using that function, but...)


The future:

In conjunction with a wildcard subject: filter field the getSubjects()
function will be even more usefull.

i.e. Player* will filter for PlayerScore, PlayerKill etc.

-- Carsten Wartmann
This commit is contained in:
Kester Maddock 2004-04-08 11:36:22 +00:00
parent 5398f1ba77
commit 80485c2926
2 changed files with 36 additions and 4 deletions

@ -60,9 +60,10 @@ KX_NetworkMessageSensor::KX_NetworkMessageSensor(
m_Networkeventmgr(eventmgr),
m_NetworkScene(NetworkScene),
m_subject(subject),
m_frame_message_count (0),
m_frame_message_count (0),
m_IsUp(false),
m_BodyList(NULL)
m_BodyList(NULL),
m_SubjectList(NULL)
{
}
@ -95,6 +96,11 @@ bool KX_NetworkMessageSensor::Evaluate(CValue* event)
m_BodyList = NULL;
}
if (m_SubjectList) {
m_SubjectList->Release();
m_SubjectList = NULL;
}
STR_String toname=GetParent()->GetName();
STR_String subject = this->m_subject;
@ -109,6 +115,7 @@ bool KX_NetworkMessageSensor::Evaluate(CValue* event)
#endif
m_IsUp = true;
m_BodyList = new CListValue();
m_SubjectList = new CListValue();
}
vector<NG_NetworkMessage*>::iterator mesit;
@ -116,12 +123,16 @@ bool KX_NetworkMessageSensor::Evaluate(CValue* event)
{
// save the body
STR_String body = (*mesit)->GetMessageText();
// save the subject
STR_String messub = (*mesit)->GetSubject();
#ifdef NAN_NET_DEBUG
if (body) {
cout << "body [" << body << "]\n";
}
#endif
m_BodyList->Add(new CStringValue(body,"body"));
// Store Subject
m_SubjectList->Add(new CStringValue(messub,"subject"));
// free the message
(*mesit)->Release();
@ -186,6 +197,9 @@ PyMethodDef KX_NetworkMessageSensor::Methods[] = {
{"getSubject", (PyCFunction)
KX_NetworkMessageSensor::sPyGetSubject, METH_VARARGS,
GetSubject_doc},
{"getSubjects", (PyCFunction)
KX_NetworkMessageSensor::sPyGetSubjects, METH_VARARGS,
GetSubjects_doc},
{NULL,NULL} //Sentinel
};
@ -243,10 +257,10 @@ PyObject* KX_NetworkMessageSensor::PyGetBodies(
Py_Return;
}
// 4. Get the message subject
// 4. Get the message subject: field of the message sensor
char KX_NetworkMessageSensor::GetSubject_doc[] =
"\tgetSubject()\n"
"\tGet the subject of the message.\n";
"\tGet the subject: field of the message sensor.\n";
PyObject* KX_NetworkMessageSensor::PyGetSubject(
PyObject* self,
@ -260,3 +274,19 @@ PyObject* KX_NetworkMessageSensor::PyGetSubject(
Py_Return;
}
// 5. Get the message subjects
char KX_NetworkMessageSensor::GetSubjects_doc[] =
"\tgetSubjects()\n"
"\tGet list of message subjects.\n";
PyObject* KX_NetworkMessageSensor::PyGetSubjects(
PyObject* self,
PyObject* args,
PyObject* kwds)
{
if (m_SubjectList) {
return ((PyObject*) m_SubjectList->AddRef());
}
Py_Return;
}

@ -54,6 +54,7 @@ class KX_NetworkMessageSensor : public SCA_ISensor
bool m_IsUp;
class CListValue* m_BodyList;
class CListValue* m_SubjectList;
public:
KX_NetworkMessageSensor(
KX_NetworkEventManager* eventmgr, // our eventmanager
@ -79,6 +80,7 @@ public:
KX_PYMETHOD_DOC(KX_NetworkMessageSensor, GetFrameMessageCount);
KX_PYMETHOD_DOC(KX_NetworkMessageSensor, GetBodies);
KX_PYMETHOD_DOC(KX_NetworkMessageSensor, GetSubject);
KX_PYMETHOD_DOC(KX_NetworkMessageSensor, GetSubjects);
};