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 .
×

[Help] Need a simple starting point

I need to build a script to calculate damage based on variables in both the selected and targeted token, but it will require an amount of if/then statements that I can't do with macros. I've got some scripting experience, but I'm struggling with the syntax. obj.selected pulls up an array of objects, and I can't figure out how to select just one (or just the first one). And the documentation doesn't show how you can retrieve the list of attributes from a character ID anyway. I can't find a sample script with enough commenting to figure out what's going on. Could someone put together or point me at a script that retrieves a specific attribute from a selected token's represented character, and a targeted token's represented character? Something with decent comments would be preferred. I feel like I could get the rest of the way if I just had a 'this is how you access the attribute', as after that I just have to evaluate and output to chat. I should be able to figure the rest out from there. Does anyone have a script handy that I can use as an example? Thank you for any help you can offer.
1397234209
Lithl
Pro
Sheet Author
API Scripter
Accessing an element in an array is similar to most languages: var myElement = nyArray[0]; would get the first element of the array. The selected array, however, only contains the id and type of the object. You'll need to use getObj(myElement._type, myElement._id) to get the token itself. Attributes are separate objects from the characters they are a part of. Similarly, characters an tokens are separate. They're all linked together by id. To get an attribute that a token ought to have, you can do this: var token = ...; // Getting a reference to the token is left as an exercise to the reader var character = getObj('character', token.get('represents')); var strength = findObjs({ _type: 'attribute', _characterid: character.id, name: 'Strength' })[0]; Note above, getObj returns the exact object you need, while findObj returns an array of matching objects, even if the array only contains one item; the same above uses findObj(...)[0] to get the first result from the set. If you wanted all of the attributes on a character, regardless of what they're called, you could still use findObjs . If, for example, you omitted the name property of the anonymous object, findObjs would return all attributes for the given character. (Of course, you'd also want to omit the array index, as you want all of them!)
Keep in mind you can use the @{selected|...} and @{target|...} as arguments to pass into the script. and then from there you can determine different values. so lets say for instance you have attributes: Damage = 9 DamageResist = 3 The @{selected|...} will be the guy "attacking" the @{target|...} !calcdmg @{selected|Damage} @{target|DamageResist} on("chat:message", function(msg) { // Exit if not an api command if (msg.type != "api") return; // Get the API Chat Command msg.who = msg.who.split(" ",1)[0]; var command = msg.content.split(" ", 1); // End Turn if (command == "!calcdmg"){ // do your stuff here. So for instance you might want to determine total damage. then apply it. var args = msg.content.split(" ", 1); // get values passed into script from chat var dmg = args[1]; // first value was the @{selected|Damage} var resist = args[2]; // second was @{target|DamageResist} var dmgResult = resist - dmg; // keep in mind that atm, you don't know who the target is, in which case you might instead want to use // @{target|token_id} so that you can use brians code above to find the DamageResist value and apply it to // HP } });
@Brian and @Chris N., thank you! This is exactly what I needed. I've got my script working and just need to clean it up now. And wait for bugs in Character Sheets to be ironed out. I get the gist of javascript now. I made the mistake of thinking it was like Java, which I have some familiarity with. Took me awhile to get used to the changes.
1397298615
Lithl
Pro
Sheet Author
API Scripter
JS is similar to Java in many ways; what's tripped you up? If it's the expectation that the character object would contain a reference to its attributes, that's completely doable in JS, it's just not how R20 was designed.
Partially that. How to get attributes to wasn't particularly straightforward, though I've got it ironed out now. But there are a few differences between the two coffee-codes that were tripping me up, like how JS doesn't declare variable types. Or at least, it doesn't seem to?
1397365247
Lithl
Pro
Sheet Author
API Scripter
Correct. JavaScript is a weak-typed language. Variables are coerced to the appropriate type as needed. This has advantages such as treating an object as a boolean when you want to check whether it's undefined, but it also has disadvantages such as variables getting coerced to something you weren't expecting, introducing hard-to-track bugs. It's sort of like everything being of type Object, except the runtime inserts all the typecasting for you.