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

Need a bit of help getting started with the API

I have a basic working knowledge of Javascript and would like to use the API for some conditionals, however I'm stuck on a few basic points- Namely: 1. How to properly set up a function in the API so that it can be called later (can you just go straight in with    function myFunction() {do stuff}  ? Or are there some other requirements I'm missing?) 2. How to call said function either in game or via a macro (I understand that a macro cannot call a function directly, but by doing something with chat it is possible.) If I knew these two things I could probably figure out the rest.  Alternatively, is the way to do this described somewhere in the Roll20 manuals?  Ive been looking around in there but there's just so much information.  If someone could point me in the right direction I'd be grateful. 
1601481266
The Aaron
Roll20 Production Team
API Scripter
The short answer is, the API is event driven, so to get 1) and 2), you need to have an event (chat:message) which is responded to by calling your function. Simple example: on('ready',()=&gt;{ const myFunc = ()=&gt;{ sendChat('myFunc','myFunc was called!'); }; on('chat:message',msg=&gt;{ if('api'===msg.type &amp;&amp; /^!call-my-func(\b\s|$)/i.test(msg.content)){ myFunc(); } }); }); In the above, on('SOME EVENT',SOME_FUNC) binds a function to be called when that event occurs.&nbsp; The 'ready' event occurs once after the API Sandbox is fully loaded.&nbsp; The 'chat:message' event occurs when something is put in chat. Here are some links to forum discussions that should help: <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> <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/6237754/slug%7D" rel="nofollow">https://app.roll20.net/forum/post/6237754/slug%7D</a> And here is a link to Nick's tutorial videos.&nbsp; He has a bunch about creating scripts that should be good introductions: <a href="https://www.youtube.com/watch?v=jam2yx8btaQ" rel="nofollow">https://www.youtube.com/watch?v=jam2yx8btaQ</a>
The example you gave was incredibly helpful!&nbsp; So basically we're defining how we call the function using a regex, and then using a regex test to check if a command was given every time the chat updates.&nbsp; Does that sound about right? I have more questions now, but they're in regards to passing data from the VTT (dice rolls, token attributes, etc) to the API, so I'll ask them in the appropriate place.&nbsp; Thank you so much for your example, you're a legend.
1601559527

Edited 1601559563
The Aaron
Roll20 Production Team
API Scripter
Pretty close.&nbsp; Here's an annotated version: // Register a function for the 'ready' event. When it occurs, which happens only once // this function will get called. This avoids getting events resulting from the // loading of all the game state into the API on('ready',()=&gt;{ // Declare a function called myFunc. This is using the "Fat Arrow" function syntax // and the const declaration from Javascript ES6 const myFunc = ()=&gt;{ sendChat('myFunc','myFunc was called!'); }; // Register a function for the 'chat:message' event. This event occurs for all // chat messages, so it's important to filter down to just the ones you care about on('chat:message',msg=&gt;{ // First check the type is an API message. API messages are not show shown in chat // and begin with a ! in the first character of the message. // // Next, make sure this is our API message. The regular expression checks that the // command starts with "!call-my-func" and either ends or has a space, all case // insensitive. if('api'===msg.type &amp;&amp; /^!call-my-func(\b\s|$)/i.test(msg.content)){ myFunc(); } }); });
1601572772
Andreas J.
Forum Champion
Sheet Author
Translator
I added this example to the wiki here: <a href="https://wiki.roll20.net/API:Use_Guide#Simple_API_Example" rel="nofollow">https://wiki.roll20.net/API:Use_Guide#Simple_API_Example</a>