QtScript: how to reload the current script?

QScriptEngine has an evaluation method () that you can use to load a script, execute it, and run a specific function from an already loaded script. But how to clear the current script and load a new one? For example, I use the () function to load a script file from a file, and then () () to get the script functions and call them. But what can I do to clear the current script and load a new one from another file? Removing and creating a QScriptEngine seems like a solution, but it likes to be created in a GUI thread (due to QScriptEngineDebugger), and all script operations are executed in a separate thread. So is it possible to clear the current script without re-creating the QScriptEngine object?

+4
source share
3 answers

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

+2
source
 engine.pushContext(); engine.evaluate("..."); engine.popContext(); engine.pushContext(); engine.evaluate("..."); engine.popContext(); 

Calling pushContext () before evaluating the script and calling popContext () before evaluating the new script will effectively clear all script data.

+7
source

You can try to set an empty object at http://qt.nokia.com/doc/4.6/qscriptengine.html#setGlobalObject

Maybe it works.

0
source

All Articles