In NaN times I suggested a python function to get the subject of a
authorKester Maddock <Christopher.Maddock.1@uni.massey.ac.nz>
Thu, 8 Apr 2004 11:36:22 +0000 (11:36 +0000)
committerKester Maddock <Christopher.Maddock.1@uni.massey.ac.nz>
Thu, 8 Apr 2004 11:36:22 +0000 (11:36 +0000)
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

source/gameengine/Ketsji/KXNetwork/KX_NetworkMessageSensor.cpp
source/gameengine/Ketsji/KXNetwork/KX_NetworkMessageSensor.h

index 7e1e46d029a7ec0973756be022f115837f9c8a9c..ab5f9b989a70aac1017dcfd2b5764ab7345d9a6d 100644 (file)
@@ -60,9 +60,10 @@ KX_NetworkMessageSensor::KX_NetworkMessageSensor(
     m_Networkeventmgr(eventmgr),
     m_NetworkScene(NetworkScene),
     m_subject(subject),
     m_Networkeventmgr(eventmgr),
     m_NetworkScene(NetworkScene),
     m_subject(subject),
-       m_frame_message_count (0),
+    m_frame_message_count (0),
     m_IsUp(false),
     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;
        }
 
                m_BodyList = NULL;
        }
 
+       if (m_SubjectList) {
+               m_SubjectList->Release();
+               m_SubjectList = NULL;
+       }
+
        STR_String toname=GetParent()->GetName();
        STR_String subject = this->m_subject;
 
        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();
 #endif
                m_IsUp = true;
                m_BodyList = new CListValue();
+               m_SubjectList = new CListValue();
        }
 
        vector<NG_NetworkMessage*>::iterator mesit;
        }
 
        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 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"));
 #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();
 
                // free the message
                (*mesit)->Release();
@@ -186,6 +197,9 @@ PyMethodDef KX_NetworkMessageSensor::Methods[] = {
        {"getSubject", (PyCFunction)
                KX_NetworkMessageSensor::sPyGetSubject, METH_VARARGS,
                GetSubject_doc},
        {"getSubject", (PyCFunction)
                KX_NetworkMessageSensor::sPyGetSubject, METH_VARARGS,
                GetSubject_doc},
+       {"getSubjects", (PyCFunction)
+               KX_NetworkMessageSensor::sPyGetSubjects, METH_VARARGS,
+               GetSubjects_doc},
        {NULL,NULL} //Sentinel
 };
 
        {NULL,NULL} //Sentinel
 };
 
@@ -243,10 +257,10 @@ PyObject* KX_NetworkMessageSensor::PyGetBodies(
        Py_Return;
 }
 
        Py_Return;
 }
 
-// 4. Get the message subject
+// 4. Get the message subject: field of the message sensor
 char KX_NetworkMessageSensor::GetSubject_doc[] =
 "\tgetSubject()\n"
 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,
 
 PyObject* KX_NetworkMessageSensor::PyGetSubject(
        PyObject* self,
@@ -260,3 +274,19 @@ PyObject* KX_NetworkMessageSensor::PyGetSubject(
        Py_Return;
 }
 
        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;
+}
index 8335a8ba7a457d7670f48fc4415eed13e8d1f0c4..f2b8b306d9d09caac8ff7d8853c559139066b48f 100644 (file)
@@ -54,6 +54,7 @@ class KX_NetworkMessageSensor : public SCA_ISensor
        bool m_IsUp;
 
        class CListValue* m_BodyList;
        bool m_IsUp;
 
        class CListValue* m_BodyList;
+       class CListValue* m_SubjectList;
 public:
        KX_NetworkMessageSensor(
                KX_NetworkEventManager* eventmgr,       // our eventmanager
 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, GetFrameMessageCount);
        KX_PYMETHOD_DOC(KX_NetworkMessageSensor, GetBodies);
        KX_PYMETHOD_DOC(KX_NetworkMessageSensor, GetSubject);
+       KX_PYMETHOD_DOC(KX_NetworkMessageSensor, GetSubjects);
 
 
 };
 
 
 };