I ran into this problem and would like to improve Hell's Eye (thanks, by the way!), As it leaves an important detail.
I am using an abridged version of my problem where I am reusing the QScriptEngine object and want to make sure there is nothing left between the evaluations. In particular, I wanted to make sure that the onEquipped function onEquipped not called for the "RC Helicopter Controller" object, since it does not change its sprite when equipped, and therefore does not define the onEquipped function in its script file. Just using pushContext() and popContext() results in nothing being called at all:
#include <QtCore> #include <QtScript> int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QScriptEngine scriptEngine; scriptEngine.pushContext(); scriptEngine.evaluate("function onEquipped(entity) { print('changing pixmap to equipped sprite for ' + entity); }").toString(); QScriptValueList args; args << QScriptValue("Pistol"); scriptEngine.globalObject().property("onEquipped").call(QScriptValue(), args); scriptEngine.popContext(); scriptEngine.pushContext(); args.clear(); args << QScriptValue("RC Helicopter Controller"); scriptEngine.globalObject().property("onEquipped").call(QScriptValue(), args); scriptEngine.popContext(); return 0; }
The function call occurs in the original context, and not in the current one. After looking at the QScriptEngine :: pushContext () documentation , I saw that you need to explicitly use the context returned from it, and in addition, you should use QScriptEngine :: activationContext () to access any variables:
#include <QtCore> #include <QtScript> int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QScriptEngine scriptEngine; scriptEngine.pushContext(); scriptEngine.evaluate("function onEquipped(entity) { print('changing pixmap to equipped sprite for ' + entity); }").toString(); QScriptValueList args; args << QScriptValue("Pistol"); scriptEngine.currentContext()->activationObject().property("onEquipped").call(QScriptValue(), args); scriptEngine.popContext(); scriptEngine.pushContext(); args.clear(); args << QScriptValue("RC Helicopter Controller"); scriptEngine.currentContext()->activationObject().property("onEquipped").call(QScriptValue(), args); scriptEngine.popContext(); return 0; }
pixmap change for fitted sprite for gun
Mitch source share