CCNotificationCenter::postNotification cannot pass data to script engine

In c**, we can pass data when postNotification:
<pre>
CCNotificationCenter::sharedNotificationCenter~~>postNotification);
</pre>
And get it in callback:
<pre>
void MyObject::callback
{
CCString * myData = pObj;
// …
}
</pre>
But this cannot work in lua, I found that postNotification don’t pass the second parameter to script engine.
<pre>
void CCNotificationCenter::postNotification
{
CCArray* ObserversCopy = CCArray::createWithCapacity);
ObserversCopy~~>addObjectsFromArray;
CCObject* obj = NULL;
CCARRAY_FOREACH
{
CCNotificationObserver* observer = obj;
if
continue;
if ) && object || observer->getObject() NULL || object == NULL))
{
if )
{
CCScriptEngineProtocol* engine = CCScriptEngineManager::sharedManager~~>getScriptEngine;
engine~~>executeNotificationEvent; // <——— “object” not used
}
else
{
observer->performSelector;
}
}
}
}
</pre>
I can pass data from lua to c** using postNotification:

CCNotificationCenter:sharedNotificationCenter():postNotification("notify 1", CCString:create("my data"))

How can I pass data to lua?

You can extend the postNotification function by increasing the input params like this:
int executeNotificationEvent(NotificationCenter* pNotificationCenter, const char* pszName,CCObject *object = NULL);
and modify the implementation of this function.

samuele3 hu wrote:

You can extend the postNotification function by increasing the input params like this:
int executeNotificationEvent(NotificationCenter* pNotificationCenter, const char* pszName,CCObject *object = NULL);
and modify the implementation of this function.

But, must modify CCNotificationCenter::postNotification to use it, how to extend CCNotificationCenter transparently?

it seems that this issue was not fixed in the latest version 2.2.2 and 3.0beta. i also wanna use CCNotificationCenter in my LUA script to accept message with a CCObject* parameter from cpp code…

BTW i wanna know if the ‘extend’ way works?

You can try my suggestion above,although it will modify CCNotificationCenter::postNotification

Samuele3 Hu wrote:

You can try my suggestion above,although it will modify CCNotificationCenter::postNotification

I just tried the “extend” way and successfully transfer an CCObject* to my LUA script, thanks a lot!

I added an overload func to CCScriptEngineProtocol:

virtual int executeNotificationEvent(CCNotificationCenter* pNotificationCenter, const char* pszName, CCObject* pObj) = 0;

and implement it in CCLuaEngine like this:

int CCLuaEngine::executeNotificationEvent(CCNotificationCenter* pNotificationCenter, const char* pszName, CCObject* pObj)
{
int nHandler = pNotificationCenter->getObserverHandlerByName(pszName);
if (!nHandler) return 0;

m_stack->pushString(pszName);
if (pObj)
	m_stack->pushCCObject(pObj, "CCObject");
int ret = m_stack->executeFunctionByHandler(nHandler, pObj ? 2 : 1);
m_stack->clean();
return ret;

}

also replace the content of the old function “executeNotificationEvent” with just one line code:

return executeNotificationEvent(pNotificationCenter, pszName, NULL);

then i can write a callback function in LUA with 2 parameters.:smiley: