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 .
×

While Loops or Some Iterative Process?

1596652135

Edited 1596652191
Desire: I would like to be able to select a token and have a macro button that, when clicked, references a token's attributes [NumberAttacks] and [AttackMod], then rolls a d20 for the value of [NumberAttacks], adds [AttackMod] to the value each time, and prints out each value.  Basically, I'd like something like this Python/Roll20 psudo-code below: x = @{selected|NumberAttacks} i = 1 while i <= x:   /r 1d20 + @{selected|AttackMod}   i += 1 Is there any way of doing something like that?  I know enough Python and C++ to get myself in trouble, but I feel like I'm floundering in the dark when it comes to API and macros. Thanks in advance for any help you can give!
1596654183
The Aaron
Roll20 Production Team
API Scripter
It can definitely be done with the API. Select 1 or more tokens, run: !roll-attacks script: on('ready',()=>{ const range = (n)=>[...Array(n+1).keys()].slice(1); on('chat:message',msg=>{ if('api'===msg.type && /^!roll-attacks(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){ (msg.selected || []) .map(o=>getObj('graphic',o._id)) .filter(g=>undefined !== g) .forEach(t=>{ let cid = t.get('represents'); if(cid){ let num = (parseInt(getAttrByName(cid, 'NumberAttacks'))||1); let mod = (parseFloat(getAttrByName(cid,'AttackMod'))||0); let roll = range(num).map(n=>`<div><b>Attack ${n}:</b> [[1d20+${mod}]]</div>`).join(''); sendChat(t.get('name'),roll); } }); } }); });
The Aaron, man that's fantastic.  Thank you!  I forgot to mention that I would like my PC's to be able to use this macro.  I just deleted the playerIsGM() function, and selected "Visible to all players" in the macro settings.  Is there a more elegant way than that?  On a side note, what resources did you use to become an API god, and where can I get them?  Thanks again!
1596662475
The Aaron
Roll20 Production Team
API Scripter
Nope, that's perfect. =D I used the forum and tenacity. =D. I can drop you some good links later if you're interested. 
The Aaron, that would be great, if you have the time.  I appreciate all your help!
1596671593
The Aaron
Roll20 Production Team
API Scripter
Nick O. has some great starter videos here:&nbsp; <a href="https://www.youtube.com/watch?v=S_D0QrIE1Zo&amp;list=PLqhGF2nCu23kPeaUV_zIGwPRBZYUlcTGv" rel="nofollow">https://www.youtube.com/watch?v=S_D0QrIE1Zo&amp;list=PLqhGF2nCu23kPeaUV_zIGwPRBZYUlcTGv</a> Here are some forum threads that I think have good starting discussions: <a href="https://app.roll20.net/forum/post/6237754/slug%7D" rel="nofollow">https://app.roll20.net/forum/post/6237754/slug%7D</a> <a href="https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1" rel="nofollow">https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1</a> <a href="https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern" rel="nofollow">https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern</a>
The Aaron, thanks man.&nbsp; I'm going to start looking in to them tomorrow.