View Source Ecspanse.Resource behaviour (ECSpanse v0.9.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]

There are two ways of providing the resources with their initial state:

  1. 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
  2. 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:

Note

Resources can be created, updated or deleted only from synchronous systems.

Summary

Types

A resource_spec is the definition required to create a resource.

Callbacks

Optional callback to validate the resource state.

Functions

Utility function. Returns all the resources and their state.

Types

@type resource_spec() ::
  (resource_module :: module())
  | {resource_module :: module(), initial_state :: keyword()}

A resource_spec is the definition required to create a resource.

Examples

  Demo.Resources.Lobby
  {Demo.Resources.Lobby, [max_players: 50]}

Callbacks

Link to this callback

validate(resource)

View Source (optional)
@callback validate(resource :: struct()) :: :ok | {:error, any()}

Optional callback to validate the resource state.

See Ecspanse.Component.validate/1 for more details.

Functions

@spec debug() :: [{resource_module :: module(), resource_state :: struct()}]

Utility function. Returns all the resources and their state.

This function is intended for use only in testing and development environments.