Yeah, I was thinking about it and the efficiency seemed important in this case. It would be pretty costly to keep doing those findObjs() calls if you were calling it multiple times, so I went for caching. But then I didn't want you to have to restart the API if you added rows, so I needed to invalidate the cache on changes. And then I needed to make sure if you made a function to do the roll that it would get updated... It just seemed like the natural way to go! =D As you may have realized while digging in that code, it doesn't actually roll anything. Inline Rolls are expanded before the message is passed to the API, but the text where the roll was has a placeholder like $[[0]] which is an index into the msg.inlinerolls object which contains the expanded roll. That code I posted up above just takes the result of each of those rolls and substitutes it back into the message text. (in fact, I just copied that block of code straight out of handleInput() from TokenMod). The rollableTable() factory function up above finds the table, then finds all it's items (a separate lookup), then builds an array that contains all of the item's names duplicated as many times as it's weight. That array gets cached and when you call the function, it gets the array from the cache and returns a random entry from it. (or multiple if you provided a parameter). Here's #2. It's not as showy as the above, mainly because there isn't a good reason to do extra special caching. Handout updating is a bit peculiar, so I'm glad to get to write this for you. One thing that has driven me crazy in the past until I figured it out is that you can't get the note's current text and set it in the same function scope. The setTimeout() with the 0 time lets the write happen after the .get() has exited. Without that, it would never make an update because something behind the scenes overwrites any changes you make while in the .get(). Also, if you get a note with nothing in it, it will tell you it's contents are the string 'null'. Both issues are solved with this function. Anyway, here's the function: // Append row(s) to a handout.
var appendNote = function(handoutname){
"use strict";
var msgs = _.flatten(_.rest(arguments)),
note = _.first(findObjs({
type: 'handout',
name: handoutname
}));
if(note && note.get && msgs) {
note.get('notes',function(n){
n = (n && 'null' !== n ? n+'<br>' : '');
setTimeout(function(){
var text=n+msgs.join('<br>');
note.set('notes',text);
},0);
});
}
}; This is just a simple function that takes the name of a handout one or more things to write to it: appendNote('Dinner Options', 'tacos', 'burgers', 'fish'); That will find the note named 'Dinner Options' (case is important) and write 3 lines to it: tacos burgers fish If you have multiple things to write, it's best to do them all in one call as much as possible. Getting the current value of the note is asynchronous, so if you make calls too often, you might end up losing some rows between. You can also call with an array of lines instead of as multiple arguments. This is identical to the above: var food = ['tacos', 'burgers', 'fish'];
appendNote('Dinner Options', food ); Just for fun, here's an example that uses this to write the current time into a handout named 'timelog' once every 5 minutes: on('ready',function(){
"use strict";
appendNote('timelog','Time to start!');
setInterval(function(){
appendNote('timelog', 'the current time is: ',Date().toString(),'');
},5000);
});
And I'm happy to have you still here. It's definitely high praise to be told I'm an important reason for member retention. =D Would that I had more time to write things. I'm sure I could turn out some truly impressive systems! =D Let me know if you need anything else!