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

[Script] callback issues with send chat

Working on a dice roller using sendChat:

https://gist.github.com/lord-xaphan/153370abf40ca781933b7f3c2cc2cc8c

getting this exception and I can't figure out what I am messing up:

ReferenceError: rollOnce is not defined ReferenceError: rollOnce is not defined at diceAction (apiscript.js:39:39) at apiscript.js:17:19 at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:151:1), <anonymous>:65:16) at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:151:1), <anonymous>:70:8) at checkForReady (/home/node/d20-api-server/api.js:1438:12) at /home/node/d20-api-server/api.js:1518:9 at c (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:14:64) 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)

Still new so probably obvious, thanks in advance

November 06 (6 years ago)
GiGs
Pro
Sheet Author
API Scripter

The rpoblem is in this section of code:

while (diceCounter>0){
        sendChat("", "/r"+diceCounter+"d10>"+targetNumber, function(obs){
            var rollOnce = obs;
        });
        var contentArray = JSON.parse(rollOnce.content);
        diceCounter = contentArray.total;
        diceResults = parseRollArray(contentArray.rolls[0].results);
        actionTotal += 10;
    };

The first problem is you declare rollOnce within a function, but later try to use it outside that function. As far as javascript is concerned, once you leave that function, rollOnce no longer exists. 

You;d need to move the var rollOnce line declaration above, to something like

while (diceCounter>0){
        var rollOnce;
        sendChat("", "/r"+diceCounter+"d10>"+targetNumber, function(obs){
            rollOnce = obs;
        });

That said, this function is constructed very weirdly to me. Can you embed a function like that within a sendChat function, and what purpose does it serve?

Whether it works or not, you'll need a space in "/r" like so "/r ".

November 06 (6 years ago)

Edited November 06 (6 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator

Yep, GG's spotted the cause of your error, but your design also needs some work. sendChat is an asynchronous function, which means that the rest of your code keeps on processing and sendChat gets resolved when there's processing room; this can be almost immediately after it's called, or it might be 30 seconds later (extremely unlikely example). This means that you either need to write your code asynchronously (otherwise known as callback hell aka what we're limited to when coding character sheets), or switch to a promise pattern with or without async/await.

I'd recommend a set up like this. Note: untested code, but should give you an idea of how to use promises and async/await.


EDIT: Also, when using JSON.parse I would highly recommend wrapping your code in a try/catch or putting very sensitive checks in to guarantee what is being parsed is in the proper format to prevent erroneous input from crashing the sandbox.