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

Rollable Table Timed Slideshow trouble

I was trying to create a script that runs a rollable table like a slideshow and changes side at a set interval for a set number of times. However even after the log outputs the correct token it doesn't appear to be changing the side of the rollable table itself. Any suggestions? on("chat:message", function(msg) { if (msg.type == "api" && msg.content.startsWith("!simple-flip")) { //example: !simple-flip -NJi3Y87o5vvrApLpR4c 1000 2 var args = msg.content.split(" "); var tokenId = args[1]; var interval = args[2]; var repeat = args[3]; var count = 0; setInterval(function() { var token = getObj("graphic", tokenId); log(token) var currentSide = token.get("currentSide"); var newSide = (currentSide + 1) % token.get("sides").length; token.set("currentSide", newSide); log("interval " + interval + " repeat " + repeat) count++; if (count >= repeat) { clearInterval(this); } }, interval); } });
1671540583

Edited 1671540656
David M.
Pro
API Scripter
It's annoying, but you also have to set the imgsrc property of the token... which means you have to retrieve and parse the pipe-delimited  sides property as well to get the URLs into an array and then lookup with the index from currentside. Finally, you'll need to make sure you use the "thumb" version of the image, which you can get from this function stolen from TheAaron     const getCleanImgsrc = function (imgsrc) {         let parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^\?]*)(\?[^?]+)?$/);             if(parts) {                 return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`);             }         return;     };
1671550711
timmaugh
Forum Champion
API Scripter
I might suggest you come at this asynchronously, as well, just to free up processor cycles. After determining your arguments, you'd just have to decrement your repeat variable, then dispatch another call to this same command line with a delay matching your interval argument. If repeat = 0, don't send the message. That would be roughly (air-coded, and not including David's suggestion): on("chat:message", function(msg) {   if (msg.type == "api" && msg.content.startsWith("!simple-flip")) {  //example: !simple-flip -NJi3Y87o5vvrApLpR4c 1000 2     let args = msg.content.split(/\s+/);     if (args.length !== 4) {         log(`Wrong number of arguments to the simple-flip script. Expected 4 including the handle; received ${args.length}.`);         return;     }     let [apihandle, tokenId, interval, repeat] = args;     // ... manipulate the token here using David's suggestion ...     if (Number(repeat) === 0) return; // if we're at 0, your work is done     setTimeout(() => sendChat('',`!simple-flip ${tokenId} ${interval} ${Number(repeat) - 1}`), interval); }; Structuring things that way, you don't tie up the processor for the length of the slideshow.
Thanks for the tips. I made some brute force modifications and got working. Made a mess of Aaron's beautiful code but beauty is in the eye of the beholder, so they say. function CleanTableUrl(imgsrc) { if (imgsrc !== null){ let parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^\?]*)(\?[^?]+)?(%[^\s]*)?$/); if(parts) { cleanImgUrl = parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`); cleanImgUrl = cleanImgUrl.replace(/%3A/g, ":").replace(/\.png%/g, ".png?"); return cleanImgUrl; } return; } return; }; on("chat:message", function(msg) { if (msg.type == "api" && msg.content.startsWith("!simple-flip")) { //example: !simple-flip <imageID> <milliseconds> <number slide changes> var args = msg.content.split(" "); var tokenId = args[1]; var interval = args[2]; var repeat = args[3]; var count = 0; setInterval(function() { var token = getObj("graphic", tokenId); var currentSide = token.get("currentSide"); var newSide = (currentSide + 1) % token.get("sides").split("|").length; // Get the array of image URLs for the token's sides var sides = token.get("sides").split("|"); //clean the URL to set imgsrc later newSideImgSrc = CleanTableUrl(sides[newSide]) // Set the new side of the token and update the imgsrc property token.set("currentSide", newSide); token.set("imgsrc", newSideImgSrc); count++; if (count >= repeat) { clearInterval(this); } }, interval); } }); I'll add some error exceptions and clean up but otherwise, this is working.
Sorry Timm, I was testing out my code while you posted and didn't see your recommendation. That would be a good idea to run asynchronously. I think this will work for my session today but I try and implement async (and maybe some other features like a force-stop and skipping specified slides).