QtScript:如何重新加载当前脚本?

发布于 2024-08-17 09:19:19 字数 280 浏览 9 评论 0原文

QScriptEngine 有evaluate() 方法,可用于加载脚本、执行脚本以及从已加载的脚本运行指定的函数。但是如何清除当前脚本并加载新脚本呢?例如,我使用评估()从文件加载脚本,然后使用评估()获取脚本函数并调用它们。但是我可以做什么来清除当前脚本并从不同的文件加载新脚本?删除和创建 QScriptEngine 似乎是一个解决方案,但它喜欢在 GUI 线程中创建(由于 QScriptEngineDebugger),而所有脚本操作都在单独的线程中执行。那么有没有办法在不重新创建 QScriptEngine 对象的情况下清除当前脚本?

QScriptEngine has evaluate() method that can be used to load script, execute it and to run a specified function from already loaded script. But how to clear current script and load a new one? For example, i use evaluate() to load a script from file and then evaluate() to get a script functions and call them. But what can i do to clear current script and load a new one from a different file? Deleting and creating QScriptEngine seems like a solution, but it likes to be created in GUI thread (due to QScriptEngineDebugger) while all script operations are performed in separate thread. So is it any way to clear current script without re-creating QScriptEngine object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴梦一场 2024-08-24 09:19:19
engine.pushContext();
engine.evaluate("...");
engine.popContext();
engine.pushContext();
engine.evaluate("...");
engine.popContext();

在评估脚本之前调用pushContext()并在评估新脚本之前调用popContext()将有效地清除所有脚本数据。

engine.pushContext();
engine.evaluate("...");
engine.popContext();
engine.pushContext();
engine.evaluate("...");
engine.popContext();

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

2024-08-24 09:19:19

我遇到了这个问题,并希望改进地狱之眼的答案(顺便说一句,谢谢!),因为它遗漏了一个重要的细节。

我将使用问题的简化版本,其中我重用了 QScriptEngine 对象,并希望确保在评估之间不会留下任何内容。特别是,我想确保 "RC Helicopter Controller" 实体不会调用 onEquipped 函数,因为它在装备时不会更改其精灵,并且因此在其脚本文件中没有定义 onEquipped 函数。简单地使用 pushContext()popContext() 根本不会调用任何内容:

#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;
}

函数调用似乎发生在原始上下文中,而不是当前上下文中。在查看 QScriptEngine::pushContext() 文档时,我看到您需要显式使用从它返回的上下文,最重要的是,您必须使用 QScriptEngine::activationContext() 访问任何变量:

#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;
}

将像素图更改为手枪的装备精灵

I ran into this problem, and would like to improve Eye of Hell's answer (thanks, by the way!), as it leaves out an important detail.

I'll use a cut down version of my problem, where I am reusing a QScriptEngine object and wanted to ensure that nothing was left behind between evaluations. In particular, I wanted to ensure that the onEquipped function was not called for the "RC Helicopter Controller" entity, as it doesn't change its sprite when it is equipped and hence doesn't define an onEquipped function in its script file. Simply 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 seems to happen on the original context, not the current one. Upon looking at QScriptEngine::pushContext() documentation, I saw that you need to explicitly use the context returned from it, and on top of that, you must 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;
}

changing pixmap to equipped sprite for Pistol

2024-08-24 09:19:19

您可以尝试将空对象设置为 http://qt.nokia.com /doc/4.6/qscriptengine.html#setGlobalObject

也许可行。

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

Maybe that works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文