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-add events for all my old characters.

1491007810

Edited 1491009199
OK, pulling my hair out here...  Still new to js and Roll20 scripting in general.  I want to build a script to add new attributes to the character.  OK, simple enough, I can do that; but  I found that it the script was adding it to all characters every time the API Sandbox would restart, not good!  Luckily I do have a separate game I am working on scripts in and copying them to my actual playing game, otherwise I'd be deleting all these added attributes from all characters, blech!  OK, so multiple search, all turning up the same basic functions to check if the attribute already exists, but even after cutting/pasting and only changing the attribute name to search for, still doing the same thing.   on('add:character', function (obj) {     var strModHE= findObjs({         type: 'attribute',         characterid: obj.id,         name: 'StrengthHE'     } , {caseInsensitive: true})[0];    log(obj.id + " - " + obj.get('name') + " - " + attribute)     if (!strModHE) {         strModHE= createObj('attribute', {             characterid: obj.id,             name: 'StrengthHE',             current: 0         });     } }) I added the log to see what was actually being run and found the following as output -  Restarting sandbox due to script changes... Previous shutdown complete, starting up... Spinning up new sandbox... "-KeTaQDt3R0hpwBwe83s - Art - Cob - undefined" "-KeUBaFi4nDc0MSEEU_5 - Greg - Amiri - undefined" "-KeUBfhczlkqn-LR9-Ba - Leslie - Endrill - undefined" "-KeVDGF_js9IVtdS-u4V - James - Doraz - undefined" "-KeaFpI7JCFxYoF2vHEJ - Linda - Bunny von Cleese - undefined" "-KefdY6FMv_-DvbSQIdw - Peter - Eklisia (ek-LEE-see-a) - undefined" "-KezwDemWU5XHDzV9Xvk - NPC - tNelag - undefined" "-KgBCAXjLRl3k91kI8ig - Ben - Judin Ukosk - undefined" "-KgGs2G5mmdNW4DcaKn6 - Ovugo Dibate - undefined" "-KgGwK0hvIrMgT-_eEmu - Rox - undefined" This would sound good, the variable strModHE is not defined, so go ahead and create the attribute.  However, when the attribute does already exist, the findObjs  is not finding it and the variable is still undefined.  I added the logging to see what it was pulling, and that all looks ok as well.  I can simply make a script and call it from chat or a macro, but I'd prefer not to have to do that.  Anyone able to help? Thanks,  Scott
1491012961
Lithl
Pro
Sheet Author
API Scripter
add: type  events fire for every applicable object in the VTT when the sandbox spins up. In order to only fire when a new  object is created, you need to register the add event after the VTT is ready, like so: on('ready', function() { on('add:character', function(obj) { // this will only trigger for actually new characters }); });
So simple, so elegant, thank you Brian! I have done some js in the past, worked with other VTTs, but really getting into the Roll20 API now.  I am running a Castles & Crusades game and will continue to automate more and more.  One of my next tasks is going to be making my own Character Sheet for it, but, not being a web developer, I first must learn CSS, not looking forward to that, but it is what it is.  I like the sheet that is out there now, but want to change a few things on it, that will be a bit of a chore.