wasvy-org/wasvy

Implementing `Single`

开放

#57 创建于 2026年4月12日

 (0 条评论) (0 个反应) (0 位负责人)Rust (6 个派生)auto 404
help wanted

仓库指标

星标
 (126 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

Following some of the exploration done by @shishanyue and https://github.com/wasvy-org/wasvy/discussions/56, it seems natural to me that we should implement bevy's Single.

A Single is very similar to a query. Singles differ from Queries only in one major way: if 0 or >1 matching components are found in the world, the system does not execute.

A Single is also just Bevy's way of getting a reference to a component in the world. So in wit, it is more appropriate to alias the existing component resource:

/// A `single` system param is resolved by wasvy as a component reference
type single = component;

In order to implement, first we need a way to add a single to a system within the setup method. We should add this functionality to the systems resource: https://github.com/wasvy-org/wasvy/blob/45549c53ce79b52e61e7663ada9dc3edf5bb4c40/wit/ecs/ecs.wit#L50-L66

Then we need an initialization mechanism for new singles. Wasvy needs to init all the wit resources that get passed as parameters when a system is executed. This method is responsible: https://github.com/wasvy-org/wasvy/blob/0da11cba82c5837d18431974f2e86a95e44966ca/src/system.rs#L286-L300

State::RunSystem contains a queries parameter which we will use to hold the Query used to fetch this component from the world: https://github.com/wasvy-org/wasvy/blob/45549c53ce79b52e61e7663ada9dc3edf5bb4c40/src/runner.rs#L185

Thus it'll be necessary for the generated component to point to (via the QueryId struct) the correct query in the queries, and ensure that during the setup phase we build the necessary query.

贡献者指南