Last active
November 8, 2020 20:24
-
-
Save danethurber/04fa0b36300503a9e21d23290a5707f1 to your computer and use it in GitHub Desktop.
FoundryVTT macros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage: Summon @token @unique "Token Name" 1 | |
const macroArgs = args; | |
const FLAG_NAME = 'dae'; | |
const sleep = (ms = 0) => new Promise(r => setTimeout(r, ms)); | |
const randomNum = (min, max) => Math.random() * (max - min) + min; | |
async function start() { | |
const [toggleState, tokenId, uniqueId, tokenName, count] = macroArgs; | |
const token = canvas.tokens.get(tokenId); | |
if(!token) throw new Error("No caster token selected"); | |
if (toggleState === 'on') { | |
let ids = [] | |
for (const _i of Array(count).fill()) { | |
const summoned = await summonToken(tokenName, token.center) | |
ids = ids.concat(summoned._id) | |
} | |
await token.setFlag(FLAG_NAME, uniqueId, ids); | |
} else { | |
const ids = await token.getFlag(FLAG_NAME, uniqueId) || []; | |
for (const id of ids) await canvas.scene.deleteEmbeddedEntity("Token", id); | |
await token.unsetFlag(FLAG_NAME, uniqueId); | |
} | |
} | |
async function summonToken(name, center) { | |
const actor = game.actors.getName(name); | |
const data = { | |
...actor.data.token, | |
x: center.x + randomNum(-200, 150), | |
y: center.y + randomNum(-200, 150), | |
hidden: false, | |
disposition: 1, | |
inplace: false | |
} | |
const token = await canvas.scene.createEmbeddedEntity("Token", data) | |
await sleep(10); | |
return token | |
} | |
start().catch(err => { | |
ui.notifications.error(err); | |
console.error(err) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (ui.players.rendered) ui.players.close(); | |
else ui.players.render(true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment