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
W
wyrmisis712d 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)
UU
Unknown User712d ago
W
wyrmisis712d 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...
E
enso712d ago
check the preCreate hook example basically you need to do actor.data.update
R
Re4XN712d 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.
W
wyrmisis712d ago
This is what got things working for me, thanks! It's been a minute since I've been able to work in Foundry 😬
LTL
Leo The League Lion712d ago
@wyrmisis gave vote LeaguePoints™ to @re4xn (#60 • 33)
Want results from more Discord servers?
Add your server
More Posts
spitballing about jest mocksok. ok. ok. pure spitballing theory here. All the client side source exists in esmodule form (hopef1.6.1 testing@dnd5e Testing Action Requested but not required. version 1.6.1 is a bugfix version, it's pretty clconversion macro@max.pat did you have a macro or script which could change the old classFeatures data into advancemeAC calc changesI do not believe the data for AC actually changed, only the way it is calculated. evidence: no chang1.6.0 Custom Sheet ChangesLooking at the changes made which are breaking for custom sheets. Gonna make a thread for this.1.6.0 testingCommunity Testing Requests: For those of you interested in helping to QA this branch, I'll put togeHook documentationdnd5e Hook Documentation: https://gitlab.com/foundrynet/dnd5e/-/wikis/HooksTyphonJS Runtime Library v0.0.9 - Release CandidateAlrighty! The TyphonJS Runtime Library (TRL) v0.0.9 is out... In the last 2 weeks+ I have done massi1.6.0 - 82%@dnd5e - **No action Required** The 1.6.0 milestone is at 82% completion https://gitlab.com/foundryadvancement consumptionnow that I brought that up on #progress-reports @calego , could I maybe pester you later for some adAlternative Alignment System D&D5eWow, that is a really powerful way to codify alignment. I like it! Do you have any ideas as to how iconsumables chat data@Zhell > So that's odd. How come consumables work different and all other item types get a non-funcilthid stop dragActually, would it be possible to modify the core function that allows people to click and drag toke1.6.x mid-milestone update@dnd5e - **No Action Required** Milestone 1.6.0 is 69% Complete (_nice_) <https://gitlab.com/foundPlugin ContributorsI'm very proud to be the maintainer of a module (Export Sheet to PDF) that is getting many, many conmore-hooks-itemroll-hooksFor those of you interested in such things, I have hacked together a way to inject hooks into the mipreHooks5eJSON.stringify jankI discovered that the problem is JSON.stringify, not parselayersTIL about CSS Layers. https://developer.mozilla.org/en-US/docs/Web/CSS/@layer ```css @layer utilitiHitDice hooks MRI just added my first merge request 😄 <https://gitlab.com/foundrynet/dnd5e/-/merge_requests/495>