RPG Maker Times

Smile Game Builder and RPG Maker Tutorials, Plugins, Resources, Windowskins

Useful (Missing?) JGSS/Javascript Functions

While looking through various MV functions, notably String and Number, I noticed a distinct lack of them.

So I created some of my own (some of these I already use in Otherworld) and thought I’d share them. And it took me AGES to figure out how to do some of them! No doubt there are better methods for doing these too; they are presented "as is" and they work.

Some are simple, some are well-known, and some aren’t. I put them together (mainly for personal use) in a single plugin, but someone may benefit from any or all of these, easily implemented in your code.

Capitalize First Letter
String.prototype.capitalizeFirstLetter = function() {
    return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
};
Example: ‘hello!’.capitalizeFirstLetter();
Output: Hello!
Capitalize Each Word
String.prototype.capitalizeEachWord = function() {
    return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};
Example: ‘hello world!’.capitalizeEachWord();
Output: Hello World!
Capitalize All Letters
String.prototype.capitalizeAllLetters = function() {
     return this.toUpperCase();
};
Example: ‘Hello world!’.capitalizeAllLetters();
Output: HELLO WORLD!
Reverse Text
As a point of interest, I used this in Otherworld for "mirror reading".
String.prototype.toReverseText = function() {
     return this.split('').reverse().join('');
};
Example: ‘Write this backwards’.toReverseText();
Output: sdrawkcb sith etirW
Reverse Words
String.prototype.toReverseWords = function() {
     return this.split(" ").reverse().join(" ");
};
Example: ‘Write backwards words’.toReverseWords();
Output: words backwards Write
Convert to L337
I couldn’t quite figure this out, but wanted to use this for a certain scene in Otherworld, so credit for this goes to Javascript One-Liners.
String.prototype.toLeet = function() {
     // Credit: Javascript One Liners
     return this.replace(/[a-z]/g,function f(a){return "4BCD3F6H1JKLMN0PQR57"[parseInt(a, 36)-10] || a.replace(/[a-t]/gi,f)});
};
Example: ‘Convert this to Leet’.toLeet();
Output: C0Nv3R7 7H15 70 L337
Decimal Points
Number.prototype.toDecimal = function() {
     return Number(this.toFixed(2));
};
Example: 347.559.toDecimal();
Output: 347.56
Round Up
Number.prototype.toRoundUp = function(decimals) {
     return parseFloat(this.toFixed(decimals));
};
Example: 646.596553.toRoundUp(3);
Output: 646.597

Categories

Archives

Latest Twitter Tweets

Hi-Tex House Pack Vol. 1

For Smile Game Builder

The Hi-Tex House Pack Vol. 1 contains 297 assets: 284 house wall terrains and 13 models. Get your copy for only $5!

Sponsored Ad

Sponsored Ad


© 2008-2018, Companion Wulf. All Rights Reserved.