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

Schrodingers variable

1605388734
Elgin
Pro
Marketplace Creator
For some reason the act of testing a variable is changing it's value in an imperceptible way. I have this statement "var init_roll = Number(die_value) + Number(initval);" Number(intval) returns the correct value when tested, but the above statement disregards the value of intval and instead sets init_roll to the value of die_value. initval is set by getting the property current from the variable attribute. The above statement will work as expected if I do not test the value of attribute in an earlier command.  If it helps, this script will go through the initiative list and reroll each value. It worked great until I noticed that some icons didn't have the initiative property set which would cause the script to crash. I am trying to write an exception to handle this, and the exception causes the script to treat all initiative modifiers as zero. Can someone help me figure out a good work around? Has anyone had this issue before? Here is my code: const TestStuff = (() => {     let version = '0.0.1',         lastUpdate = 1576528439,         schemaVersion = 0.1,          debugVariable = function(target_var) {     sendChat('',JSON.stringify (target_var)); }, rerollInitiative = function() {     var newturnorder = [];         var turnorder;         var gmturns = 0;         var current_round = 1;         if(Campaign().get("turnorder") == "") turnorder = []; //NOTE: We check to make sure that the turnorder isn't just an empty string first. If it is treat it like an empty array.         else turnorder = JSON.parse(Campaign().get("turnorder"));         for (let i = 0; i < turnorder.length; i++) {             var token = findObjs({_type: "graphic", id: turnorder[i].id})[0];             if (token != null) {                 var initval = 0;                 var layer = token.get("layer");                 var characterid = token.get("represents");                 var attribute = findObjs({type: 'attribute', characterid: characterid, name: 'initiative'}, {caseInsensitive: true})[0];                 if (attribute == undefined) {  // **** THIS IF STATEMENT BREAKS EVERYTHING                     //attribute = findObjs({type: 'attribute', characterid: characterid, name: 'CGen_Init'}, {caseInsensitive: true})[0];                 }                 intval = Number(attribute.get('current'));     debugVariable(intval);                                  var die_value = Math.floor((Math.random() * 20) + 1);     //debugVariable(die_value);                 var init_roll = Number(die_value) + Number(initval); // ****** HERE IS WHERE LOGIC BREAKS DOWN     debugVariable(init_roll);     debugVariable('next');                 if (layer == "gmlayer")                 {                     gmturns = gmturns + 1;                     newturnorder.push({                         id: "-1",                         pr: init_roll,                         custom: "GM"                     });                 }                 turnorder[i].pr = init_roll;                 newturnorder.push(turnorder[i]);             }         }         Campaign().set("turnorder", JSON.stringify(newturnorder.sort(function(a, b){return b.pr-a.pr}))); },     handleInput = function(msg_orig) {         let args,             nl,             longtext,             msg = _.clone(msg_orig);         if ( (msg.type !== "api") || (!playerIsGM(msg.playerid)) ) {             return;         }                  args = msg.content.split(/\s/);                  switch(args.shift()) {             case '!test-stuff':                  rerollInitiative();                 break;         }     },     registerEventHandlers = function() {         on('chat:message', handleInput);     };     return {         RegisterEventHandlers: registerEventHandlers     };      })(); on('ready',function() {     'use strict';     TestStuff.RegisterEventHandlers(); });
1605392627

Edited 1605393038
Pat
Pro
API Scripter
Well, I'm not an expert here - but one thing:   if (attribute == undefined) {  // **** THIS IF STATEMENT BREAKS EVERYTHING          //attribute = findObjs({type: 'attribute', characterid: characterid, name: 'CGen_Init'}, {caseInsensitive: true})[0];  } ...if you use == it allows JavaScript to use falsy values instead of say typof attribute === 'undefined' which will tell you if it is actually undefined instead of false, zero, empty string (''), or null instead.  One other thing: you're debugging intval and not Number(intval), and die_value and not Number(die_value)... I'm not saying they are different, but if you're adding Number(die_value) and then testing die_value... 
1605393028
Elgin
Pro
Marketplace Creator
I tried that after posting, but still breaks the script
1605393103
Pat
Pro
API Scripter
What is the message when it breaks, if I can ask?  Elgin said: I tried that after posting, but still breaks the script
1605394931

Edited 1605395016
Elgin
Pro
Marketplace Creator
attribute is undefined, so the error is something along the lines of cannot 'get' undefined value. Edit: For reference, the error message generated was:  TypeError: Cannot read property 'get' of undefined TypeError: Cannot read property 'get' of undefined at rerollInitiative (apiscript.js:4032:41) at handleInput (apiscript.js:4081:17) at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:154:1), <anonymous>:65:16) at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:154:1), <anonymous>:70:8) at /home/node/d20-api-server/api.js:1661:12 at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560 at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147) at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546) at Id.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:489) at Zd.Ld.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:94:425)
1605395801
Pat
Pro
API Scripter
So likely that means the return value from findObjs({type: 'attribute', characterid: characterid, name: 'CGen_Init'}, {caseInsensitive: true})[0]  ...returns undefined - you're sure that CGen_Init attribute always exists for the passed-in characterid? ...also maybe consider using  getAttrByName for the attribute and   getObj for getting an object when you've got the unique ID? That way you don't have to worry about arrays... 
1605397208
Oosh
Sheet Author
API Scripter
You've got both intval and initval . intval = Number(attribute.get('current')); var initval = 0; I can't see that initval is ever defined as anything except 0? var init_roll = Number(die_value) + Number(initval);
1605397462
Pat
Pro
API Scripter
Good catch. That'd be it.
1605403078

Edited 1605403226
Elgin
Pro
Marketplace Creator
Pat said: So likely that means the return value from findObjs({type: 'attribute', characterid: characterid, name: 'CGen_Init'}, {caseInsensitive: true})[0]  ...returns undefined - you're sure that CGen_Init attribute always exists for the passed-in characterid? ...also maybe consider using  getAttrByName for the attribute and   getObj for getting an object when you've got the unique ID? That way you don't have to worry about arrays...  The variable is set by another script called CreatureGen. All creatures generated by that script have that value set instead of initiative. So if attribute is undefined (if they initiative isn't set) then CGen_Init must be set instead. But that code isn't the issue as you can see it is commented out. The error I showed has a working solution commented out. Thanks Oosh. I was really thinking I found a compiler error here. Too much trauma working in VB I think.