文档
Storable

Storable

⚠️

本页面正在建设中

⚠️

测试功能,可能会有变动

Storable 允许你在游戏状态中存储数据。存储在 Storable 中的数据将与当前游戏状态一起保存和加载。

如果你需要创建操作数据的动作,可以使用 Persistent 抽象。

存储在 Storable 中的数据按命名空间分隔。你可以在不同的命名空间中使用相同的键。 例如,你可以在 player1 命名空间中存储玩家 1 的名字,也可以在 player2 命名空间中存储玩家 2 的名字。

game 命名空间已经定义且可以存储数据,你可以通过实例化一个新的 Namespace 来添加自己的命名空间。

例如,创建一个新的 player1 命名空间,并在其中存储玩家 1 的名字:

type Player1Content = {
    name: string;
};
 
const storable = game.getLiveGame().getStorable();
 
// 初始化一个新的命名空间
const player1namespace = new Namespace<Player1Content>();
storable.addNamespace('player1', player1namespace);
 
// 从命名空间中设置数据
const namespace = storable.getNamespace<Player1Content>('player1');
namespace.set('name', 'John Doe');
 
// 从命名空间中获取数据
const name = namespace.get('name');
console.log(name); // John Doe

公共方法

addNamespace<T extends NameSpaceContent<keyof T>>

  • namespace: Namespace<T>
  • 返回 this

getNamespace<T extends NameSpaceContent<keyof T> = any>

  • key: string - 命名空间的键
  • 返回 Namespace<T>

setNamespace<T extends NameSpaceContent<keyof T> = any>

  • key: string - 命名空间的键
  • namespace: Namespace<T>
  • 返回 this

getNamespaces

  • 返回 { [key: string]: Namespace<any>; }

hasNamespace

  • key: string
  • 返回 boolean

removeNamespace

  • key: string
  • 返回 this

keys

  • 返回 string[]

values

  • 返回 Namespace<any>[]

entries

  • 返回 [string, Namespace<any>][]