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

Add or subtract attribute based on cards

1405549297
Wil
Sheet Author
I'm running Fate Core, and like a number of people running it on Roll20 I am using a card deck to distribute Fate Points. However, we still have to remember to update character sheets with the Fate Point total. In playing around with the tokens, I had a realization: Is it possible to increment an attribute when a card is dealt, and decrement it when a card is discarded? It should just be a matter of pulling the count of the cards via the API, right? I've just started poking around with this possibility, so I'll update if I find something. I'd like to start a repository of Fate Core-specific scripts as I develop them.
1405551171

Edited 1405620601
The Aaron
Roll20 Production Team
API Scripter
You can certainly get the count for a person's cards in hand. I don't know if there are events you could listen to, but you could certainly poll each players hand every so often.
1405551481
Wil
Sheet Author
Ok, so now the goal is to figure out how to get the count out of the players' hands.
1405551840
The Aaron
Roll20 Production Team
API Scripter
This will log them all to the API console: _.each(findObjs({_type: 'hand'}),function(h){ log(h); });
1405551925
Wil
Sheet Author
Thanks! I was just about to try something similar. Newish to Java, but not development - it's like learning to ride a unicycle after riding a bicycle.
1405552110

Edited 1405552148
The Aaron
Roll20 Production Team
API Scripter
Looks like there is a change event for hands: on('change:hand',function(obj,prev){ log(obj); })
1405552243
The Aaron
Roll20 Production Team
API Scripter
No worries. JavaScript is not the same as Java, so be careful when you're looking for sources of information, you might get confused! I highly recommend JavaScript: The Good Parts by Douglas Crockford. Fantastic book that has taught me a ton about JavaScript. I used to hate it as a language, (being a C++ purist!) but I find myself really enjoying it after having read that book.
1405552399
Wil
Sheet Author
Sorry, meant JavaScript. I'll have to check that out, it's also good to expand my horizons (I'm a SQL dev, so beyond some rudimentary C# and VB I'm notably lacking on the web dev end). So far, looks like what you found is going to work perfectly. It shows how many cards are in the hand when it changes. I'll post it when I'm done.
1405553179
The Aaron
Roll20 Production Team
API Scripter
Sweet! Let me know if you need any help! =D
1405553342
Wil
Sheet Author
I've figured out how to get the length of the object but not the count of the individual keys. For example: {"_currentHand":"-JJi9rIR22sfIEHaA7ey,-JJi9rIR22sfIEHaA7ey,-JJi9rIR22sfIEHaA7ey,-JJi9rIR22sfIEHaA7ey" So that shows me there are four cards in the hand, I just need to count them. I'm sure I'll figure it out. Then all I need to do is pass that count to the attribute for the appropriate character.
1405555029
The Aaron
Roll20 Production Team
API Scripter
It's a string, so you need to turn it into an array: obj.get('_currentHand').split(',').length; // === 4 obj.get('_currentHand').split(',')[0]; // === '-JJi9rIR22sfIEHaA7ey' var cards=obj.get('_currentHand').split(','); //== cards is now an array with 4 strings in it
1405555422
Wil
Sheet Author
Thanks again! That did the trick, I can get the length of the array. The rest of it should be easy.
1405555548
Wil
Sheet Author
BTW, here's what I have so far with your help. Works like a charm. on('change:hand',function(obj,prev){ var count = 0; obj.get('_currentHand').split(',').length; obj.get('_currentHand').split(',')[0]; var cards=obj.get('_currentHand').split(','); count = cards.length log(count); })
1405555604
The Aaron
Roll20 Production Team
API Scripter
Cool. They've recently made this much easier: <a href="https://wiki.roll20.net/API:Objects#Working_with_C" rel="nofollow">https://wiki.roll20.net/API:Objects#Working_with_C</a>...
1405555705

Edited 1405556190
The Aaron
Roll20 Production Team
API Scripter
You probably want to wrap that in an on('ready',..): on('ready', function(){ on('change:hand',function(obj,prev){ var cards=obj.get('_currentHand').split(','), count = cards.length; log(count); }); }); That will prevent you from doing a bunch of extra work while the table gets setup on refresh. Edit: and you can dump these, they were only there as examples: obj.get('_currentHand').split(',').length; obj.get('_currentHand').split(',')[0];