Cauldron2D.Save (Cauldron2D v0.1.3)

Copy Markdown View Source

Saved games: a game state kept on disk under a slot name, and read back to start a Cauldron2D.World from with its :state option.

:ok = Cauldron2D.Save.write(:my_game, "quick", Cauldron2D.World.snapshot(world))
{:ok, state} = Cauldron2D.Save.read(:my_game, "quick")
{:ok, world} = Cauldron2D.World.start_link(game: MyGame, state: state)

Slots live at Cauldron2D.Paths.data(game, ["saves", slot <> ".save"]). A slot name is one path segment; anything with a separator or .. is {:error, :bad_slot}. The state is any term.

Summary

Functions

Forget slot.

Every slot, oldest first, with when it was written.

The file a slot is kept in.

The state kept under slot.

Keep state under slot, replacing what was there.

Types

slot()

@type slot() :: String.t()

Functions

delete(game, slot)

@spec delete(Cauldron2D.Paths.game(), slot()) :: :ok | {:error, term()}

Forget slot.

list(game)

@spec list(Cauldron2D.Paths.game()) :: [%{slot: slot(), at: DateTime.t()}]

Every slot, oldest first, with when it was written.

path(game, slot)

@spec path(Cauldron2D.Paths.game(), slot()) :: Path.t()

The file a slot is kept in.

read(game, slot)

@spec read(Cauldron2D.Paths.game(), slot()) :: {:ok, term()} | {:error, term()}

The state kept under slot.

write(game, slot, state)

@spec write(Cauldron2D.Paths.game(), slot(), term()) :: :ok | {:error, term()}

Keep state under slot, replacing what was there.