Resources

Resources are singular data structures in an ECS World. As such, they can be thought of as Components that exist only once and are not associated to an Entity. Examples could be the current game/simulation tick, a grid that your entities live on, or an acceleration structure for spatial indexing.

Creating resources

Resources can be of any type, but only one resource of a particular type can exist in a World. They are simply added to the world with add_resource!:

struct Tick
    time::Int
end

add_resource!(world, Tick(0))

Accessing resources

Resources can be retrieved via get_resource:

tick = get_resource(world, Tick)
time = tick.time

As getting a resource is not particularly fast (10-15ns), this should not be done in hot loops like queries, but beforehand.

The existence of a resource type in the World can be checked with has_resource:

if has_resource(world, Tick)
    # ...
end

Setting and removing resources

Resources can also be removed from the world using remove_resource!, or overwritten with set_resource!, which is particularly useful for immutable types:

set_resource!(world, Tick(1))
remove_resource!(world, Tick)