View Source Ecspanse.Resource behaviour (ECSpanse v0.10.0)
Resources are global components that don't belong to any entity.
They are best used for configuration, global state, statistics, etc.
Resources are defined by invoking use Ecspanse.Resource
in their module definition.
Options
:state
- a list with all the resource state struct keys and their initial values (if any). For example:[:player_count, max_players: 100]
:export_filter
- :none | :resource - indicates if the resource should be exported. Defaults to:none
. SeeEcspanse.Snapshot
for details.:none
- no filter. The resource will be exported.:resource
- the resource will not be exported.
There are two ways of providing the resources with their initial state:
At compile time, when invoking the
use Ecspanse.Resource
, by providing the:state
option.defmodule Demo.Resources.PlayerCount do use Ecspanse.Resource, state: [player_count: 0, max_players: 100] end
At runtime when creating the resources from specs:
t:Ecspanse.Resource.resource_spec()
Ecspanse.Command.insert_resource!({Demo.Resources.Lobby, [max_players: 50]})
There are some special resources that are created automatically by the framework:
Ecspanse.Resource.FPS
- tracks the frames per second.
Note
Resources can be created, updated or deleted only from synchronous systems.
Summary
Implemented Callbacks
Fetches the resource. It has the same functionality as Ecspanse.Query.fetch_resource/1
,
but it may be more convenient to use in some cases.
Types
A resource_spec
is the definition required to create a resource.
Callbacks
Optional callback to validate the resource state.
Implemented Callbacks
@callback fetch() :: {:ok, component :: struct()} | {:error, :not_found}
Fetches the resource. It has the same functionality as Ecspanse.Query.fetch_resource/1
,
but it may be more convenient to use in some cases.
Implemented Callback
This callback is implemented by the library and can be used as such.
Examples:
{:ok, %Demo.Resources.Config{} = resource} = Demo.Resources.Config.fetch()
# it's the same as:
{:ok, %Demo.Resources.Config{} = resource} = Ecspanse.Query.fetch_resource(Demo.Resources.Config)
Types
Callbacks
Optional callback to validate the resource state.
See Ecspanse.Component.validate/1
for more details.
Functions
Utility function. Returns all the resources and their state.
This function is intended for use only in testing and development environments.