Array keeps resetting
Hi, I am very new to system development/programming and trying to put together a system for an rpg that does not currently have an implementation by starting from boilerplate. Due to the requirements of games I currently run on foundry, I am using version 11+. I am trying to store player-inputted values in an arrayField of variable length determined by player level, attached to the player characters datamodel. The idea is that players can only store a limited number of values, before they need to replace one. However, every time the Document updates, the array resets to empty. How can I get the array to persist? What am I missing?
The datamodel declaring the array
The loop pushing new values onto the array
I have tried removing the loop, but the results seem to be the same. Sorry if this is a super basic question, but I've been stuck on this for hours
3 Replies
Pretty sure the issue is that you're never writing to the database, so you need to run the
.update
function on the actual document with your modified data to get it to persistOkay, I located the issue - it seems to be with the fact that the way I had implemented it was meant to allow players to directly edit specific indexes within the array via their character sheet. In fact, everything I tried involved changing only single indexes. This is incompatible with the
.update()
function, as explained by the quote below
Working with Arrays: Because the updates work using object key merges, whereas arrays purely work via index, Foundry cannot modify specific indexes inside an array via update, arrays need to be written as a cohesive whole. To update an array in a document's data, you need to edit a copy of the array and then write the new array as a whole with an update.Thanks for pointing me in the right direction! I'll have to come up with some other implementation..
You could modify an index, you just have to give all the array to the update each time