钩子
在初始化、图片预加载与存档还原节点注入自定义逻辑的游戏钩子
此页面正在建设中
测试版功能,可能会发生变化
钩子是一种向游戏中注入自定义逻辑的方式。它对于创建插件非常有用
game.hooks.hook("init", () => {
console.log("Game initialized");
});公共方法
hook
添加一个钩子到游戏中
const token = game.hooks.hook("init", () => {
console.log("Game initialized");
});
return function cleanup() {
token.cancel();
}key: Hooks- 钩子的键hook: (...value: T[K]) => VoidFunction | void- 返回一个清理函数的钩子函数- 返回
LiveGameEventToken- 参见 LiveGameEventToken
unhook
从游戏中移除一个钩子
const listener = function () { /* ... */ };
game.hooks.hook("init", listener); // 添加一个钩子
game.hooks.unhook("init", listener); // 移除钩子key: Hooks- 钩子的键listener: (...value: T[K]) => VoidFunction | void- 被添加到钩子中的监听函数
钩子列表
init
此钩子在游戏初始化时且播放器渲染前被调用
game.hooks.hook("init", () => {
console.log("let's do something cool here");
});preloadImage
此钩子在预加载图片时被调用。这对于创建图片代理逻辑很有用
src: string- 图片的源地址set: (src: string, options?: RequestInit) => void- 设置图片源地址的函数
game.hooks.hook("preloadImage", (src, set) => {
const newSrc = `https://example.com/proxy?url=${src}`;
set(newSrc, {
credentials: "include",
});
});beforeRestore
此钩子在反序列化游戏状态之前被调用
game.hooks.hook("beforeRestore", () => {
console.log("准备恢复游戏状态");
});afterRestore
此钩子在反序列化游戏状态之后被调用
game.hooks.hook("afterRestore", () => {
console.log("游戏状态恢复成功");
});