Advertisement Create a free account

When creating an API script, what is the best way to keep a recurring event game to game?

For example: I am about to start running a campaign that will be starting this Friday. The campaign will have time elements to it, and I would like to create a script that will keep track of the day, and the event. I see that after some inactivity, the API spins down, and doesn't work on the game, so a constant variable won't work, as it will most likely reset upon each time, so this leads me to a couple ideas. I can create a handout in my game called "Calendar" and have the API script rewrite the day everytime it progresses. I am wanting to be able to place entries into the script that will then alert me when that day happens.  Say it is currently day 6, and I want to place an event to happen on day 9, so I use a command such as !calendar-event 9 Something happens Can you give me any tips as to implementing this? Is this a thing that can be done?  Thanks for your help! Jeremiah
1547076855
The Aaron
Forum Champion
API Scripter
I’d suggest the state object, which is persisted between games:  https://wiki.roll20.net/API:Objects#state
The Aaron said: I’d suggest the state object, which is persisted between games:  https://wiki.roll20.net/API:Objects#state How to you declare state? Is it just state [name of variable] = information?
1547082094

Edited 1547082891
The Aaron
Forum Champion
API Scripter
There’s a full working example in that link. It is a global variable containing an object. You can access it as you would any other object, either with object notation: state.myStuff = {}; Or array notation: state["myStuff"] = {}; Edit: If you’re talking about literally declaring the state global variable, you don’t because it is declared in the global scope by the sandbox. You can imagine that somewhere in the API startup code there’s a line like “let state = await loadStateForGame(game_id);” or some such. 
1547090040
GiGs
Pro
Sheet Author
If you're uncomfortable with using the state, you can also use a character sheet attribute. A lot of people have a character sheet called Macros,and store their global Abilities in that. Once you have a character sheet set aside for utility purposes like this, you can use it for other things - like storing data you want to persist between sessions. One downside of the state is its unique to campaigns. So if you ever copy the campaign, and start playing in the copy, you wont have the old state object. But outside of niche situations like that, the state approach is the superior approach , and certainly the most powerful once you're familiar with it. But if you are just holding a few small pieces of information, and are relatively new at javascript, storing the data in a character sheet attribute is fine, if less elegant - its is simple, and uses techniques you are likely already familiar with.
1547090462

Edited 1547145770
GiGs
Pro
Sheet Author
To add to Aaron's post, you create an object under the state object, and use that to store data in. So, you might start with state.jeremiahcampaign = {}; then add the calendar date inside that state.jeremiahcampaign.day = 6; You might want to store day, month, and year, just a total number of days. Dates can get complicated. That could lead to  state.jeremiahcampaign.date = {day: 1, month: 1, year: 1103}; and then you'd likely need a function to  add days, check if it increases the month and/or year, so if you are doing something simple, just using the day count is simple. That said, there are already at least two scripts that manage calendars (google should find the links): Kirsty's Multi-world Calendar Robin Kuiper's LazyCalandar It might be easier to check them out and see if they work for you, and then write a little script to check the current date maintained by those and notify of you events.
1547142036
Jakob
Pro
Sheet Author
API Scripter
There are some caveats, state is limited to around 1 MB afaik, and can only store objects which are serialisable as JSON.