PROJECT LINCTRA

鏈次元(內測版)

selection參數詳解

selection 用來讀取玩家目前選中的主手、副手物品,以及正在操作的物品槽位。例如玩家拿著錢包和收款地址操作 ATM,就可以從 selection 了解兩隻手目前選中了什麼。

self 表示正在操作的資源,selection 表示當前的物品選擇。操作場景中的 ATM 時,self 是 ATM,selection 裡的主手、副手則可能是錢包和地址物品。

哪些函式可以使用 selection

selection 用於 Object Action,是函式的第二個參數,由主系統傳入。場景物件操作和背包、快捷欄的物品操作都可以使用:

export function objectAction(self, selection, host) {}

函式名稱可以按功能命名,再由 Catalog 對應到實際的匯出函式。Ability Action、On Frame Action 和 Assets Service 的第二個參數不是 selection,不要混用。

selection 的所有屬性

屬性類型用途與樣例
mainHandItemobject/null目前選中的主手物品,沒有有效物品時為 null。
offHandItemobject/null目前選中的副手物品,沒有有效物品時為 null。注意名稱中的大寫 H。
activeSlotRefstring正在操作的物品槽位,例如 'inventory:2' 表示背包第三格。操作場景物件或沒有可用槽位時為 ''。

mainHandItem 和 offHandItem 具有相同的屬性。下表用 物品 代表其中任意一個,例如 物品.itemKey 可以是 selection.mainHandItem.itemKey 或 selection.offHandItem.itemKey:

屬性類型用途與樣例
物品.packIdstring物品所屬的資源包,例如 'basepack'。
物品.itemKeystring包內的物品資源 key,例如 'wallet-metamask'。這裡叫 itemKey,不是 self.asset 中的 key。
物品.runtimeKeystring包含資源包的識別字串,例如 'basepack::wallet-metamask';沒有 packId 時只保留 itemKey。
物品.slotRefstring物品資料中提供的槽位字串,未提供時為 '',不一定有值。它不是 self.slotRef 那種含 scope、index 的物件。
物品.countnumber此次資料提供的數量,未提供時預設為 1。不應用它作為扣除物品或交易的數量依據。
物品.customobject物品的自訂資料,沒有資料時為 {}。
物品.custom.<欄位名稱>依欄位而定例如錢包的 currentAddress、地址物品的 address,依資源而定,沒有共用的固定欄位清單。

例子一:讀取主手錢包和副手地址

假設玩家主手選中 MetaMask 錢包、副手選中 EVM 地址,正在操作場景中的 ATM。下面是 selection 資料樣例,地址僅作示意:

const selectionExample = {
  mainHandItem: {
    packId: 'basepack',
    itemKey: 'wallet-metamask',
    runtimeKey: 'basepack::wallet-metamask',
    slotRef: '',
    count: 1,
    custom: {
      currentAddress: '0x1111111111111111111111111111111111111111'
    }
  },
  offHandItem: {
    packId: 'basepack',
    itemKey: 'evm-address',
    runtimeKey: 'basepack::evm-address',
    slotRef: '',
    count: 1,
    custom: {
      address: '0x2222222222222222222222222222222222222222'
    }
  },
  activeSlotRef: ''
};

只需要幾句程式,就可以讀取兩隻手的物品資訊:

const selection = selectionExample;

if (selection.mainHandItem) {
  const walletKey = selection.mainHandItem.itemKey;
  const currentAddress = selection.mainHandItem.custom.currentAddress;
}

if (selection.offHandItem) {
  const addressKey = selection.offHandItem.itemKey;
  const address = selection.offHandItem.custom.address;
}

walletKey 取得主手物品的 key,currentAddress 取得這個錢包設定的地址;addressKey 和 address 則讀取副手地址物品的資料。因為兩隻手都可能沒有物品,所以先確認物品存在再讀取。

本例操作的是場景 ATM,不是某個背包槽位,因此 activeSlotRef 為空字串。這不代表主手或副手沒有物品。以上只示範資料讀取,不包含轉賬流程。

例子二:讀取正在操作的物品槽位

假設玩家正在操作背包第三格的物品,而主手、副手都沒有選中有效物品:

const inventorySelectionExample = {
  mainHandItem: null,
  offHandItem: null,
  activeSlotRef: 'inventory:2'
};

讀取 activeSlotRef 就能取得這次操作的槽位:

const selection = inventorySelectionExample;

const activeSlot = selection.activeSlotRef; // 'inventory:2'
const hasMainHandItem = selection.mainHandItem !== null; // false
const hasOffHandItem = selection.offHandItem !== null;   // false

inventory 表示背包,2 是從 0 開始的索引,所以指第三格。如果操作的是快捷欄,槽位字串也可以是 'hotbarMain:0' 或 'hotbarOffhand:0'。

正在操作的物品不一定就是主手或副手物品。activeSlotRef 表示此次操作的位置,mainHandItem 和 offHandItem 仍表示玩家當前的主手、副手選擇。

使用時記住這幾點

  • 先確認物品存在。mainHandItem 和 offHandItem 都可能為 null;custom 中的欄位也要按物品類型確認。
  • selection 是物品資料,不是服務接口。讀到錢包物品不代表可以直接在它上面呼叫連線或轉賬函式。
  • selection 用於讀取,不是寫入或保存資料的接口。要切換選擇、更新或扣除物品,請使用主系統提供的相應接口。