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

Difficulty setting attributes

1513119315

Edited 1513119488
Missingquery
Pro
Sheet Author
API Scripter
I am attempting to set the value of an attribute, but it doesn't seem to be working. At the same time, though, the log doesn't return any errors and everything else seems to be working fine up to that point. Here is my relevant code: //credit to Brian on the forums for the basic function framework! on('chat:message', function(msg) {     if (msg.type != 'api') return;     var parts = msg.content.split(' ');     var command = parts.shift().substring(1);     if (command == 'script') {         if (parts.length < 2) {             sendChat('SYSTEM', 'You must provide a selected token id and a target token id.');             return;         }         var selectedId = parts[0];         var targetId = parts[1];         var selectedToken = getObj('graphic', selectedId);         var targetToken = getObj('graphic', targetId);         if (!selectedToken) {             sendChat('SYSTEM', 'Selected token id not provided.');             return;         }         if (!targetToken) {             sendChat('SYSTEM', 'Target token id not provided.');             return;         }         var who = getObj('character', selectedToken.get('represents'));         if (!who) {             who = selectedToken.get('name');         } else {             who = 'character|' + who.id;         }         var attacker = getObj('character', selectedToken.get('represents'));         var defender = getObj('character', targetToken.get('represents'));                  sendChat(who, '/me attacks ' + targetToken.get('name') + '!');                  //Grab basic stats- this isn't relevant here         //Hit/crit/avo/dod         let HitA = getAttrByName(attacker.id, 'hit');         let HitB = getAttrByName(defender.id, 'hit');         let CritA = getAttrByName(attacker.id, 'crit');         let CritB = getAttrByName(defender.id, 'crit');         let AvoA = getAttrByName(attacker.id, 'avo');         let AvoB = getAttrByName(defender.id, 'avo');         let DdgA = getAttrByName(attacker.id, 'lck_total');         let DdgB = getAttrByName(defender.id, 'lck_total');                  let DmgtypeA;         let DmgtypeB;         let DmgA;         let DmgB;                  //Targeted stat //The numeric values of DmgA and DmgB are calculated here                  //Actual battle script- This is the part that doesn't work         //Check if attacker's attack hits         if (randomInteger(100) < (HitA - AvoB)){             sendChat(who,"/me 's attack hits!")             //Check if attack crits             if (randomInteger(100) < (CritA - DdgB)){                 DmgA *= 3;                 sendChat(who,"Crit!")             }             HPB -= DmgA;             log(HPB); //this doesn't work for some reason             defender.set("HP_current", HPB)         } else {             sendChat(who,"/me misses!")         }     } });
It looks like HPB isn't declared anywhere?
1513120788
Missingquery
Pro
Sheet Author
API Scripter
Oh yeah, HPB and HPA are declared in the basic stats section. Sorry about that! They look something like: let HPA = getAttrByName(attacker.id, 'hp_total'); let HPB = getAttrByName(defender.id, 'hp_total');
1513120855
The Aaron
Pro
API Scripter
getattrbyname() just returns the value of an attribute, it doesn’t return a reference to it. You then store that value in a local variable and change the value in that local variable.  If you want to change the attribute, you need to get the attribute object with getObj() or probably more likely findObj(). 
1513121677
Missingquery
Pro
Sheet Author
API Scripter
Well, I changed HPA and HPB to be gotten via findObj(), but the attribute value still isn't being set.
1513121943
The Aaron
Pro
API Scripter
You must use the .get() and .set() functions to modify current and max. 
1513122084
Missingquery
Pro
Sheet Author
API Scripter
defender.set("HP_current", HPB) I've done that here, but it still doesn't affect anything.
1513123393
The Aaron
Pro
API Scripter
There’s a limited subset of properties you can set on each object type:&nbsp; <a href="https://wiki.roll20.net/API:Objects" rel="nofollow">https://wiki.roll20.net/API:Objects</a> attributes are not part of a character, they are associated with it via the character’s id. You need to get the attribute (something like findObj({type:’attribute’,name:’hp’,characterid:defender.id})[0] ) and call .set(‘current’,HBA) on it.&nbsp;
1513193711
Lithl
Pro
Sheet Author
API Scripter
Marth Lowell said: defender.set("HP_current", HPB) I've done that here, but it still doesn't affect anything. defender is a character object, which does not have an "HP_current" property to set. You want to get an attribute object, and call myAttr.set('current', HPB) .