preUpdateHook question

Hey all, question for anyone who has a sec. I'm trying to use preCreateActor to add rolled values to a new actor, but I don't seem to be able to get the values to apply. Anyone mind having a look? I'll throw my code in a thread so it doesn't clutter up the channel
8 Replies
wyrmisis
wyrmisis•3y ago
Hooks.on('preCreateActor', async (actor) => {
const formula = game.settings.get('foundry-chromatic-dungeons', 'autoroll-pc-stats');

if (actor.type !== 'pc') return;
if (formula === 'manual') return;

const attributeKeys = ['str', 'int', 'wis', 'dex', 'con', 'cha'];
const rolls = new Array(
new Roll(formula),
new Roll(formula),
new Roll(formula),
new Roll(formula),
new Roll(formula),
new Roll(formula)
);

await Promise.all(rolls.map(
roll => roll.roll()
));

const totals = rolls.map(roll => roll.total);
const attributes = attributeKeys.reduce((prev, curr, i) => ({
...prev,
[curr]: totals[i]
}), {});

console.info(attributes);

await actor.update({ 'data.attributes': attributes });
});
Hooks.on('preCreateActor', async (actor) => {
const formula = game.settings.get('foundry-chromatic-dungeons', 'autoroll-pc-stats');

if (actor.type !== 'pc') return;
if (formula === 'manual') return;

const attributeKeys = ['str', 'int', 'wis', 'dex', 'con', 'cha'];
const rolls = new Array(
new Roll(formula),
new Roll(formula),
new Roll(formula),
new Roll(formula),
new Roll(formula),
new Roll(formula)
);

await Promise.all(rolls.map(
roll => roll.roll()
));

const totals = rolls.map(roll => roll.total);
const attributes = attributeKeys.reduce((prev, curr, i) => ({
...prev,
[curr]: totals[i]
}), {});

console.info(attributes);

await actor.update({ 'data.attributes': attributes });
});
The console.info has the following:
{
"str": 12,
"int": 8,
"wis": 9,
"dex": 10,
"con": 8,
"cha": 10
}
{
"str": 12,
"int": 8,
"wis": 9,
"dex": 10,
"con": 8,
"cha": 10
}
The error I'm getting when trying to do the update:
Uncaught (in promise) Error: You must provide an _id for every object in the update data Array.
at ClientDatabaseBackend._preUpdateDocumentArray (foundry.js:10185:32)
Uncaught (in promise) Error: You must provide an _id for every object in the update data Array.
at ClientDatabaseBackend._preUpdateDocumentArray (foundry.js:10185:32)
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
wyrmisis
wyrmisis•3y ago
Nope! It's a system I've been building, https://github.com/wyrmisis/foundry-chromatic-dungeons
GitHub
GitHub - wyrmisis/foundry-chromatic-dungeons: An implementation of ...
An implementation of the OSR TTRPG system, Chromatic Dungeons (https://izegrimcreations.com/gaming-related) - GitHub - wyrmisis/foundry-chromatic-dungeons: An implementation of the OSR TTRPG system...
enso
enso•3y ago
check the preCreate hook example basically you need to do actor.data.update
Re4XN
Re4XN•3y ago
A working example: https://github.com/fh-fvtt/zweihander/blob/9a55f94a8682a8443b5d85f175fb66855e5c7b39/module/actor/entity/pc.js#L121 Do not if you have the possibility, update data inside hooks. It can lead to weird behavior. Do it inside the respective methods instead.
wyrmisis
wyrmisis•3y ago
This is what got things working for me, thanks! It's been a minute since I've been able to work in Foundry 😬
Leo The League Lion
Leo The League Lion•3y ago
@wyrmisis gave vote LeaguePoints™ to @re4xn (#60 • 33)
Want results from more Discord servers?
Add your server