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

Macro creation through API - need access to read-only "_player_id" variable?

Is there a way to set the "read-only" variable 'playerid' for an auto-generated macro? I'm working on a sample script to create a basic macro object through the API on campaign startup. Script: on("ready", function() { createObj("macro", { name: "attack_macro", action: "/me attacks!", visibleto: "", istokenaction: false }); }); I'm getting this error: ERROR: You must pass in a valid 'playerid' property when creating a macro. I have tried setting the "playerid" field, with the same error result sample attempts: " ... playerid: 0 ... " " ... playerid: 1 ... " " ... playerid: "Two's Company" ... " Thoughts on this would be great. Thank you! -Two's Company
1392425297

Edited 1392425337
Fabio M.
KS Backer
It has to be a valid player ID. To know yours (and your players IDs), you can use the following script. Just select a token that is controlled by one of your players (or that is linked to a character's sheet) and type !playerid in the chat and it will whisper you (the master) the name of the token followed by the ID(s) of the players controlling it or controlling the character. You then can use the IDs in your code (without commas - if any - but with the dash). A valid ID is something like this: -J5R2trPC6jWXlClBuTL on('chat:message', function(msg) { if(msg.type == "api" && msg.content.indexOf("!playerid") !== -1) { var selected = msg.selected; _.each(selected, function(obj) { if(obj._type != 'graphic') return; var token = getObj("graphic", obj._id); if(token.get("controlledby") == ""){ var character = getObj("character", token.get("represents")); sendChat("Script", "/w gm " + token.get("name") + ": " + character.get("controlledby")); } else sendChat("Script", "/w gm " + token.get("name") + ": " + token.get("controlledby")); }); } });
1392430034

Edited 1392448485
Thank you for the script, Fabio. Unfortunately, I'm getting a new error now. "Unexpected identifier" My guess is that this is a problem with the generation of macros via the "create_object". The "playerid" variable is listed as "read-only" per the object definitions here <a href="https://wiki.roll20.net/API:Objects#Macro" rel="nofollow">https://wiki.roll20.net/API:Objects#Macro</a> If this is the case, how should I go about creating macros at campaign startup? Best, Two's Company
1392430453

Edited 1392430695
Fabio M.
KS Backer
Sorry, I forgot to tell you that the valid player ID was probably meant for the "visibleto" property. You shouldn't specify the _playerid. But I didn't try to create a macro yet, so I'm not sure about this. If I have 5 minutes later I'll try it and get back with some (hopefully) good news. Edit: Another thing, I've just noted that you're creating the macro with the on ready event. I'd rather create it with a specific command to avoid many copies of the same macro being created by mistake at every loading of the campaign.
Ok, I tried the code and it works. I was wrong, you have to specify the playerid property. I used my id, though (GM). So read your id from a token controlled by you and use that one. The script I used: on('chat:message', function(msg) { if(msg.type == "api" && msg.content.indexOf("!createmacro") !== -1) { createObj("macro", { name: "attack_macro", action: "/me attacks!", visibleto: "", playerid: "-J5R2trPC6jWXlClBuTL", istokenaction: false }); } });
Fabio, Awesome. Thank you so much! This functions perfectly. Hope things are well! -Two's Company