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

REQUEST FOR CODE

I have a bit of a unique request for a Pathfinder adventure of mine. What I need is a Doomsday Timer. Is there a way to make a code that I can start and stop where every 20 min it rolls a D4 automatically and keeps a running total? This may not be possible but I thought I would challenge some of you master codesmiths out there.
Do you require a complete script, or just how to handle the start and stopping of the timer? var doom_timer = 0; // timer var x = 0; // total doom var doom_status = true; var doom_clock = null; function doom_initialize(initial_x){ x = initial_x; doom_status = true; // Run the clock once every 1 seconds doom_clock = setInterval(function(){ if (doom_status){ doom_timer += 1; // count every seconds if (doom_timer >= 10*60){ // 10 minutes doom_timer = 0; // reset count x += Math.floor((Math.random() * 4) + 1); // add 1d4 } } }, 1000); } function doom_stop(){ doom_status = false; } function doom_start(){ doom_status = true; } function doom_count(){ return x; } The above code handles the basics, but it still needs to be tied into the chat API. Call doom_initialize to continue a previous session and start the event. Use doom_start()/doom_stop() to continue/pause the counter. Use doom_count() to get the current value.
Forgive me but what you mean complete script and how do you tie it into chat.I am willing to learn and thank you for taking the time to do this.
1416244744
The Aaron
Roll20 Production Team
API Scripter
He means that what he has provided is the clock portions of a script, but would still require writing the chat interaction and such.
Ah ok So an example would be after the dice roll it would display the message "x" people dead. Thank you very much!
1416249114

