World is a GenServer that manages the lifecycle of entities at runtime. A world is responsible for creating, cloning, and destroying entities from its own context. It also manages the event routing logic for the components an entity has, ensuring that events are dispatched and handled correctly.
Summary
Functions
Same as Genesis.Context.all/2 but scoped to the world.
Same as Genesis.Context.at_least/4 but scoped to the world.
Same as Genesis.Context.at_most/4 but scoped to the world.
Same as Genesis.Context.between/5 but scoped to the world.
Returns a specification to start this module under a supervisor.
Clones an entity with all its components.
See Genesis.Manager.clone/2 for more details.
Returns the underlying world context.
Executes a function with the world context synchronously.
Creates a new entity in the world.
Creates a new entity from a prefab. The prefab must be registered before it can be used. Optionally, you can provide overrides to modify component properties.
Same as create/1 but raises on error.
Returns the created entity on success.
Same as create/3 but raises on error.
Returns the created entity on success.
Destroys an entity from the world.
Returns :ok if the entity was successfully destroyed, or :noop if the entity doesn't exist.
See Genesis.Context.destroy/2 for more details.
Same as Genesis.Context.exists?/2 but scoped to the world.
Same as fetch/2 but scoped to the world.
List all entities in the world with their respective components.
Same as Genesis.Context.match/3 but scoped to the world.
Sends an event to an entity.
Starts the world process.
Same as GenServer.start_link/3.
Functions
Same as Genesis.Context.all/2 but scoped to the world.
Same as Genesis.Context.at_least/4 but scoped to the world.
Same as Genesis.Context.at_most/4 but scoped to the world.
Same as Genesis.Context.between/5 but scoped to the world.
Returns a specification to start this module under a supervisor.
See Supervisor.
Clones an entity with all its components.
See Genesis.Manager.clone/2 for more details.
Returns the underlying world context.
Since both writes and reads in a world are always synchronous, this function creates an escape hatch so users can execute operations using the underlying context. All read operations executed directly in the context should be considered dirty by default.
Example
context = World.context(world)
Genesis.Context.search(context, ...)
Executes a function with the world context synchronously.
Differently from context/1, this allows for safe context operations
since it's executed synchronously within the world server itself. The function
must return a value that will be replied back to the caller.
Example
World.context(world, fn ctx ->
Genesis.Context.emplace(ctx, entity, component)
end)
Creates a new entity in the world.
Examples
Genesis.World.create(world)
#=> {:ok, entity}
Creates a new entity from a prefab. The prefab must be registered before it can be used. Optionally, you can provide overrides to modify component properties.
Examples
# Using a prefab name
Genesis.World.create(world, "Player")
#=> {:ok, entity}
# Or using a prefab entity directly
Genesis.World.create(world, prefab)
#=> {:ok, entity}
Genesis.World.create(world, "Player", %{"health" => %{current: 50}})
#=> {:ok, entity}
Same as create/1 but raises on error.
Returns the created entity on success.
Same as create/3 but raises on error.
Returns the created entity on success.
Destroys an entity from the world.
Returns :ok if the entity was successfully destroyed, or :noop if the entity doesn't exist.
See Genesis.Context.destroy/2 for more details.
Same as Genesis.Context.exists?/2 but scoped to the world.
Same as fetch/2 but scoped to the world.
List all entities in the world with their respective components.
Options
:format_as- Specifies how to represent the components of each entity. Can be:list(default) to return a list of component structs, or:mapto return a map where keys are component aliases and values are component structs.
Examples
Genesis.World.list(world, format_as: :list)
#=> [{entity, [%Health{current: 100}]}]
Genesis.World.list(world, format_as: :map)
#=> [{entity, %{"health" => %Health{current: 100}}}]
Same as Genesis.Context.match/3 but scoped to the world.
Sends an event to an entity.
Examples
Genesis.World.send(world, entity, :move, %{direction: :north})
#=> :ok
Genesis.World.send(world, entity, :damage, %{amount: 10})
#=> :ok
Starts the world process.
Same as GenServer.start_link/3.