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

[Script] Help with making an API that will create a token macro

I am very new to JavaScript and this is my first time posting on the forums so I apologize if this dose not belong here, but I can not get this to work and could not find any other forum post similar. I am trying to make an API that will create a token macro for my selected token, but I can't seem to get it to work right. Whenever I run it I get this Type Error  TypeError: Cannot read property 'get' of undefined I don't think I am getting the selected objects id correctly and I am not quite sure how to go about it. I know this is an easy fix I just don't know what I am doing wrong. on("chat:message",function(msg){     if(msg.type=="api" && msg.content.indexOf("!test")==0){         var selected = msg.selected;         var selObj = getObj(selected[0]._id);         createObj("ability", {             characterid: selObj.get("_id"),             name: "testing",             action: "testing",             istokenaction: true,         });     } });
What kind of token macro are you trying to create? I have 0 coding expertise, but if you're just looking to create attacks, spells, and checks/saves on a token, I'd recommend keithcurtis's Token Action Maker script. It's excellent.
@Jay R. Thanks, but that's not quite what I am looking for. I am using Three of Swords Door Open API. The script allows you to open a door with the command !DoorOpenClose. I want my players to be able to open doors that are unlocked so I made a door token and gave it the a macro that ran !DoorOpenClose, but if I was a door to be locked I don't want to have to remove the Token macro from each locked door and when they find the key or pick the lock manually add it. I want to be able to select the Locked Door token and run a Unlock Door command that will create a Token Macro that will allow my players to open the door from then on.
1603853379
GiGs
Pro
Sheet Author
API Scripter
I'm not familiar with that script, but an alternative approach might be to set the ownership of the door token using tokenmod. If players cant click it, they cant use the macros linked to it. Then you give them ownership of the door token, and now they can click it and use the macro.
1603853778
GiGs
Pro
Sheet Author
API Scripter
Regarding your script, your main problem is here: characterid: selObj.get("_id"), At the point this command runs, selObj is referring to a graphic object, a token, not a character. So the characterid will be wrong. The steps you need to do with your script: get selected object check you have just one object selected, and it is a token with its represents property set. Get the character from the represents property. Get the character id. Set the various ability properties. But you have another problem. An Ability set as a token action will only show up for the controller of the token, IIRC, so your players won't see it anyway.
@ GiGs Thanks for the help, I realized what I wanted to do was impossible without making an individual character sheet for each door on my map. For some reason I was under the impression that I could have a token linked to a character sheet, but have different macros for each token on the map.
1603872545

Edited 1603875068
Oosh
Sheet Author
API Scripter
edit - didn't read that post right in the middle about the Door script you're already using..... the chat menu part probably isn't what you're looking for, but using the bar1 - bar3 fields could work for you. They're token specific and don't rely on a character sheet You can probably do what you want to do (though I'm not really clear on what that is....) - using the barX properties of the graphic object: you can set this to hold a macro that you can run from the chat bar with @{selected|bar1}. You won't get individual token actions for that token, but you can do something clever like have a macro on a character sheet that links back to the token bar. So the macros character sheet has this ability called bar1 which is: @{selected|bar1} This step is just a workaround... I don't think you can use the [Button](~ability) syntax to link to an Attribute or a Token Bar, despite the fact that it can contain exactly the same text as an Ability macro. And then your chat macro prints 3 buttons in chat: /w gm [@{selected|bar1|max}](~macros|bar1) [@{selected|bar2|max}](~macros|bar2) [@{selected|bar3|max}](~macros|bar3) Then you just need your script to set bar1 and bar1|max on the token to your action/label for that token. Using what you've got above: on("chat:message",function(msg) { if (msg.type=="api" && msg.content.indexOf("!test") == 0) { var selected = msg.selected; var selObj = getObj('graphic', selected[0]._id); selObj.set('bar1_value','&{template:default}{{name=Test}}{{Door script command goes here}}'); selObj.set('bar1_max','Click Me'); } else { return; } }) It would need a bunch of work, but it can be done if that's what you're trying to achieve. You can use the script to set bar1_value to whatever code opens that door with your Door script, then run it again to "lock" the door by removing the contents of bar1_value (or set it to a message telling the player that the door is locked). All the player macro needs to contain is @{target|t1|bar1}. They click the macro, then do a target click on the door. This would rely on that door script being accessible to players, obviously. If they can't send it commands, it won't work.