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

StayToBack Conflict Issues

1743684248

Edited 1743719465
So I am trying to work on this script that will send a token to the back and keep it there. It was working fine yesterday and today its the one script crashing my game. Could someone take a look at this code and give me insight on it? Perhaps suggest a better way to do it? Since the AoE scripts are currently not working for Jumpgate, I was using this as a quick fix to toss an AoE token on the board and keep it out of the way. Good idea in theory, but my programming skills are remedial at best.  EDIT: The script doesn't crash at startup, but when I bring any other token and put it on the board. Or move it, then it crashes. on('ready',()=>{ const lookWhere = [ 'bar1_value', 'bar1_max', 'bar2_value', 'bar2_max', 'bar3_value', 'bar3_max', 'name', 'gmnotes' ]; const regex = /\[back\]/i; const DELAY = 100; const checkAndSendBack = (obj) => { if (lookWhere.find(k => regex.test(decodeURIComponent(obj.get(k) || '')))) { toBack(obj); } }; // When a graphic is added on('add:graphic', (obj) => { let id = obj.id; let limit = 5; const check = () => { let graphic = getObj('graphic', id); if (graphic) { checkAndSendBack(graphic); } else if (--limit) { setTimeout(check, DELAY); } }; setTimeout(check, DELAY); }); // Ensure token stays in the back when moved or modified on('change:graphic', (obj) => { checkAndSendBack(obj); }); });
1743685946

Edited 1743719697
So, I tried this and got rid of anything trying to keep the token in the background. This seems to work but I'll still welcome any comments. I'm sure I could have used a SpawnDefaultToken script to accomplish this, but I felt it would be less arduous to make a quick script. I was wrong :D on('ready',()=>{ const lookWhere = [ 'bar1_value', 'bar1_max', 'bar2_value', 'bar2_max', 'bar3_value', 'bar3_max', 'name', 'gmnotes' ]; const regex = /\[back\]/i; const DELAY = 100; const checkAndSendBack = (obj) => { if (lookWhere.some(k => regex.test(decodeURIComponent(obj.get(k) || '')))) { obj.set('layer', 'map'); // Sends token to the map layer } }; // When a graphic is added on('add:graphic', (obj) => { let id = obj.id; let limit = 5; const check = () => { let graphic = getObj('graphic', id); if (graphic) { checkAndSendBack(graphic); } else if (--limit) { setTimeout(check, DELAY); } }; setTimeout(check, DELAY); }); });
1743716683
timmaugh
Forum Champion
API Scripter
Can you supply the call stack of the error when it happens? (screenshot or copy the pink box in your script panel when it throws the error). That will help us narrow down what is going on.
Your scripts are currently disabled due to an error that was detected. Please make appropriate changes to your script's code and click the "Save Script" button. We will then attempt to start running the scripts again.  More info...  If this script was installed from the Mod Library, you might find help in the Community API Forum. For reference, the error message generated was:  URIError: URI malformed URIError: URI malformed at decodeURIComponent (<anonymous>) at apiscript.js:61985:40 at Array.find (<anonymous>) at checkAndSendBack (apiscript.js:61985:19) at apiscript.js:62008:5 at /home/node/d20-api-server/pubsub.js:63:16 at Object.publish (/home/node/d20-api-server/pubsub.js:68:8) at TrackedObj.set (/home/node/d20-api-server/api.js:1241:14) at updateLocalCache (/home/node/d20-api-server/api.js:1605:18) at /home/node/d20-api-server/api.js:1841:13
1743720284

Edited 1743721582
One of the "lookWhere" fields on the token that causes the crash possibly has a % in it (not followed by 2 hex digits). That will make decodeURIComponent barf.
Hopefully someone will correct me if I'm wrong but i think out of your "lookWhere" fields, only the "gmnotes" field is URI encoded. So you would only need to call decodeURIComponent() when reading from that field; the other ones should be plain (non-encoded) text.