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

Calling API function from macro

Am I correct in stating that a macro cannot call an API function? Thanks.
1380587985
esampson
Pro
Sheet Author
I haven't tested it, but I don't see why it shouldn't be able to. Generally API functions that are 'called' are called through chat (with the !<blah> message). Since macros can use chat I would imagine they could call API functions.
1380590554

Edited 1380590659
I didn't think about echoing out !functionName(); Thanks Well I tried DavoutConditions.test = function (){ sendChat("API", "/w gm EXECUTED"); }; with the macro being: !DavoutConditions.test(); and I get {"who":"error","type":"error","content":"Unable to find a player or character with name: undefined"} in the log.
You can totally put an api chat command into a macro. I use them all the time. For example, I have a macro to add temp hp to selected tokens, one to check temp hp on tokens, and another to equip various weapons in attributes on character sheets. !addthp ?{Temp HP to Add|0} !chkthp !equip Dagger | 0 | 1d4 | piercing | MH I just put those commands in macros and they work great.
1380594335
Lithl
Pro
Sheet Author
API Scripter
You can't call a function directly. You can create an API command which can be called from the macro (use on('chat:message', function(msg) {...}); to process the API command).
You can, but only as a direct link to the function. This is useful for single function scripts (like and end or turn, or auto initiative script) where you just want to click a button instead of typing the entire chat cue. Just make the body of the macro !command and let the script do it's thing.
Thanks guys.
1385050483
tontione
Pro
Sheet Author
Sorry, i just subscribed a mentor account, and i don't understand how to code an api call from a macro. Can you give an exemple ? I tried : macro : !test1 Api script named "test1" : log("ok"); => log is written at start, but not for a clic on the macro button. same with Api script : function test1(){ log("ok"); } => i think i misunderstood something. Can you help me ?
You need to create an intermediary. The chat cannot call a function directly. You need to write a wrapper that intercepts the chat message and then - if it contains your command - IT calls your function. Here's an example of a simple chat parser that calls a function. In this case, the user would select some tokens, then type !InfoDump in the chat. (or make a macro that does the same thing) var L47FC = L47FC || {}; L47FC.DumpTokenInfo= function (token) { log(token.get("name") + ": " + token.get("statusmarkers") + " location: " + token.get("left")/70 + ", " + token.get("top")/70 + " : " + token.get("width")/70 + ", " + token.get("height")/70); } on("chat:message", function(msg) { if (msg.type == "api" && msg.content.indexOf("!InfoDump") !== -1 && msg.who.indexOf("(GM)") !== -1) { log("InfoDump: " + msg); _.each(msg.selected, function(objInfo) { var obj = getObj(objInfo._type, objInfo._id); if( obj.get("_type") == "graphic" ){ if( obj.get("_subtype") == "token" ){ L47FC.DumpTokenInfo(obj); } } }); } });