【问题标题】:Change state from outside Bevy system我可以在 bevy 系统之外更新 bevy 应用程序状态或资源吗?
【发布时间】:2022-12-30 11:52:32
【问题描述】:

我已经有一个在浏览器中运行的 bevy 应用程序。

我想做的是在 js/ts 方面有一些功能,可以在 bevy 世界中创建或销毁实体,这可能吗?我已经尝试let app=App::new();,然后绑定一个函数来运行应用程序app.run();,并绑定一个函数来覆盖资源app.insert_resource(...);。但是当我在运行应用程序后调用函数来覆盖资源时,它会显示错误消息:递归使用检测到的对象,这将导致锈蚀中的不安全别名。


感谢@kmdreko 的建议,我尝试使用 Arc 更新 resrouce,但在此之前似乎还有另一个问题,问题是在我初始化 bevy 应用程序之后,其余代码将永远无法访问,这是我的代码:

<script type="module">
    import init, {BevyApp} from '../pkg/wasm_bevy_demo.js';

    init().then(() => {
        // new() function create and run a bevy app, and return a Arc<Mutex> in BevyApp{}
        const bevyCode = BevyApp.new();
        // this log info never show in the console
        console.log("reach after run bevy app");
        bevyCode.update_scroll_rate(10, 10);
    })
</script>

【问题讨论】:

  • 只是一个猜测,但您可能会尝试修改现有资源,而不是使用 insert_resource 覆盖它。不过,您可能必须使用共享可变性(Arc&lt;Mutex&gt;Rc&lt;RefCell&gt;)。

标签: rust wasm-bindgen bevy


【解决方案1】:

我们看一下Bevyrun()函数,当run()执行时,你之前创建的app已经被empty()替换了:

pub fn run(&mut self) {
    // ...
    let mut app = std::mem::replace(self, App::empty());
    let runner = std::mem::replace(&mut app.runner, Box::new(run_once));
    
    (runner)(app);
}

因此,无法在 bevy 系统之外更新 bevy 应用程序状态或资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-14
    • 2013-10-13
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    相关资源
    最近更新 更多