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

Asynchronous Scripting

June 15 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
So, I'm looking at using sendChat to parse some rolls. Is it possible to use the values returned from sendChat's callback function within the same function?

ie.
testFunction = function(){
	var rollresult;
	sendChat('', '/r 2d4+2',function(ops){
		rollresult=JSON.parse(ops[0].content).total;
	};
	log(rollresult);
}

June 15 (8 years ago)
Lithl
Pro
Sheet Author
API Scripter
No. A three-parameter sendChat is an asynchronous call. You would have to do something like this:
function testFunction() {
sendChat('', '/ r 2d4+2', function(ops) {
var rollresult = JSON.parse(ops[0].content).total;
log(rollresult);
});
}
June 15 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Hmm, ok. Guess it's time for some more functions then.