Adding a background to an actor bug #54
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I added a class into a character in foundry, and when it synced, dragonroll synced to foundry a background that does not exist. And then foundry gets an error when I add a background because of that. Can you fix it?
I'll start by exploring how Foundry sync maps actor fields, especially classes and backgrounds.
Now I have decisive evidence. Let me verify the key Foundry semantics once more and then implement:
Root cause confirmed. Now the backend mapper fix:
Now the Foundry module side — the fields that hold item references:
Now the tests:
Tests pass — now let me verify they actually catch the bug by temporarily reverting the source changes:
Pre-existing failures are identical before and after (23 in both runs). Let me review my final diff:
Fixed
Root cause. D&D 5e does not store an actor's background as a name:
system.details.backgroundis aLocalDocumentField— it holds the id of the embedded background item on that actor (same forsystem.details.race; verified against the dnd5e system's own schema in v3, v4, v5 and v6). Dragonroll holds names, and both sides of the sync confused the two:backend/src/services/actor-foundry.jsreadsystem.details.backgroundas if it were a name, so the Foundry item id (aB3cD9eF1gH2iJ4k) landed in the campaign's Background field.dragonroll-foundry/scripts/api.jscreated abackgrounditem literally named after that id — the background that does not exist — and wrote the name intosystem.details.background, leaving a reference that resolves to nothing. Adding a real background in Foundry then failed.Any actor change triggers that round trip, which is why adding a class is what surfaced it.
Changes
backend/src/services/actor-foundry.js: a newdetailsName()reads thosesystem.detailsentries through the actor's items — an id resolves to that item's name, plain text (older system releases) is kept as the name, and an id that resolves to nothing is dropped instead of becoming a name. Applied tobackground,className,subclassandspecies.dragonroll-foundry/scripts/api.js: a name is never written into those reference fields.embeddedDetailReferenceData()pointssystem.details.backgroundat the background item the sync manages (and clears it when the campaign holds no background, which also repairs actors an earlier sync corrupted), and pointssystem.details.raceat the world's own race item when it carries the species name — otherwise that reference is left to the GM. Run after the embedded items are reconciled, and right after a character is created.Tests —
tests/foundry-actor-details.test.js(12 new,node --test): the backend mapper resolving references, dropping a dead id, keeping plain text; and the module applying a campaign character to a stubbed Foundry world (background item referenced by id, the bogus item an earlier sync invented removed, a cleared background leaving no dangling reference, a species never written as a name, and a character created from Dragonroll referencing the item it was created with). 9 of them fail on the previous code.Full suite:
145 pass / 23 fail— the 23 failures (map pins, map tokens, item info window, item-folder-sync) are pre-existing; I confirmed the same list and count on an unmodified checkout.Implementation is ready for review: #60
The automated implementation stopped because of an internal error. Please check the worker logs for details.