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] String Formatter

Those of us used to working in languages that have access to things like sprintf--or even in nodejs--have gotten used to writing our strings with replaceable variables. &nbsp;I've taken the liberty of snagging the util.format function from nodejs, and then optimizing it a bit to run faster. <a href="https://gist.github.com/kkragenbrink/5499147" rel="nofollow">https://gist.github.com/kkragenbrink/5499147</a>
How about explaining how this works for those us us who don't understand it? :D
Sure. &nbsp;Long version:&nbsp; <a href="http://nodejs.org/api/util.html#util_util_format_format" rel="nofollow">http://nodejs.org/api/util.html#util_util_format_format</a> My version: You can use this to perform string simple string formatting. &nbsp;So, for example, you might want to write a custom !roll command for some reason, and while you could just have it output to the chat as a preformatted string. I'm pseudocoding, so bear with me, but you might do something like this: var roll = '1d10+2d6+3'; // From user input. var total = 12; // From random cool code. var rolls = [6, 1, 2]; // From random cool code. var user = 'Cool Player'; // From msg.who; var msgform = '%s rolled %s: %d (rolls: %s)'; // The template you want to output. sendChat(user, format(msgform, user, roll, total, rolls.join(','))); The advantages are many:&nbsp; String concatenation (e.g. string + string + string) is notoriously slow in Javascript. You can preformat your messages in a much easier to read manner. It's actually much shorter, and thus easier for you to read and debug later. It separates the presentation from the logic.
Ah, I see. That is cool. It would be awesome if we could have a way to do an include or require and have a community supported function library somewhere. What little I know about coding, I learned from the old Neverwinter Nights 1 and MapTool. In NWN1, we could create a script library that you could define all kinds of awesome functions in and re-use them as needed just by including that library.
Can you please include the copywrite notice from the original source code in your version of it?&nbsp;
Done and done.