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

Am I doing functions right?

1403777353

Edited 1403783508
I recently decided to get in depth with functions in my code mainly because I was reusing lines of code I didn't need to. However, I am not sure if I am doing this right.... Here in my gist, I have function declaration starting at line 50. From what I understand, since this function is outside the main body of the on(chat) function I declare into a variable for the main script function to call back. What I am wondering, is my function starting at line 62 correct? I am only calling this function inside getDamResolution so I didn't put it into a variable. <a href="https://gist.github.com/mpiersant/fc3979961e5e93fdeb6c" rel="nofollow">https://gist.github.com/mpiersant/fc3979961e5e93fdeb6c</a>
1403782512
The Aaron
Roll20 Production Team
API Scripter
Looks right. Does it run? One thing to consider is that your function on line 50 will be visible (in the global scope) to every script you have installed. If you use a pretty generic name (yours isn't, but consider var print = function(var str) {} ) it could get overridden or override another function. You are probably better off to declare an object for your code and put your functions in that.
1403783693

Edited 1403784159
Actually I just updated the gist. I found several errors that I have corrected. However, it isn't working but not because of the structure. I added the other functions I had in another script I use as a global function namespace. getAC is having an issue using the passed variable that holds @{target|token_id}. Why it started acting up, I have no clue. You can see on line 13: var targAC = getAC(targetId); // get target's AC value Calling the function (which is global) starting on like 160. I pass it targetId which as been defined on line 8 as parts[1]. I just checked my macro, and target|token_id is indeed being feed to the script as parts[1]. I have no clue why it isn't working. When I had this all in the main body of the on(chat) call it all worked fine, and before restructuring the code I made a test bed to set up this structure. I made a function, passed it the id just like I have, then had the function get the graphic representation, then had it pull various attributes and log them. Worked well so I added it all as you see now. LOL I get scripts to work using the noob methods, then learn how to clean it up, and break it. :(
1403783986
The Aaron
Roll20 Production Team
API Scripter
attkRoll() and getDamResolution() will both be in the global scope. Here's a skeleton of moving them into an object. I also like to advocate putting minimal code in the on() function bodies, and handling all the on calls in one places. var funcTester = funcTester || { attkRoll: function(targAC, whoAttk, selectedId){ // Do attk Roll stuff }, getDamResolution: function(success, targetId, selectedId) { // do dam resolution stuff }, chatHandler: function(msg){ // do chat message stuff }, attributeChangeHandler: function(obj,prev) { // mess with attributes }, registerEventHandlers: function() { on('chat:message', funcTester.chatHandler); on('change:attribute', funcTester.attributeChangeHandler); } }; on('ready',function(){ funcTester.registerEventHandlers(); }); In this case, the only thing in the global scope is an object called funcTester
1403784322

Edited 1403784545
That is hurting my head trying to figure it out. I made some of the functions global as I might have need of them in other scripts. This particular script is associated with the players first attack. My dual attack script will also be getting AC and attacks as well as damage, but using different modifiers and attributes.
1403784675
The Aaron
Roll20 Production Team
API Scripter
I'm more than happy to walk through it with you if you want. Which part is giving you problems?
1403785193

Edited 1403785373
It is basically a function, that holds function handles? So I take all the bodies of the globals I wrote, and dump them into their respective function handle? Okay, so how do I call the appropriate handle at the right time? I have 5 functions atm that are global. I only added them into this gist to make it 1 page, they actually are on another script page on their own waiting to be called. I figured I didn't need to put them into an on() fucntion({}); call.
1403786174
The Aaron
Roll20 Production Team
API Scripter
It's an object, just like JSON (JavaScript Object Notation). Labels (like member variables) are assigned values. Those values can be functions or objects or numbers or whatever. Label assignments are separated by commas. Members of the object are referenced by the . as in the on('ready') call. As as for accessing the handlers, when the 'ready' event is fired, it calls the registerEventHandlers() function which then registers the other on() event handlers. From that point on any time one of those events occurs, your callbacks get called.
So to call them I simply put on('attkroll:', function ({});
1403798224
The Aaron
Roll20 Production Team
API Scripter
No, sorry. The on() function is a Roll20 method of subscribing to a particular event. When you call it, you are telling Roll20 " every time this event happens, I want you to call this function. " You only need to call on() once for each time you want to subscribe a function to an event, and then whenever that event occurs, your function will be called. (See <a href="https://wiki.roll20.net/API:Events" rel="nofollow">https://wiki.roll20.net/API:Events</a> for more details.) If you want to call the attkRoll() function in the skeleton code I wrote, you would call it: funcTester.attkRoll( foo, bar, baz); or if storing the result: var result = funcTester.attakRoll( foo, bar, baz); In this way, the only thing in the global scope is the funcTester object. There are no private functions in Javascript, so other modules/scripts you write can call those functions the same way. You can think of this like namespacing your functions to the funcTester namespace. (or if you're not familiar with namespaces, you can think of it like the area code of a phone number.) ( The on() function is basically the Observer pattern, which you can read more about here if you're interested: <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow">http://en.wikipedia.org/wiki/Observer_pattern</a> )
Interesting. I will have to look into it more. For now, I need to push my code out in a working state. Guys are getting upset I am talking so long to get a campaign, no matter how short, out the door.
1403805712
The Aaron
Roll20 Production Team
API Scripter
No worries. What's holding you up? =D
Pool time... that is what is holding me up. It's pretty hot here in tulsa in the summer, so a good portion of my day is taking an 11 year old swimming.
1403872222
The Aaron
Roll20 Production Team
API Scripter
:) doesn't sound like a bad problem to have!