Skip to content

Instantly share code, notes, and snippets.

@hobione2k
Last active March 22, 2024 20:15
Show Gist options
  • Save hobione2k/c9f7e120a6f993432e3d14c82acb4cb8 to your computer and use it in GitHub Desktop.
Save hobione2k/c9f7e120a6f993432e3d14c82acb4cb8 to your computer and use it in GitHub Desktop.
ClusterScript Itemにsendするやつ
const sendAll = (items, type, value) => {
if (items == null) return;
items = items.concat();
while (items.length > 0) { // 送信先があれば実行
const item = items.shift(); // 取り出す
try {
item.send(type, value);
} catch (e) {
items.unshift(item); // エラーになったら戻して次回に回す
return items;
}
}
return [];
};
@hobione2k
Copy link
Author

hobione2k commented Mar 22, 2024

近くのItemに特定のデータを送るサンプル

const dedupItem = (items) =>
  items.filter((i, n) => items.findIndex((i2) => i2.id == i.id) === n);  // ItemHandleの重複を解消

$.onInteract((player) => {
  const items = $.getItemsNear($.getPosition(), 10);          // 近くのItemを取得
  $.state.items = dedupItem($.state.items.concat(items));  // 送信先itemとして追加(重複も排除)
});

$.onUpdate((deltaTime) => {
  $.state.items = sendAll($.state.items, "SampleSendData", "何かのデータ");
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment