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

Some questions about API capability

1553968767
Pantoufle
Pro
Sheet Author
Translator
Hello, I have a character sheet for Abstract Dungeon. In this game you roll your dice at the beggining and then use them during the game. I have a roll button on my sheet but I have to manually input the result in the attribute after the roll. I know we can input attributes with chatsetatr script. I know some scripts listen chat or monitor sheets. My question is : is it difficult to make an api script companion for my character sheet ? What I need is : when click on a button (sometimes repeating sections), get the result from the chat and put it in the right attribute. I don't know anything on JS, wondering if I can get it done easily before getting into it :p Any help appreciated :) Pantoufle
1553972434
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Yes, it's pretty easily doable. The way I'd do it: Add the repeating row id to the outputted roll template. This will require making an attribute to store the row id in, and a sheetworker to assign the row id to the attribute. Make a roll listener in an API script that looks for some pattern in chat (probably the roll template declaration, the row id, and whatever template fields you need Then the API parses that info and extracts the die rolls to then apply them to the sheet. If you want, I can write a proto script that would show the rough format of the script.
1553974960
Pantoufle
Pro
Sheet Author
Translator
I'd love to see that Scott :) please do There are a bunch of attributes (between 13 and 20) to manage with this. Can we make it generic for all atributes or do we need to write a script part for each attribute? Thx for your help
1554001660

Edited 1554001678
GiGs
Pro
Sheet Author
API Scripter
My Universal Sheet Worker senses are tingling... Yes, you can make it generic for all attributes (whether a sheet  worker or script). There are multiple ways to achieve it.
1554021584
Pantoufle
Pro
Sheet Author
Translator
Wait, you can do it with sheet worker?  I read that you cannot make a button that changes an attribute AND display in chat without api. Is it wrong? I'd rather make it through sheet worker for all users :D But I have no clue how to begin :p
1554032380

Edited 1554032388
GiGs
Pro
Sheet Author
API Scripter
No you're correct there, sheet workers cant send stuff to chat. I was talking about making code generic to affect multiple attributes, not the printing to chat.
1554138383

Edited 1554138426
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Pantoufle here's the proto API script I whipped up: on('chat:message',(msg)=>{ log(msg); let msgCopy = _.clone(msg), rollResult,attributeToSet,characterID; const patternMatch = /{{characterid=(.+?)}} {{attribute=(.+?)}} {{roll=\$\[\[(\d+)\]\]}}/,//this will match the rolltemplate text of {{characterid=-ty8U95t}} {{attribute=repeating_sectionName_-J4tlkjasdf90TH-78sR_receptacle}} {{roll=[[xdy]]}} rollTemplateRegex = /yourRollTemplatesHere|separatedByBars/;//What roll templates we want to act on if(msg.rolltemplate && rollTemplateRegex.test(msg.rolltemplate)){ msg.content.replace(patternMatch,(match,cID,attributeToChange,rollIndex)=>{ rollResult = msg.inlinerolls[rollIndex].results.total; characterID = cID; attributeToSet = attributeToChange; let attributeObj = findObjs({ type:'attribute', characterid:characterID, name:attributeToSet }); log(`character ID: ${characterID}`); log(`toSet: ${attributeToSet}`); log(`result: ${rollResult}`); log(attributeObj); if(attributeObj){ attributeObj = attributeObj[0]; attributeObj.set({current:rollResult}); } }); } }); And here's the macro I tested it with (note this was in a character sheet less game where I made an attribute called TestAttribute): &{template:yourRollTemplatesHere} {{characterid=@{character_id}}} {{attribute=TestAttribute}} {{roll=[[1d8]]}} And, now for a (probably pretty bad) explanation of what's going on. First we're looking for specific roll templates to respond to so that we can do a quick yay/nay on whether to do more computationally involved process on the message. Second we then look to see if the message has the information we are looking for and extract it if it does (the .replace). The way I set this up, the script is expecting a roll template field that contains the id of the character to affect, a field with what attribute this roll needs to be stored in and a roll field for the roll itself. Keep in mind that the API's parsing of the message does not care at all about what formatting your roll template has on it or which fields are displayed in chat and which ones aren't; it only cares about the raw text of the chat message. The other thing to keep in mind is that the roll results of a message aren't stored directly in the message content; instead references to them are inserted into the content and then there is an array called "inlinerolls" that is part of the msg object that stores all the details on these rolls. Finally, once we've extracted all this information we find the object that is the attribute we want to modify and set it's current value to the value of the roll. Using this protoscript, you should be able to update any attribute's current value with the value of a roll by passing the character id, the name of the attribute you want to affect, and the roll. This includes everything from repeating_section_-yT78Er43-5yU80_roll_storage to strength . You'll just need to do a simple sheetworker to update repeating items with what attribute gets changed from rolls of that item.
1554157572
Pantoufle
Pro
Sheet Author
Translator
thx a lot Scott, I do not understand everything yet but it is very insightful. 2 questions : is it possible to "hide" the attribute field (not display it in the chat or more likely to hide it from player's eyes :p) ? what does the sheetworker do ? (do you mean if there are other attributes depending on the one we changed ?) Again thx a lot, that saves me so much time :)
1554158503

Edited 1554158610
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Pantoufle said: thx a lot Scott, I do not understand everything yet but it is very insightful. 2 questions : is it possible to "hide" the attribute field (not display it in the chat or more likely to hide it from player's eyes :p) ? Yes, the roll template will only display what you tell it to display in the html definition of the roll template. On the sheet side, the input can be type='hidden' or use css to do display:none. what does the sheetworker do ? (do you mean if there are other attributes depending on the one we changed ?) There is no innate method to get the row id of a repeating item. So, we need to make an attribute that a sheetworker will store the row id (or the way I have that API set up, an actual attribute name). Something like this for the sheetworker (untested): on('change:repeating_sectionname change:repeating_othersection',(event)=>{     let rowID = event.sourceAttribute.replace(/(repeating_.+?_.+_).+/,'$1');     const setObj = {};     setObj[`${rowID}attribute_holder`]=`${rowID}attribute_to_store_in`;     setAttrs(setObj,{silent:true}); });
1554159683
Pantoufle
Pro
Sheet Author
Translator
Oh ok I misread your message about the API and raw text chat message :p I get what you mean for sheetworker ! I'll give these a try when I get time and come back to tell you if I can get it done :) Thx
1554833354
Pantoufle
Pro
Sheet Author
Translator
Hello again, I can't make it work... I think the main issue is being a js noob :p Scott, could you explain a little more the sheet worker ie what do I have to change in the code ? In the const patternMatch part of the script, do I need to enter the pattern of the chat output or only the attributes of the roll template ? (don't know if it's clear)
1554835365
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Pantoufle said: Hello again, I can't make it work... I think the main issue is being a js noob :p Scott, could you explain a little more the sheet worker ie what do I have to change in the code ? In the const patternMatch part of the script, do I need to enter the pattern of the chat output or only the attributes of the roll template ? (don't know if it's clear) Might need some more info on your question about the sheetworker. For the patternMatch though, what goes there depends on what is going to be in your chat message. It doesn't need to match the entire chat message, just some piece of it that is going to be what you are looking for.
1554835814
Pantoufle
Pro
Sheet Author
Translator
Well about the sheetworker : do I need to rename "attribute_holder" or "attribute_to_store_in" ? and if possible could you explain with Testattribute you used in your script example ? :) Thx for the quick answer Scott ! :)
1554841003
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Ah, ok. So in the sheetworker setting attribute holder to contain rowID and attribute_to_store_in is just setting the attribute_holder attribute's value to be something like "repeating_section_-Ju780asdfakj25ljdjooi52ioh08h_attribute_to_store_in" or "attribute_to_store_in" depending on whether it's in a repeating section or not so that when that is output to chat, the script can then scrape the attribute name it needs to look for, which speeds up the API script's functions. Attribute_holder and attribute_to_store_in could be called anything you want; I just used those names to denote what their purpose was. In the API script and chat output example, TestAttribute was used instead of attribute_to_store_in .