Actual JS code from RPG Maker MV

I think this would be good to know for everyone who want to prepare their JS knowledge for RPG Maker MV.

  • The script is wrapped in Javascript's self-executing anonoymous function.
  • There is a class (or object) called "Number" in the screenshot, probably for storing system variables.
  • The class (or object) called "Window_TitleCommand" get 2 new functions by using .prototype (The function names are blurred).
Thus, if you would like to prepare for RPG Maker MV, finding more information about self-executing anonymous function, class (Object-Oriented JavaScript), and prototype in JavaScript is good idea !
Reference: http://www.rpgmaker-mv.com/javascript-not-java/

In order to learn about these things, please don't rely on W3School since some information are inaccurate (see this website for reference: http://www.w3fools.com )

Mozilla developer website and Stackoverflow is your (and my) best bet when it comes to programming question.

I have collected the useful links here for everyone to learn more about the topics mentioned 🙂

These articles would make a good start for RMMV 😀
There was me thinking you had some leaked functions or something 😛
There was me thinking you had some leaked functions or something 😛
I wished I have lol

Even if I have the original scripts, I would be sued if I post that publicly. 😛
Code:
/* * @param Width * @desc japanese text * @default 240 * @param Background * @desc japanese text * @default 0 * @help japanese text*/(function() {    var parameters = PluginManager.parameters['TitleCommandPosition'];    var offsetX = Number(parameters['Offset X'] || 0);    var offsetY = Number(parameters['Offset Y'] || 0);    var width = Number(parameters['Width'] || 240);    var background = Number(parameters['Background'] || 0);    var _Window_TitleCommand_updatePlacement = Window_TitleCommand.prototype.updatePlacement;    Window_TitleCommand.prototype.updatePlacement = function() {        _Window_TitleCommand_updatePlacement.call(this);        this.x = offsetX;        this.y = offsetY;        this.setBackgroundType(background);    };    Window_TitleCommand.prototype.windowWidth = function() {        return width;    };})();
Even if i'm not so used with Js, i understand this snippet. I'm relieved...
Number is acually a default Javascript class, you know, 3 and 3.5 are Numbers not Fixnum nor Float...

So if your code is wrapper on a function, it's sad they didn't add that automatically like node.js wich works with a more "modular" idea...

Javascript has "wrapper classes"  for all of the default types:

Number

Boolean

String

Array

Function

Object

numbers are always floats (even 3) so there is no need for an integer "class" and a float "class"

This line of code (while it's needed for sanity because javascript is a pain) is how cautions you have to be with any type of parameter:

var offsetX = Number(parameters['Offset X'] || 0);This is just to ensure than you have a number... ugh
@Hudell:

Wow o_o This is awesome.

@Ramiro:

Thank you for your explanation ! This is nice trick 😀

I thought using something like parseInt() would solve this problem. But from quick research, it doesn't 🙁
Nope, while Number.parseInt() (or parseInt, both are the same) can transform your string into a number, you can't rely on that.

Why ? because Number.parseInt( 56 ) is of course 'what you may expect': 0...

Tested on chrome console, last versions actually shows 56... Still not secure because older browsers may have different behaviour...

Still, this is one of those horrible edge cases of javascript, you usually don't worry too much to control the inputs, and mention wich type they have to be on the parameter list (like you may want to do in ruby too)

Just remember than integers are something that doesn't exists... this may be important for calculating Hp damage and Mp costs now
Wow, this is actually helpful knowledge 😀
So if your code is wrapper on a function, it's sad they didn't add that automatically like node.js wich works with a more "modular" idea...
Not sure what you mean by this? Are you talking about the self-executing function that surrounds the code?
Not sure what you mean by this? Are you talking about the self-executing function that surrounds the code?
That is exactly what I am complaining about.

You know, in node.js you don't need that, every file you do is a self-executing function that sorrounds the code by itself, and you use others by requiring them ala ruby

//File app.js// Heh!, no need to surroind my code on a function, node does that for me.var express = require('express.js');var app = express();// my code using express goes here...module.exports = app;
Code:
//File index.jsvar app = require('app.js');app.listen(80); // yay!
There is no need to self executing functions with "use strict" as it's first line
Wow! You are JavaScript god, Ramiro 😀

One quick question about "use strict" since you mentioned,

Should we use "use strict" in RPG Maker MV code ?
Wow! You are JavaScript god, Ramiro 😀

One quick question about "use strict" since you mentioned,

Should we use "use strict" in RPG Maker MV code ?
We should use it everywhere, but most people don't :/
There is no need to self executing functions with "use strict" as it's first line
There is no "use strict" in the example.


What are you referring to?
Yes, use it always...
Just for your own sanity...

You don't need to, your code will work fine. But you should for YOUR safety.

There are some bad things about not using strict mode, wich is only like a way "hey, you said to me you won't do that crazy and dangerous action" 

Edit:

Wow! You are JavaScript god, Ramiro  😀
No I'm not a god, I have that fame for no reason... I'm good at it just because I am an engineer and spend a lot of time doing work with javascript... (And trying to implement my own language... xD and made a rgss clone ruby game library)
Good tool for you guys http://jshint.com/
Yup, jshint is specially useful when integrated with your code editor

sample.png
Yes, use it always...


Just for your own sanity...


You don't need to, your code will work fine. But you should for YOUR safety.


There are some bad things about not using strict mode, wich is only like a way "hey, you said to me you won't do that crazy and dangerous action" 


Edit:


No I'm not a god, I have that fame for no reason... I'm good at it just because I am an engineer and spend a lot of time doing work with javascript... (And trying to implement my own language... xD and made a rgss clone ruby game library)
Maybe it will be possible to just introduce a new script that will act as a wrapper over all of the other scripts.


It probably wouldn't be impossible to just load all of the scripts in the project and then just modify them on the fly.
@Tsukihime

Yes, it has... the infamous eval (wich is not allowed on strict mode)...

But we didn't really see anything about the engine, probably this may be a random script from an early build. Or nothing at all...
@Tsukihime


Yes, it has... the infamous eval (wich is not allowed on strict mode)...


But we didn't really see anything about the engine, probably this may be a random script from an early build. Or nothing at all...
MV would probably require eval's for certain portions of the engine, such as damage formulas.

Original thread: https://forums.rpgmakerweb.com/threads/actual-js-code-from-rpg-maker-mv.44112/