PROJECT LINCTRA

鏈次元(內測版)

self參數詳解

self 用來取得目前執行的資源資訊和自訂屬性。例如掛畫要顯示哪張圖片、錢包目前使用哪個地址,都可以從此次呼叫提供的 self 資料中讀取。

self 用於讀取目前資源的資訊和自訂屬性,不是寫入或保存資料的接口。若要更新物品資料,需要使用主系統提供的相應接口。

哪些函式可以使用 self

Ability Action、Object Action、On Frame Action 和 Assets Service 都可以使用 self。它是函式的第一個參數,由主系統傳入,不需要開發者自己建立。

// Ability Action
export function abilityAction(self, actor, dialog, host) {}

// Object Action
export function objectAction(self, selection, host) {}

// On Frame Action
export function onFrame(self, render, host) {}

// Assets Service
export async function service(self, input, host) {}

上面是參數位置示意,函式名稱可以按功能命名,再由 Catalog 對應到實際的匯出函式。不使用的參數也應保留,讓主系統能按規定的接口呼叫。

self 的所有屬性

self 的固定屬性如下。slotRef 只在部分呼叫中提供,也可能為 null;custom 裡面的欄位則取決於資源及此次呼叫提供的資料。

屬性類型用途與樣例
assetobject目前執行的資源識別資訊,包含下面四個屬性。
asset.typestring資源類型,例如 'object' 或 'ability',不是 Catalog 中的 category。
asset.idnumber此次呼叫提供的 ID,例如場景掛畫的 12;未提供時通常為 0,不能一律當作物品唯一編號。
asset.packIdstring資源包 ID,例如 'biglazycat'。
asset.keystring包內的資源 key,例如 'small-customize-painting'。
slotRefobject/null部分呼叫提供的物品槽位資訊,例如 { scope: 'inventory', index: 2 }。沒有可用槽位時可以是 null,場景物件等入口則不提供此屬性。
slotRef.scopestring槽位所屬範圍,例如背包 'inventory'、主手快捷欄 'hotbarMain' 或副手快捷欄 'hotbarOffhand'。
slotRef.indexnumber從 0 開始的槽位索引。例如 2 表示該範圍中的第三個槽位。
customobject此次呼叫提供的自訂資料,沒有資料時為 {}。
custom.<欄位名稱>依欄位而定例如掛畫的 custom.image、錢包的 custom.currentAddress。沒有所有資源共用的固定欄位清單。

packId 和 key 合起來識別一種資源。例如兩幅小型自定義掛畫可以有相同的 packId 和 key,但各自的 custom.image 不同,因此顯示不同圖片。

例子一:掛畫從 self 讀取自訂圖片

以 biglazycat 的小型自定義掛畫為例。下面是一次場景繪製時的 self 資料樣例,圖片地址僅作示意:

const paintingSelfExample = {
  asset: {
    type: 'object',
    id: 12,
    packId: 'biglazycat',
    key: 'small-customize-painting'
  },
  custom: {
    image: 'https://example.com/painting.jpg'
  }
};

asset 說明正在繪製哪個資源,custom.image 則決定這幅掛畫要展示的圖片。場景掛畫不需要背包槽位,所以這份資料沒有 slotRef。

本章的程式只示範如何讀取 self,不需要加入圖片載入或繪製流程。用上面的資料作為 self,就可以直接取得:

const self = paintingSelfExample;

const packId = self.asset.packId;   // 'biglazycat'
const key = self.asset.key;         // 'small-customize-painting'
const imageUrl = self.custom.image; // 'https://example.com/painting.jpg'

關鍵就是 self.custom.image。它取得這幅掛畫設定的圖片地址;換成另一幅掛畫的 self,就能讀到另一張圖片的地址。如何把圖片畫出來,見On Frame Action介紹。

例子二:讀取錢包物品的地址與槽位

以下是已有 MetaMask 錢包物品的 self 資料樣例。假設呼叫方提供了它在背包中的槽位,以及目前的錢包自訂資料:

const walletSelfExample = {
  asset: {
    type: 'object',
    id: 0,
    packId: 'basepack',
    key: 'wallet-metamask'
  },
  slotRef: {
    scope: 'inventory',
    index: 2
  },
  custom: {
    linkStatus: 'linked',
    currentAddress: '0x1111111111111111111111111111111111111111',
    addresses: [{
      value: '0x1111111111111111111111111111111111111111',
      chains: ['Ethereum', 'Polygon']
    }]
  }
};

同樣只需要幾句程式,就能讀取錢包地址、連線狀態和槽位:

const self = walletSelfExample;

const walletKey = self.asset.key;
const currentAddress = self.custom.currentAddress;
const linkStatus = self.custom.linkStatus;

if (self.slotRef) {
  const scope = self.slotRef.scope; // 'inventory',表示背包
  const index = self.slotRef.index; // 2,表示第三個槽位
}

walletKey 取得錢包資源的 key,currentAddress 取得目前地址,linkStatus 取得連線狀態。slotRef 則表示物品位置,可能為 null,所以先確認存在再讀取。以上只示範資料讀取,不包含連線或餘額查詢等服務實作。

使用時記住這幾點

  • asset 是識別資訊,不是完整 Catalog。不要直接從 self.asset 讀取 label、category 或 wallet 配置。
  • custom 的欄位依資源而定。使用圖片、地址等資料前,先確認有沒有值。
  • self 是讀取資料的入口,不是保存資料的接口。要更新物品資料,請使用主系統提供的相應接口。