Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Corrupt state?

Hi there, Is there a simple way to control the "state of the state-object" of a campaign? All of a sudden I've started to loose script information that's supposed to be persistent.
1479318772
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
The state object has a maximum size of 2 mb. If you are using several scripts that store a lot of information in the state, you could be running into this.
!? I didn't know that. Thanks Scott. Any easy way I can check the size of it?
1479324277

Edited 1479324394
Jakob
Sheet Author
API Scripter
Try this, as an inaccurate approximation to the state's size in byte (it will be printed to the console on the API scripts page). function roughSizeOfObject(object) {     'use strict';     var objectList = [];     var recurse = function (value) {         var bytes = 0;         if (typeof value === 'boolean') {             bytes = 4;         } else if (typeof value === 'string') {             bytes = value.length * 2;         } else if (typeof value === 'number') {             bytes = 8;         } else if (typeof value === 'object' &&             objectList.indexOf(value) === -1) {             objectList[objectList.length] = value;             for (let i in value) {                 bytes += 8; // assumed existence overhead                 bytes += recurse(value[i])             }         }         return bytes;     }     return recurse(object); }; on('ready', function () {     'use strict';     log('Size of state: ' + roughSizeOfObject(state) + ' bytes.'); });
Thank you Jacob. Much obliged.
1479326539
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Can also just Jason.stringify it and use .length on the string to get a decent approximation. This reminds me that I need to go back and optimize the state usage in my scripts (especially page navigator) as I was very greedy with the state when I first started.
Scott C. said: Can also just Jason.stringify it and use .length on the string to get a decent approximation. ...that statement went way over my head. :) I love them scripts but I will never understand how they work.
1479329526

Edited 1479330247
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Ravenknight said: Scott C. said: Can also just Jason.stringify it and use .length on the string to get a decent approximation. ...that statement went way over my head. :) I love them scripts but I will never understand how they work. Well, first of all, my phone apparently doesn't recognize code speak, so Jason, should have been JSON. Now, that I'm on my computer, you could do something like this to get a rough estimate of the state's size: on('ready',()=>{ log('-=> The state is roughly ' + JSON.stringify(state).length/1000000 + 'mb'); //Could probably even iterate through each property of the state to get a log of how much space each script is taking up by adding the following: var scripts = _.keys(state); _.each(scripts,(s)=>{ log(' >> Script '+s+' is using roughly '+JSON.stringify(state[s]).length/1000000+' mb); }); }); Turning the object(s) into a string and taking the length works because each character is equal to one byte. The above is untested, but I think it should work, Scott