Ok you were right about initialization, so now I modified the code as such log(finalres);
let object = findObjs({characterid:characterID,name:attributeToSet},{ caseInsensitive: true })[0];
if (object === undefined){
object=createObj("attribute", {
name: attributeToSet,
characterid: characterID
});
}
log(object);
object.setWithWorker({current: finalres});
//object.set({current: finalres});
log(object); Now, what the sheetworker is supposed to updated the value in projection to the rate value, and IF the projection value is lower than the score value, it also modifies wp by the same amount on("change:bond_projection_1 change:bond_projection_2 change:bond_projection_3 change:bond_projection_4 change:bond_projection_5 change:bond_projection_6 change:bond_projection_7 change:bond_projection_8 change:bond_projection_9 change:bond_projection_10 ", function(v){
let proj=v.sourceAttribute;
let tmp=proj.split("_");
let num=tmp[2];
let bond="bond_score_";
bond=bond.concat(num);
getAttrs([bond,proj,"wp"], function(values){
let bdiff=0;
let update={};
bdiff=Math.round(values[bond])-Math.round(values[proj]);
if (bdiff>0){
update[bond]=values[bond]-bdiff;
setAttrs(update);
setAttrs({
wp: (values.wp-bdiff)
});
}
else{
update[bond]=values[proj];
setAttrs(update);
}
});
});
on("ready change:bond_score_1 change:bond_score_2 change:bond_score_3 change:bond_score_4 change:bond_score_5 change:bond_score_6 change:bond_score_7 change:bond_score_8 change:bond_score_9 change:bond_score_10 ", function(v){
let bond=v.sourceAttribute;
let value_n=parseInt(v.newValue,10) || 0;
if (value_n<0){
value_n = 0;
setAttrs({bond: 0});
}
let tmp=bond.split("_");
let num=tmp[2];
let proj="bond_projection_";
proj=proj.concat(num);
let bproj={};
bproj[proj]=value_n;
getAttrs([proj], function(v){
if (v[proj] !== value_n){
setAttrs(bproj);
}
}) ;
});
The worksheet works as a charm when I modify the projection box manually. However, when I set it up using the API script, the code set wp, bond, and projection to 0. The weird thing is that if I use set instead of setWithWorker, the output in projection looks ok. Also, looking at the log with setWithWorker, the value inside the attribute looks ok: {"content":" {{characterid=-M48fgB3sG9ELrSPwLwp}} {{attribute=bond_projection_1}} {{roll=$[[0]]}} {{name=Projection on Name}} {{maxwp=$[[1]]}} {{max_score=$[[2]]}}","inlinerolls":[{"expression":"1d4","results":{"resultType":"sum","rolls":[{"dice":1,"results":[{"v":3}],"rollid":"-M49Pqg_lqDwU4ZzcpCg","sides":4,"type":"R"}],"total":3,"type":"V"},"rollid":"-M49Pqg_lqDwU4ZzcpCg","signature":"535e1467938a9344dc6de0a46958b84ff2028dd58e592b985738598f0d74c5d10c4733385630c32121d01224b6beaf431609162addbed4806500601fa791f263","tdseed":101287523266040340},{"expression":"0","results":{"resultType":"M","rolls":[{"expr":0,"type":"M"}],"total":0,"type":"V"},"signature":false},{"expression":"10","results":{"resultType":"M","rolls":[{"expr":10,"type":"M"}],"total":10,"type":"V"},"signature":false}],"playerid":"-LQrzlnhmmVT5GFwzfLO","rolltemplate":"bond","type":"general","who":"Handler (GM)"}
"character ID: -M48fgB3sG9ELrSPwLwp"
"toSet: bond_projection_1"
"result: 3"
10 // original value
7 // original value - result //value before updating
{"name":"bond_projection_1","current":10,"max":"","_id":"-M49LPYr9ezfc4-_rrnS","_type":"attribute","_characterid":"-M48fgB3sG9ELrSPwLwp"}
//value after updating
{"name":"bond_projection_1","current":7,"max":"","_id":"-M49LPYr9ezfc4-_rrnS","_type":"attribute","_characterid":"-M48fgB3sG9ELrSPwLwp"}
I have no clue where the bug is or if it's a problem with the setWithWorker function