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

[Help] Getting started on writing a Script

Hello all, I'm new to writing roll20 scripts, and I'm wondering what a good place to start is. I'm looking to write a script that changes chat colors. I've downloaded the API repo, but am wondering what files/folders I should look in to start. Any help would be appreciated!
1495135682

Edited 1495135852
The Aaron
Pro
API Scripter
In a recent post, this was my advice to getting started: The Aaron said: It really doesn't matter what you choose to do first, it's all about learning. The API runs on top of Node.js, so if you're looking for more in the way of tutorials, reading about the way Node modules work is probably helpful (the whole "event driven" thing). However, I don't think you need to go overboard there. Javascript isn't as complicated as it seems and 3 hours is long enough to learn the basics of the language, enough to write some API Scripts. I would start by writing the following: • A script that parrots back whatever you pass it: This will give you the chance to learn about API commands and the chat:message event, which is probably the most used event. !say polly want a cracker? polly want a cracker? Then play with the formatting a bit. Make it whisper it to the person that ran the command. Add some header text like "The parrot says" to it. • A script that whispers to the GM how far a token moved. This will give you a chance to learn about change:graphic events, probably the other most used event. Start with just the x,y pixel distance, then add the name of the token, convert to the units of the current page, add the name of the character it represents, if any. If you want to get REALLY fancy, read the lastmove and calculate the actual distance based on the path. • A script that tells you if your roll is higher, lower or the same as your last roll This will give you the chance to get into persisting data and understanding the inlinerolls syntax, which is a bit complicated. !roll [[1d20]] 18 (no prior rolls) 7 (lower) 9 (higher) 9 (same) 20 (higher) Once you've run through those, you'll probably have a pretty good feeling for the parts of the API.
I'll try those out. Thanks!
1495137647
The Aaron
Pro
API Scripter
Yup, definitely post back if you have any questions. We're a helpful bunch, by and large. =D
I've gotten close to what I need, but I'm having a hard time changing the CSS that I need to change. Is this just not as easily done with Node.js as it is in JavaScript?
1495621110
The Aaron
Pro
API Scripter
CSS is all inline and only a subset of properties are white listed. 
1495621153
The Aaron
Pro
API Scripter
No classes, only the style attribute. 
I see. So is doing 'document.whatever.style.property' not the right way? I keep getting an error that document isn't defined.
1495633995

Edited 1495634076
The Aaron
Pro
API Scripter
Oh, nope.  The API has much more akin with a node module running in node's V8 on a server.  There is no document or window object.  You have a limited selection of things you can manipulate.  The basic pattern of an API script is: Register Event Handlers Receive Event Do stuff Call functions / Create Objects Goto 2. Any manipulation of the state of the game will be by either modifying/removing existing objects or creating new ones.  Outside of object based information, the way you communicate with the users is basically sendChat() output.  You can send whatever text you like via sendChat() and it will get output into the chat as a new message, and generally interpreted as if someone typed it all out in the chat box (with some minor exceptions around formatting).
1495634136
The Aaron
Pro
API Scripter
With that in mind, read through the first few pages of the API wiki again:&nbsp; <a href="https://wiki.roll20.net/API:Introduction" rel="nofollow">https://wiki.roll20.net/API:Introduction</a> It will probably make a lot more sense now.
1495634158
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
How do you define css inline? I've been just using html div tags.
1495634566

Edited 1495637921
The Aaron
Pro
API Scripter
Just like you've been doing. sendChat('Test message', `&lt;div style="border: 1px solid black; border-radius: 1em; padding: .5em; color: red; background-color: #cccccc;"&gt;Howdy!&lt;/div&gt;`);
1495637401
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
so the inline css is separate from the div tag? That seems opposite of what I found looking&nbsp; around the net , where it seemed to be part of the tag.
1495637897

Edited 1495637960
The Aaron
Pro
API Scripter
I edited the example to be clearer...
1495640508
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Ah, ok, thanks, that's what I needed to get it through my thick head ;)