John, I'm glad you got it working the way you want. I feel a bit bad because I did a bad job explaining the state object to you, and you aren't actually using it. =/ I'll try it again... state is a variable in the global scope which is automatically created for the API. The Wiki doesn't do a great job of explaining it, but here is where you can find it: <a href="https://wiki.roll20.net/API:Objects#Global_Objects" rel="nofollow">https://wiki.roll20.net/API:Objects#Global_Objects</a> Like everything in Javascript, it is an object. In this case, it is an object that starts out with nothing in it, but anything in it will be persisted in a database across restarts of the API. It is a shared resource, so every API script running in the same campaign uses the same object. This is why you need to be extra careful not to stomp on other scripts toes and pick your names carefully. Additionally, since it is persisted every second or so, you need to be mindful not to put too much into it. You should strive to store the absolute minimum amount of information in it. A while back, there was a script that stored all of the spells from Pathfinder in the state, which caused a big issue. Don't do that. =D How I use state For each of my scripts, I create a property in the state object: state.MyScript = {}; However, you can't write that to the state object every time or you would overwrite whatever you have that you were intending to persist. So, you need to wrap this in a check: if( !_.has(state,'MyScript')) {
state.MyScript = {};
} _.has() is basically the same as Object.hasOwnProperty(). This will see if state already has a property named 'MyScript', and will only add one if it isn't there already. You can also setup your property with properties of its own: if( !_.has(state,'MyScript')) {
state.MyScript = {
hour: 0,
minute: 0,
second: 0
};
} All that is left then is to do this at a point that makes sense. I do this in the on('ready') event, so it only gets run once per restart of the api: on('ready',function(){
if( !_.has(state,'MyScript')) {
state.MyScript = {
hour: 0,
minute: 0,
second: 0
};
}
}); Now in your script you can be assured that you can access these properties with impunity: state.MyScript.minute += 20;
state.MyScript.hour += Math.floor(state.MyScript.minute / 60);
state.MyScript.minute %= 60;
Hopefully that clears up the fallout from my rambling earlier explanation. =D Feel free to PM me if you want to chat on deeper topics!