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

Save result of rollable table to use outside its send chat

1399937882

Edited 1399938105
DXWarlock
Sheet Author
API Scripter
Im working a script that does a few if logics to look at different tables. the issue Im running into is I cannot save what a rollable table returns to use later..which means I need to duplicate the entire 'what to do with it' into each table lookup and its messy. for example a snippet of what I'm doing: if (roll >= critRange) { sendChat('', '/roll 1t[HitTable]', function (results) { var text = JSON.parse(results[0].content).rolls[0].results[0].tableItem.name; var output = "Lucky Shot: " + text; }); //FAIL--------------------- } else if (roll <= failRange) { sendChat('', '/roll 1t[Fumble]', function (results) { text = JSON.parse(results[0].content).rolls[0].results[0].tableItem.name; var output = "Fumble: " + text; }); //normal roll--------------------- } I have about 8 of these in the if/else if/else, but they are all along the same lines.. Returns the variable fine nside itself..but if I try to use 'output' anywhere outside that it returns undefined. The problem is I have quite a bit of other things it needs to do for it, send chat, format the text, etc.. It would be a lot easier to be able to just use 'output' of whichever was looked up in my if/else logic, in one final part at the bottom of my script to send to chat, then having to add ALL of the stuff to do inside each table lookup IF. Anyone got any tips? on how to look up in a rollable table and save the variable for outside the 'sendchat' that called it?
1399941118

Edited 1399941192
Lithl
Pro
Sheet Author
API Scripter
You're running into an issue with variable scope. You need to use the variable in the scope you want to have it available in that scope. A simple example: var output = 'foo'; var otherScope = function() { var output = 'bar'; return output; } log(output); log(otherScope()); What you want is to declare your variable at the scope you're going to need it and only declare it at that scope. var output = ''; if (roll >= critRange) { sendChat('', '/roll 1t[HitTable]', function (results) { var text = JSON.parse(results[0].content).rolls[0].results[0].tableItem.name; output = 'Lucky Shot: ' + text; // Note lack of "var" }); } else if (roll <= failRange) { sendChat('', '/roll 1t[Fumble]', function (results) { var text = JSON.parse(results[0].content).rolls[0].results[0].tableItem.name; output = 'Fumble: ' + text; }); } log(output);
1399951551
DXWarlock
Sheet Author
API Scripter
Ah! Ill try that see if that helps, I was pulling my hair out not wanting to repeat the same code 10 times :)