pub struct World { /* private fields */ }
Expand description

A collection of resources and systems.

The World is the executor of your systems and async computations.

Most applications will create and configure a World in their main function, finally running it or ticking it in a loop.

use apecs::*;

#[derive(CanFetch)]
struct MyAppData {
    channel: Read<chan::mpmc::Channel<String>>,
    count: Write<usize>,
}

let mut world = World::default();
world
    .with_system(
        "compute_hello",
        |mut data: MyAppData| -> anyhow::Result<ShouldContinue> {
            if *data.count >= 3 {
                data.channel
                    .tx
                    .try_broadcast("hello world".to_string())
                    .unwrap();
                end()
            } else {
                *data.count += 1;
                ok()
            }
        },
    )
    .unwrap()
    .with_async_system("await_hello", |mut facade: Facade| async move {
        let mut rx = facade
            .visit(|mut data: MyAppData| Ok(data.channel.new_receiver()))
            .await?;
        if let Ok(msg) = rx.recv().await {
            println!("got message: {}", msg);
        }
        Ok(())
    });
world.run();

World is the outermost object of apecs, as it contains all systems and resources. It contains Entities and Components as default resources. You can create entities, attach components and query them from outside the world if desired:

let mut world = World::default();
// Nearly any type can be used as a component with zero boilerplate
let a = world.entity_with_bundle((123, true, "abc"));
let b = world.entity_with_bundle((42, false));

// Query the world for all matching bundles
let mut query = world.query::<(&mut i32, &bool)>();
for (number, flag) in query.iter_mut() {
    if **flag {
        **number *= 2;
    }
}

// Perform random access within the same query
let a_entry: &Entry<i32> = query.find_one(a.id()).unwrap().0;
assert_eq!(**a_entry, 246);
// Track changes to individual components
assert_eq!(apecs::current_iteration(), a_entry.last_changed());

Where to look next 📚

  • Entities for info on creating and deleting Entitys
  • Components for info on creating updating and deleting components
  • Entry for info on tracking changes to individual components
  • Query for info on querying bundles of components
  • Facade for info on interacting with the World from within an async system
  • Plugin for info on how to bundle systems and resources of common operation together into an easy-to-integrate package

Implementations

Add a plugin to the world, instantiating any missing resources or systems.

Errs

Errs if the plugin requires resources that cannot be created by default.

Add a syncronous system.

Errs

Errs if expected resources must first be added to the world.

Add a syncronous system that has a dependency on one or more other syncronous systems.

Errs

Errs if expected resources must first be added to the world.

Add a syncronous system barrier.

Any systems added after the barrier will be scheduled after the systems added before the barrier.

Returns whether a resources of the given type exists in the world.

Spawn an asynchronous task.

Conduct a world tick.

Calls World::tick_async, then World::tick_sync, then World::tick_lazy.

Panics

Panics if any sub-tick step returns an error

Just tick the synchronous systems.

Just tick the async futures, including sending resources to async systems.

Applies lazy world updates.

Also runs component entity and archetype upkeep.

Run all systems and futures until they have finished or one system has erred, whichever comes first.

Attempt to get a reference to one resource.

Attempt to get a mutable reference to one resource.

Create a new entity.

Create a new entity and immediately attach the bundle of components to it.

Returns the scheduled systems’ names, collated by batch.

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.