API

Ark's public API.

World

The World is the central data storage for Entities, Components and Resources.

Ark.WorldType
World{CS<:Tuple,CT<:Tuple,N}

The World is the central ECS storage.

source
Ark.WorldMethod
World(comp_types::Type...; allow_mutable::Bool=false)

Creates a new, empty World for the given component types.

Arguments

  • comp_types: The component types used by the world.
  • allow_mutable: Allows mutable components. Use with care, as all mutable objects are heap-allocated in Julia.

Example

world = World(Position, Velocity)
;
source
Ark.is_lockedFunction
is_locked(world::World)::Bool

Returns whether the world is currently locked for modifications.

source

Entities

Entities are the "game objects" or "model entities". An entity if just an ID with a generation, but Components can be attached to an entity.

Ark.new_entity!Function
new_entity!(world::World, values::Tuple)::Entity

Creates a new Entity with the given component values. Types are inferred from the values.

source
Ark.new_entities!Function
new_entities!(world::World, n::Int, defaults::Tuple; iterate::Bool=false)::Union{Batch,Nothing}

Creates the given number of Entity, initialized with default values. Component types are inferred from the provided default values.

If iterate is true, a Batch iterator over the newly created entities is returned that can be used for initialization.

Arguments

  • world::World: The World instance to use.
  • n::Int: The number of entities to create.
  • defaults::Tuple: A tuple of default values for initialization, like (Position(0, 0), Velocity(1, 1)).
  • iterate::Bool: Whether to return a batch for individual entity initialization.
source
new_entities!(world::World, n::Int, comp_types::Tuple{Vararg{Val}})::Batch

Creates the given number of Entity.

Returns a Batch iterator over the newly created entities that should be used to initialize components. Note that components are not initialized/undef unless set in the iterator!

For a more convenient tuple syntax, the macro @new_entities! is provided.

Arguments

  • world::World: The World instance to use.
  • n::Int: The number of entities to create.
  • comp_types::Tuple: Component types for the new entities, like Val.((Position, Velocity)).
source
Ark.@new_entities!Macro
@new_entities!(world::World, n::Int, comp_types::Tuple{Vararg{Val}})::Batch

Creates the given number of Entity.

Returns a Batch iterator over the newly created entities that should be used to initialize components. Note that components are not initialized/undef unless set in the iterator.

Macro version of new_entities! for ergonomic construction of component mappers.

Arguments

  • world::World: The World instance to use.
  • n::Int: The number of entities to create.
  • comp_types::Tuple: Component types for the new entities, like (Position, Velocity).
source

Components

Components contain the data associated with Entities

Ark.get_componentsFunction
get_components(world::World, entity::Entity, comp_types::Tuple)

Get the given components for an Entity. Components are returned in a tuple.

For a more convenient tuple syntax, the macro @get_components is provided.

Example

pos, vel = get_components(world, entity, Val.((Position, Velocity)))
source
Ark.@get_componentsMacro
@get_components(world::World, entity::Entity, comp_types::Tuple)

Get the given components for an Entity. Components are returned in a tuple.

Macro version of get_components for more ergonomic component type tuples.

Example

pos, vel = @get_components(world, entity, (Position, Velocity))
source
Ark.has_componentsFunction
has_components(world::World, entity::Entity, comp_types::Tuple)::Bool

Returns whether an Entity has all given components.

For a more convenient tuple syntax, the macro @has_components is provided.

Example

has = has_components(world, entity, Val.((Position, Velocity)))
source
Ark.@has_componentsMacro
@has_components(world::World, entity::Entity, comp_types::Tuple)::Bool

Returns whether an Entity has all given components.

Macro version of has_components for more ergonomic component type tuples.

Example

has = @has_components(world, entity, (Position, Velocity))
source
Ark.set_components!Function
set_components!(world::World, entity::Entity, values::Tuple)

Sets the given component values for an Entity. Types are inferred from the values. The entity must already have all these components.

source
Ark.add_components!Function
add_components!(world::World, entity::Entity, values::Tuple)