Edited 1416249795
so would I make a macro with this doom_start()/doom_stop() or Call doom_initialize to start and pause the timer? Please forgive my ignorance, I am trying to learn.
1416250782
The Aaron
Roll20 Production Team
API Scripter
No, sorry. Let me try to expand on this a bit. Macros are a collection of things you could type in chant manually, but don't want to have to type over and over. API programs are written in Javascript and are installed outside of the VTT in the Campaign Details for the Campaign where you want to use them. The API has access to various details of the campaign, as well as the ability to get notified of various events and perform actions. One of the events the API can subscribe to is someone entering a chat message. It would look like this: on('chat:message', function(msg){ log('got a chat message'); }); What Toke has posted is just some functions that handle the clock portion. You would still need to write the chat portion. It could look something like this: var doom_timer = 0; // timer var x = 0; // total doom var doom_status = true; var doom_clock = null; var doom_interval= (20*60); // 20 minutes function doom_initialize(initial_x){ x = initial_x; doom_status = true; // Run the clock once every 1 seconds doom_clock = setInterval(function(){ if (doom_status){ doom_timer += 1; // count every seconds if (doom_timer >= doom_interval ){ doom_timer = 0; // reset count x += Math.floor((Math.random() * 4) + 1); // add 1d4 } } }, 1000); } function doom_stop(){ doom_status = false; } function doom_start(){ doom_status = true; } function doom_count(){ return x; } on('ready',function(){ on('chat:message',function(msg){ var args; if (msg.type !== 'api') { return; } args = msg.content.split(/\s+/); switch(args[0]) { case '!doom-set': doom_initialize(parseInt(args[1],10) || 0); sendChat('Doomsday Clock','/w gm Set Doomsday Clock to: '+doom_count()); break; case '!doom-start': doom_start(); sendChat('Doomsday Clock','/w gm Started Doomsday Clock.'); break; case '!doom-stop': doom_stop(); sendChat('Doomsday Clock','/w gm Stopped Doomsday Clock.'); break; case '!doom-show': sendChat('Doomsday Clock', '/w gm Doomsday Clock says: '+doom_count()); break; } }); }); I bolded my additions and changes. Note: this is not how I would write this normally, but I'm just throwing it in here so you can see what was missing. You could take this now and place it in the API section and then you would have access to chat commands to set, start ,stop and show the clock. I have no idea if this works, but you should be able to build from here.
so in chat I would type !doom-start to start the timer?
1416255062
The Aaron
Roll20 Production Team
API Scripter
After loading that in the API (and provided it actually works...) then yes.
1416255111
The Aaron
Roll20 Production Team
API Scripter
Note: this does not persist the state of the dooms day clock between sessions, and if the API crashes, you'll have to start over. It should really save info in the state object.
Well I am not getting any error messages but I am not getting any sign its working either. I will keep at it . thank you again!
1416256797
The Aaron
Roll20 Production Team
API Scripter
After: doom_timer += 1; try adding: log('Doomsday Clock: '+doom_timer); that will log the counter each second in the API log (below the script). when you call one of the commands, it should message you in the chat.
1416257811
The Aaron
Roll20 Production Team
API Scripter
You might want to change to this version: var doom_timer = 0; // timer var x = 0; // total doom var doom_status = true; var doom_clock = null; var doom_interval= (20*60); // 20 minutes function doom_initialize(initial_x){ x = initial_x; doom_status = true; // Run the clock once every 1 seconds doom_clock = setInterval(function(){ if (doom_status){ doom_timer += 1; // count every seconds if (doom_timer >= doom_interval){ doom_timer = 0; // reset count x += randomInteger(4); // add 1d4 sendChat('Doomsday Clock','/w gm Doomsday Clock says: '+x); } } }, 1000); } function doom_stop(){ doom_status = false; } function doom_start(){ doom_status = true; } function doom_count(){ return x; } on('ready',function(){ on('chat:message',function(msg){ var args; if (msg.type !== 'api') { return; } args = msg.content.split(/\s+/); switch(args[0]) { case '!doom-set': doom_initialize(parseInt(args[1],10) || 0); sendChat('Doomsday Clock','/w gm Set Doomsday Clock to: '+doom_count()); break; case '!doom-start': doom_start(); sendChat('Doomsday Clock','/w gm Started Doomsday Clock.'); break; case '!doom-stop': doom_stop(); sendChat('Doomsday Clock','/w gm Stopped Doomsday Clock.'); break; case '!doom-show': sendChat('Doomsday Clock', '/w gm Doomsday Clock says: '+doom_count()); break; } }); }); I changed it to use randomInteger() and added a sendChat() for when the clock is incremented.. I should probably just sit down and write this as a full script instead of continuing to piecemeal it here... You do need to call !doom-set once to kick off the clock counter. (which automatically starts the clock the way it is written...)
I know this is a big pain in your butt and I dont blame you if you dont have time for it. It means a lot you going through all this trouble. As always,, thank you.
1416264424
The Aaron
Roll20 Production Team
API Scripter
Hahaha, I'm complaining about me, not you. =D I enjoy doing these scripts. =D I'm just saying I should actually formally solve your problem, instead of tacking on to the other post's partial solution.
Well I was going to leave this out of my adventure but I think it will add a sense of urgency. I have been reading the forums trying to learn how to code and thus far I feel like I have been given a Japanese dictionary .
1416269433
Finderski
Pro
Sheet Author
Compendium Curator
Danger Moose, I know what you mean. :) But I'm slowly getting there; just keep at it and things will finally start to click.
Well I no longer get error messages but when I input !doom-show or !doom-start I do not get any sign that it is working
From the looks of it, nothing will happen until you do !doom-set. That should give you a "Set Doomsday Clock to: 0" whisper, with a "Doomsday Clock says: $x" whisper every 20 minutes after that.
!doom-set gets the same result. no whispers or anything.
If you're not getting the initial whisper, I assume something is crashing. Are you using the last script Aaron posted above? Are you running any other scripts? What shows up in the API console? I generally find it's a good idea to have the API console open in a window on one side of the screen and the campaign open in another window on the other side of the screen so you can watch the API console output while you type commands in the campaign. That can help you track down exactly what triggers the crash.