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! Trying to use API, missing some logic as to how to get it to work for Class resources/repeating sections

So I am using the Community Contributed Sheet (I'm aware thats not fully supported, but i'm a big fan of it's layout, seems easier and more intuitive than the OGL and shaped for me) . Anyway, I am trying to simply have a script called UseClassResource, its not robust just a simple example for the very first classresource on my list. I use the following code to get the information I want. var ClassResource = findObjs ({         _type: "attribute",         _characterid: Selected_Char_Id,         _name: "repeating_classresources_$0_ClassResourceTotal"         }); When I send ClassResource to sendChat to help troubleshoot what the heck is going on. It indeed is getting 1 object, verified by typeof ClassResource... Now my problem I am having is getting the actual value from this. I've tried ClassResource.current (undefined) ClassResource.value (undefined). And sendChat the actual object doesn't print anything (which I sort of figured it wouldn't? as it is an Object according to typeof.  I tried inspecting the sheet to get what the actual name was (it is attr_ClassResourceTotal) and it shows it is of type number, which I don't understand how to get the number out of it. because of the OOP of the system, I thought maybe I should not grab is specifically? (such as _name: "repeating_classresources_$0_" to hopefully be able to .ClassResourceTotal it? I am just guessing. I have some experience with programming (C and C based coding) so I am sure I am not utilizing the OOP correctly. I am certain the other parts of my code are working correctly (made 2 scripts so far, took awhile but eventually was able to troubleshoot it with the above method I mentioned) But repeating section and get/set functions aren't behaving in the same way and its confusing me to no end :(. If anyone have time to respond I would greatly Appreciate it
1591114628

Edited 1591124075
Nm I Think I got this working using just getAttr(); function... Thanks anyway :)
Okay apparently I still need help, after using the findObj function, how do I access the fields of the obj? findObj seems to be required if I want to set a value. (I have been able to get values with getAttr, but using those obj don't work. I also sometimes get error message saying a function doesn't exist when I try to use the .get() function (so for example on below if after I find this object, when I do ClassResource.get("current"); it yells at me that that function doesn't exist. var ClassResource = findObjs ({          _type: "attribute",          _characterid: Selected_Char_Id,          _name: "repeating_classresources_$0_ClassResourceTotal"          }); After getting the the value, i want to perform math on it *that part seems to work* and then set it back, replacing the value that existed there before (I also do some error checking to mak sure my value before setting is between 0 and max value, again not a problem so far) just having issue trying to set anything. I've also tried doing ClassResource[0].get("current") but get undefined bs. (the value in question is defined was able to sendChat with getAttr the value of current) So again any assistance would be appreciated. been messing with for a couple days now and I know I am missing something really simple, as what I am trying to do isn't particularly complicated. Thank you for your time.
1591119127
GiGs
Pro
Sheet Author
API Scripter
Its difficult to diagnose script-related issues from descriptions, you need to provide code you've tried and we can tell you why its not working. In case you aren't aware, findObjs returns an array of objects. So ClassResource[0] is the object returned from the function above.
1591120464

Edited 1591120712
GiGs
Pro
Sheet Author
API Scripter
Does your findObjs return anything? I notice a couple of issues on this line:    _name: "repeating_classresources_$0_ClassResourceTotal" It's name , not _name . You only use _ with the ones that specifically have it listed. It denotes a specific kind of property, it's not something you can apply to any property, Also, does this syntax work in the API? "repeating_classresources_$0_ClassResourceTotal" I have never tried it, but I'd have assumed you need to use its full id. That $0 syntax is meant for ease of player use with macros and the like. It doesn't work in sheet workers - I'm not as familiar with the API, but i would expect it not to work there too.
Here is my code:  //Syntax for this API is !UseClassResource <Selected|token_id> <Selected|character_id> <Numer of Resource Spent> on("ready",function() {     on("chat:message",function(msg){         if(msg.type=="api" && msg.content.indexOf("!UseClassResource")==0 ){             var selected = msg.selected;             if (selected===undefined){                 sendChat("API","Please select a character.");                 return;             }                          var args = msg.content.split(" ");              var command = args.shift().substring(1);                          if (command == 'UseClassResource'){                                  if (args.length < 3) {                     sendChat('SYSTEM', 'You must provide a selected token id, character id and the spell slot level used');                     return;             }                    // 1st Parameter should be the Token's Id                 var Selected_Tok_Id = args[0];                 var Selected_Char_Id = args[1];                 var Selected_ResourceNumberSpent = args[2];                 Selected_ResourceNumberSpent = parseInt(Selected_ResourceNumberSpent,10);                                                   var Selected_Token = getObj('graphic', Selected_Tok_Id);                 var Selected_Character = getObj('character',Selected_Char_Id);                                  if (!Selected_Token) {                     sendChat('SYSTEM', 'Selected token id not provided.');                     return;                 }                 if (!Selected_Character) {                     sendChat('SYSTEM', 'Selected character id not provided.');                     return;                 }                 if (!Selected_ResourceNumberSpent) {                     sendChat('SYSTEM', 'Resource Number Spent not provided.');                     return;                 }                 //Preparing to get speakingas for sendChat                 var SpeakingAs = getObj('character', Selected_Token.get('represents'));                                  if(!SpeakingAs){                                          SpeakingAs = Selected_Token.get('name');                                      }                 else {                                          SpeakingAs = 'character|' + SpeakingAs.id;                 }                              var ClassRes = findObjs ({         _type: "attribute",         _characterid: Selected_Char_Id,         _name: "repeating_classresources_$0_ClassResourceTotal"         });              var ClassResource = getObj('attribute',Selected_Character.get('repeating_classresources_$0_ClassResourceTotal'));     sendChat(SpeakingAs,"Getting getAttrByName of repeating_classresources_$0_ClassResourceTotal|max is "+getAttrByName(Selected_Char_Id, "repeating_classresources_$0_ClassResourceTotal", "max"));                                  var CurrentSum = getAttrByName(Selected_Char_Id, "repeating_classresources_$0_ClassResourceTotal", "current");                                  sendChat(SpeakingAs,"The typeof ClassResource is "+ typeof ClassResource);                 sendChat(SpeakingAs,"Current Sum as a string retrieved by getAttrByName is "+CurrentSum);                 CurrentSum = parseInt(CurrentSum,10);                 sendChat(SpeakingAs,"Current Sum changed to a Number is "+CurrentSum.toString());                 sendChat(SpeakingAs,"The typeof of ClassRes is "+ typeof ClassRes);                 sendChat(SpeakingAs,"Trying to get ClassRes.get('current') is "+ClassRes.get("current"));                 //CurrentSum.set("current",CurrentSum);                                  /*                 var MaxSum = getAttrByName(Selected_Char_Id, "repeating_classresources_$0_ClassResourceTotal_max", "value");                 */                 // sendChat(SpeakingAs,"CurrentSum = " + CurrentSum);                 //CurrentSum = parseInt(CurrentSum,10);                 //MaxSum = parseInt(MaxSum,10);                 // sendChat(SpeakingAs,"CurrentSum = " + CurrentSum);                 // Increment Spell Used                 //CurrentSum = CurrentSum - Selected_ResourceNumberSpent;                              /*                 if (CurrentSum < 0){                     CurrentSum = CurrentSum + Selected_ResourceNumberSpent;                     sendChat(SpeakingAs,"It appears I tried use Ki points that was not available to me!");                 }                  else {                     if(!ClassResource){                         sendChat(SpeakingAs, "It appears the Class Resource I tried to use is Undefined, please examine your characters attributes and try again");                         return;                     }                      else{                        // ClassResource.set("current",CurrentSum);                      //   sendChat(SpeakingAs, ClassResource );                      //   sendChat(SpeakingAs, ClassResource );                     }                 }                 */                                  /*sendChat(SpeakingAs,"CurrentSum = " + CurrentSum);                 sendChat(SpeakingAs,"MaxSum = " + MaxSum);                 */                              } // If command == UseClassResource                                 } // If statement from the UsaClassResource and Api is msg.type     }); });
1591124396

Edited 1591124433
This is what is output to the Chat (I didn't change _name yet for this output) There is supposed to be a SnippetTool picture here... That is unfortunate... perhaps thats why I couldn't post before.
1591124508
GiGs
Pro
Sheet Author
API Scripter
Are you trying to create a script that simply spends points from an attribute? If so, you should look into ammo or chatSetAttrs - they can be used to do this already.
1591124614
GiGs
Pro
Sheet Author
API Scripter
When i post pictures to the forum, I add them to imgur.com first, and copy the lick from there and paste that here. I find roll20 doesnt handle picture uploads very well at all.
I wanted to make a ClassResource Function that I could then manipulate per each character in my Campaign to spend their resources from. I wasn't Sure about Ammo and chatSetAttrs compatibility with Community Contributed Character Sheets. Thanks for the heads up about images.&nbsp; <a href="https://imgur.com/8l4sADT" rel="nofollow">https://imgur.com/8l4sADT</a> That is the error message i get. I'll look into the chatSetAttr, I wouldn't even know where to begin with the ammo one( I have that script downloaded, but everything I looked up on it uses one of the more popular character sheets.
1591126080
GiGs
Pro
Sheet Author
API Scripter
Ammo and chatSetAttr both work with any sheet. You just need to know the attribute names. That error is because the ClassRes = findObjs() function isnt working, likely for the reasons i posted earlier.
Thank you for your assistance. I will probably just use ChatSetAttr, although I have error checking and message in my code that allowed the user to be warned they used a resource that wasn't available... Another Short Question and I think this topic can be closed. Can you call API's inside API scripts? I probably could do some error checking for the values and basically make sure the chatsetattr doesn't execute (or reverse what it did) if its lower than 0... Thanks again for all your help @GiGs
1591129390
GiGs
Pro
Sheet Author
API Scripter
You're welcome :) Yes, you can do that. Just do whatever checking you want to do, then send the command you want to run to chat using sendChat.