Adds the given component values to an Entity. Types are inferred from the values.

source
Ark.remove_components!Function
remove_components!(world::World, entity::Entity, comp_types::Tuple)

Removes the given components from an Entity.

For a more convenient tuple syntax, the macro @remove_components! is provided.

Example

remove_components!(world, entity, Val.((Position, Velocity)))
source
Ark.@remove_components!Macro
@remove_components!(world::World, entity::Entity, comp_types::Tuple)

Removes the given components from an Entity.

Macro version of remove_components! for ergonomic construction of component mappers.

Example

@remove_components!(world, entity, (Position, Velocity))
source
Ark.exchange_components!Function
exchange_components!(world::World{CS,CT,N}, entity::Entity; add::Tuple, remove::Tuple)

Adds and removes components on an Entity. Types are inferred from the add values.

For a more convenient tuple syntax, the macro @exchange_components! is provided.

Example

exchange_components!(world, entity;
    add=(Health(100),),
    remove=Val.((Position, Velocity)),
)
source
Ark.@exchange_components!Macro
@exchange_components!(world::World, entity::Entity, add::Tuple, remove::Tuple)

Removes the given components from an Entity.

Macro version of exchange_components! for more ergonomic component type tuples.

Example

@exchange_components!(world, entity,
    add = (Health(100),),
    remove = Val.((Position, Velocity)),
)
source

Queries

Queries are used to filter and process Entities with a certain set of Components.

Ark.@QueryMacro
@Query(
    world::World,
    comp_types::Tuple,
    with::Tuple=(),
    without::Tuple=(),
    optional::Tuple=(),
    exclusive::Bool=false
)

Creates a query.

Macro version of Query that allows ergonomic construction of queries using simulated keyword arguments.

Queries can be stored and re-used. However, query creation is fast (<20ns), so this is not mandatory.

Arguments

  • world: The World instance to query.
  • comp_types::Tuple: Components the query filters for and provides access to. Must be a literal tuple like (Position, Velocity).
  • with::Tuple: Additional components the entities must have. Passed as with=(Health,).
  • without::Tuple: Components the entities must not have. Passed as without=(Altitude,).
  • optional::Tuple: Components that are optional in the query. Passed as optional=(Velocity,).
  • exclusive::Bool: Makes the query exclusive in base and with components, can't be combined with without.

Example

for (entities, positions, velocities) in @Query(world, (Position, Velocity))
    for i in eachindex(entities)
        pos = positions[i]
        vel = velocities[i]
        positions[i] = Position(pos.x + vel.dx, pos.y + vel.dy)
    end
end
source
Ark.close!Method
close!(q::Query)

Closes the query and unlocks the world.

Must be called if a query is not fully iterated.

source
Ark.EntitiesType
Entities

Archetype column for entities. Can be iterated and indexed like a Vector.

Used in query iteration.

source

Resources

Resources are singleton-like data structures that appear only once in a World and are not associated to an Entity.

Ark.get_resourceFunction
get_resource(world::World, res_type::Type{T})::T

Get the resource of type T from the world.

source
Ark.has_resourceFunction
has_resource(world::World, res_type::Type{T})::Bool

Check if a resource of type T is in the world.

source
Ark.add_resource!Function
add_resource!(world::World, res::T)::T

Add the given resource to the world. Returns the newly added resource.

source
Ark.set_resource!Function
set_resource!(world::World, res::T)::T

Overwrites an existing resource in the world. Returns the newly overwritten resource.

source
Ark.remove_resource!Function
remove_resource!(world::World, res_type::Type{T})::T

Remove the resource of type T from the world. Returns the removed resource.

source

Batch

An iterator over entities that were created or modified using batch operations. Behaves like a Query and can be used for component initialization.

Ark.BatchType
Batch

A batch iterator. This is returned from batch operations and serves for initializing newly added components.

source
Ark.close!Method
close!(b::Batch)

Closes the batch iterator and unlocks the world.

Must be called if a batch is not fully iterated.

source

Index