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

Function repeating? Really weird error

1614135909
Joshua S.
Pro
Sheet Author
API Scripter
I'm getting a really weird error on a function I am working on. I've put lots of logs in there to try to track it down. This function should take the notes section from one handout and make a new handout with the same notes section. The notes section is long, so I've abbreviated it in this code on('chat:message', (msg) => { if ('api' === msg.type && /!test\b/i.test(msg.content)) { const args = msg.content.split(/\s+--/); log('start test'); let handout = findObjs({ _type: "handout", name: "README" })[0]; //log(handout); let text = 'nothing'; handout.get("notes", function(notes) { //log(notes); //do something with the character bio here. text = notes; }); log('after handout.get'); createObj("handout", { name: "README ALSO", inplayerjournals: "all" }); log('after createObj'); let newhandout = findObjs({ _type: "handout", name: "README ALSO" })[0]; log('after newhandout'); log(text); newhandout.set({notes: text}); log('after set'); } }); Here is the console logs it produces: "start test" "after handout.get" "after createObj" "after handout.get" "after newhandout" "<p>Skall! Welcome to the Fate of the Norns Companion Script. This script will enhance the ... and then re-check the runes under actives. </p>" "after createObj" "after newhandout" "nothing" "after set" "after set" This run produced two handouts, one with "nothing" in the notes area and one with the "<p>Skall..." text. It doesn't always do this, sometimes it produces one handout with "nothing", sometimes it produces one handout with "<p>Skall...". This is the first run where I put in all of the logs to track the flow of the script. I am stumped. I've tried it with var instead of let and its still wonky.
1614137794

Edited 1614139740
timmaugh
Pro
API Scripter
Getting the notes of a handout is asynchronous, so when you set the text variable in the callback... handout.get("notes", function(notes) { //log(notes); //do something with the character bio here. text = notes; }); It isn't actually filled immediately. Your post-callback code continues running. You'll need to embed everything else downstream in that callback, or break the downstream stuff into its own function and call it from the callback (that can help it "read" (to a human) as synchronous code). Try this... (air coded): on('chat:message', msg => { if (!('api' === msg.type && /!test\b/i.test(msg.content))) return; const args = msg.content.split(/\s+--/); let handout = findObjs({ _type: "handout", name: "README" })[0]; let text = 'nothing'; handout.get("notes", notes => { //do something with the character bio here... let newhandout = findObjs({ _type: "handout", name: "README ALSO" })[0] ||             createObj("handout", { name: "README ALSO", inplayerjournals: "all" }); newhandout.set({notes: notes}); }); });