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

problem with updating sheetworker script due to order of operations

1642521972

Edited 1642522323
Matthew C
Pro
Sheet Author
Hey, trying to get a sheet worker functioning to update a string used in a custom parser, but for some reason it is not working (and is probably as inneficient as possible) on('clicked:repeating_warantagonist:antagonistattack', async (info) => {         const parseClickTrigger = function(string){           let match = string.match(/clicked:(?:(repeating_[^_]+)_([^_]+)_)?(.+)/);           match.shift();           return match;         };         let check = [];         let infotext = "1";         let text = [];         getSectionIDs("repeating_qualities", function(rowIds) {                     for(var i=0; i < rowIds.length; i++) {                 check.push("repeating_qualities_" + rowIds[i] + "_attach_attack");                 text.push("repeating_qualities_" + rowIds[i] + "_quality_description");             }                     getAttrs(check, function(v) {                 getAttrs(text, function(x) {                     console.log(v);                     _.each(v,(k,l)=>{                         infotext = x[text]="\n";                     });                     //setAttrs(setObj);                 });             });             console.log(infotext);         });         let [section,rowID,button] = parseClickTrigger(info.triggerName);         let name = "@{" + [section,rowID,button][0] + "_" + [section,rowID,button][1] + "_" + "active_name_input" + "}";         let offence = "@{" + [section,rowID,button][0] + "_" + [section,rowID,button][1] + "_" + "action_offence" + "}";         let effect = "@{" + [section,rowID,button][0] + "_" + [section,rowID,button][1] + "_" + "active_prefix" + "}";         let damage = "@{" + [section,rowID,button][0] + "_" + [section,rowID,button][1] + "_" + "action_damage" + "}";         let modifiera = "@{" + [section,rowID,button][0] + "_" + [section,rowID,button][1] + "_" + "action_shards" + "}";         let manipulation = "@{" + [section,rowID,button][0] + "_" + [section,rowID,button][1] + "_" + "action_manipulation" + "}";         let roll = "&{template:antagonistdamage} {{name="+name+"}} {{charactername=@{character_name}}} {{info="+infotext+"}} {{modifier=[["+modifiera+"]]}} {{manipulation=[["+manipulation+"]]}} {{targetname=@{selected|token_name}}} {{damage=[["+damage+"]]}} {{prefix="+effect+"}} {{penalty=[[(?{Attack Penalty|0})]]}} {{penaltyOffence=[[(?{Offence/Alleviation Penalty|0})]]}} {{offence=[["+offence+"]]}}"         await startRoll(roll, (results) => {             finishRoll(                 results.rollId,                 {                     manipulation: results.results.manipulation.result - results.results.penaltyOffence.result,                     offence: results.results.offence.result - results.results.penaltyOffence.result,                     penalty: results.results.penalty.result + results.results.modifier.result                 }                             );         });     }); This is the script, anyone have an idea why the string infotext is not being updated?
1642544657
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Hi Matthew! Welcome to sheetworkers! I see you are using my parseClickTrigger function; I've noted in the below code some details on how the return value of that works. I would recommend putting your utility functions like parseClickTrigger outside of the listeners so that you can call them from any of your listeners. That aside, there are a few issues in your code relating to the asynchronous nature of getAttrs . Anything that you are you going to do with the attributes passed from getAttrs has to be done inside the callback of getAttrs. Additionally, I would highly recommend combining your two getAttrs into a single call. And, finally, startRoll should either be awaited, or used with the callback model. My personal preference is the await method as it allows for much cleaner code. So, with all of that said, here's a rough rewrite of your functions: const parseClickTrigger = function(string){ let match = string.match(/clicked:(?:(repeating_[^_]+)_([^_]+)_)?(.+)/); match.shift(); return match; }; const antagonistAttack = function(info){ let check = []; let text = []; getSectionIDs("repeating_qualities", (rowIds)=>{ rowIds.forEach((id)=>{ check.push(`repeating_qualities_${id}_attach_attack`);//I've switched your string concatenation to string literal notation as it is easier to read and to type. text.push(`repeating_qualities_${id}_quality_description`); }); getAttrs([...check,...text], async (v) => { console.log(v); let infotext = rowIds.map((id,index)=>{ return `${index + 1}: ${v[`repeating_qualities_${id}_quality_description`]}`; }).join('\n'); console.log(infotext); //setAttrs(setObj); let [section,rowID,button] = parseClickTrigger(info.triggerName);//This uses a JS pattern called destructuring and assigns the elements of the array directly to the indicated variable, so you can just call the variables as normal. let rollAttributes = ['active_name_input','action_offence','active_prefix','action_damage','action_shards','action_manipulation'].reduce((memo,name)=>{ //This reduce is probably a little overkill, but I'm a fan of automating string construction as much as possible. Ideally your sheet attribute names would reflec the field they go into in the roll template and then you could do this truly programmatically memo[name].attr = `@{${section}_${rowID}_${name}}`; return memo; },{active_name_input:{field:'name',attr:''}, action_offence:{field:'offence',attr:''}, active_prefix:{field:'prefix',attr:''}, action_damage:{field:'damage',attr:''}, action_shards:{field:'modifier',attr:''}, action_manipulation:{field:'manipulation',attr:''}}); let roll = Object.values(rollAttributes).reduce((text,obj)=>{ return text += `{{${obj.field}=${obj.attr}}}`;//Add the fields },`&{template:antagonistdamage} {{charactername=@{character_name}}} {{info=${infotext}}} {{targetname=@{selected|token_name}}} {{penalty=[[(?{Attack Penalty|0})]]}} {{penaltyOffence=[[(?{Offence/Alleviation Penalty|0})]]}}`); let results = await startRoll(roll);//The roll is sent and when the results are in, the script resumes running const computeObj = {//Using an object to store your results in is a good idea. This lets you build the computed object as needed and lets you log the computed object when your done so you can see where something may have gone wrong. manipulation: results.results.manipulation.result - results.results.penaltyOffence.result, offence: results.results.offence.result - results.results.penaltyOffence.result, penalty: results.results.penalty.result + results.results.modifier.result }; finishRoll(results.rollId,computeObj); }); }); }); on('clicked:repeating_warantagonist:antagonistattack', antagonistAttack);//I prefer to have my listeners call functions and put the code for the listener in it's own function. This helps me read the code. I haven't tested this code since I don't have the html that it depends on, but it should give you an idea of what was going on.
1642545660
Matthew C
Pro
Sheet Author
Oh awesome thanks, I will have to check this out and yeah, I was using your parser originally once, but it got used over and over and I forgot to move it out.
1642655814
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
At the end of the month, I'll be posting a link to a large library of my sheetworker and PUG functions for sheet building. Working on the documentation right now.
1642666857
GiGs
Pro
Sheet Author
API Scripter
That sounds very handy. I've never been able to install PUG, but I'm sure there'll be stuff thats useful to me (and everyone else!).