A warm up for the upcoming RPG Maker MV

Untill they decide to add a new sub-forum, I will post here some things you may want to know about javascript.

We don't really know about how the engine will work, now they will support plug-ins and a lot of features, but here are some 'tricks' to allow people to get a llitle more confident about using javascript, if they came from ruby and only ruby.

Unlike ruby, javascript looks a lot more like c.

If you already look at the code fragments, you will se than I'm using a lot of curly brackets, and that is because javascript does not have 'if x then end' but 'if (x) {  }'

Oh, don't worry is not as bad when you use it some time, but the migration will be a pain...

First of all, you have to remember than javascript does not have integers, like ruby

so, 3 / 2, is 1,5 always (you don't need to put a .0 like 2.0 or 3.0)

So, you still want to do integer division ?

You may prefer one of this two methods:

// By the way, this are line comments, like the '#' in rubyMath.floor(x / y) // Will give the integer part (for example 2.0 for 2.5) // This will work with any kind of integer up to 2^54 ~~(x / y) // A faster way, but only for numbers up to 2^32The second one should be used anytime you can (if you are using the standard damage limit like 99999 this should work fine almost always)

Aliasing is also possible with the new rpg maker, albeit a bit more obscure:

var oldFunctionAlias = oldFunctionoldFunction = function () {   // some code ...   oldFunctionAlias.apply(this, arguments);  // more code}You can also change methods of "classes" (even if, there are no concept of "class" in javascript now, more later)

var oldFunctionAlias = AClass.prototype.oldFunction // "prototype?, don't worry later I will talk about this"AClass.prototype.oldFunction = function () {  // ...}You should ALWAYS ALWAYS ALWAYS declare new variables with 'var':

var x = 23I told you there are no classes in javascript (Unless of course, this version follows the ES6 standard, with I doubt)

And even then, there are no "truly" classes there.

Here, objects are all prototypes, and as such, can extend from other objects.

So, here is like, if you like to "extend" something, you copy it, and then change the things you need.

While in ruby this is also possible (because open classes and monkey patching allow us) Now is the core concept, and not an add-on.

How do I make a class now? Well, here we use functions as constructors (Imagine, you define a class with only its "initialize" method:

function AClass(arg1, arg2) {    this.x = 3; // 'this' is the equivalent of 'self', javascript does not have self};Now, if you want to create a new instance, you just do:

var a_var = new AClass(1, 2);Not so hard, but here comes the tricy part.

Do you want to extend it now ? like ASubclass? let me show you how you need to do it:

function ASubclass() { // code... AClass.apply(this, arguments); // You can call this "how javascript does 'super' and 'alias' " // code...};// Here comes the prototype againASubclass.prototype = Object.create(AClass.prototype);ASubclass.prototype.constructor = ASubclass;Now, let me explain this:

A prototype is an especial value in all functions, its the object wich is passed as "this" when you call that function.

You can use any object really, like a literal object: { x: 2, y: 3 } (more on this later) or a class, like you just saw there.

You need to clone a prototype from the class thou.

Say goodbye to hashes, and hello to bare objects.

Javascript does not have hashes (called normally maps in other language) (well, unless you use the class Map).

The old way to get and set elements from an array is still almost like ruby

var an_array = [1, 2, 3]an_array[2] // Will give you 3, like rubyan_array['2'] // But this will also give 3The problem here is, objects only carry properties as strings, so everything is converted into one.

So, unlike ruby '2' and 2 does not store different values on an object.

Oh, and if you just think that is enough, just considered this:

an_item.a_varis really a short (an probably faster) way to write:

an_item['a_var']Also, multi dimensional arrays are not allowed, well, not like ruby:

an_item[2, 3]  // is invalidan_item[2][3] // is valid, but it also means another thing technicallyBelive me, even when I am planning to create my own language (that's why I am not very active) and kinda like javascript (its not clear my love-hate relation with this language)

 I will indeed miss ruby... for sure.

Hope this mini lessons help you get a warm up from the upcoming scripting, if you are interesting.

if you still want more, you can learn it from some places:

Hope this post will help you decide to learn even more.

Remember to have fun, and stay awesome! 

EDIT:
You may also want to know:

Doing mixins:

function mixin(proto /* , more args */) { for (var i = 1; i < arguments.legth; ++i) {   var obj = arguments;   for (p in obj) {       if (obj.hasOwnProperty(p) && !proto.hasOwnProperty(p)) {           proto[p] = obj[p];       }   } } };You just use like:

mixin(Class.prototype, Mixin1, Mixin2, ...);Arrays, and regexp are almost the same:

var array = [1, 2, 3];
Code:
var regexp = /(.*?)[a-z]$/i;
Som platforms now support string interpolation:

console.log("I have my variable 'x' with the value of %{x}");You can do a -format like- if you prefer with this:

String.prototype.format= function (o) {return this.replace(/{([^{}]*)}/g,function (a, {var r = o;return typeof r === 'string' || typeof r === 'number' ? r : a;});};And use it like:

Code:
"Hello world, mi age is {age}".format({ age: 25 })
I wonder if you are this Ramiro 😀  https://ramirormvxa.wordpress.com/
Yes yami indeed I AM that ramiro/holywyvern 😀

Even my profile pic is the same here and there.

Long time no se Dr. yami ^^

And I always resurrect like a phoenix when a new maker comes out.

Enterbrain uses phoenix down!

Ramiro is ready to battle (again)

By the way, I really want to help like last time on this new maker
Thanks for the warmup. I know a bit about Java, but never learned enough to do anything with it. This might be the nudge I need to dive into it.
Finally, something I can code xD
Good topic Ramiro. Let's get people excited to learn JavaScript :3

Thanks for the warmup. I know a bit about Java, but never learned enough to do anything with it. This might be the nudge I need to dive into it.
I also know about C#, it will help me as much as Java

Of course neither are actually JavaScript

*hugs*
I see I really wonder how they will organise this since....classes don't exist in terms  in javascript

I think the scripts organization will be a total mess for my own opinion UNLESS

they use a modified version of javascripts who permit to implements classes and modules

as I remember HTML5 have sorta a forms of class and module no?

but thanks for the warm up I currently trying to learn it...but this not SO hard to learn due I know ruby and they have some similitude 
What you've got up there is a little much for me right off the bat. I've gone through some of the very basics and have a little more on that to get my head around before I launch into classes and aliasing.


I'm sure nothing will beat actually looking at the scripts when the engine is released and figuring out what things are doing 🙂


Looking forward to getting my hands dirty with a new language - and one that's so widely used!


@Nio, I don't think we'll actually get to code in HTML5. The page says you can EXPORT to it, not develop in it. Any editing of HTML5 (I imagine) would be done outside the editor for the exported version only.
What you've got up there is a little much for me right off the bat. I've gone through some of the very basics and have a little more on that to get my head around before I launch into classes and aliasing.

I'm sure nothing will beat actually looking at the scripts when the engine is released and figuring out what things are doing 🙂

Looking forward to getting my hands dirty with a new language - and one that's so widely used!

@Nio, I don't think we'll actually get to code in HTML5. The page says you can EXPORT to it, not develop in it. Any editing of HTML5 (I imagine) would be done outside the editor for the exported version only.
yeah I want to learn it too actually this seem a funs language even if this seem likes a really messy language 

for the HTLM yeah when I think it's more for the exporting stuff I think :/

ALSO ...they will no need to export your mouse scripts anymore :c unless if the pathfinding is better with your scripts lol
No, the pathfinding with my scripts won't be better. I don't mind that I won't need to convert it. I may make mods to the new one though, if it doesn't do everything (or most things) that mine currently does.


I'd like to see some guidelines on layout & formatting for easy to read scripts - what's the generally accepted best-practice for formatting?
yeah I want to learn it too actually this seem a funs language even if this seem likes a really messy language
1. RGSS3:

msgbox 90071992547409932. JavaScript (you can try it in your web browser's JS console (usually by pressing F12, pasting or writing it and then Enter to evaluate)):

alert(9007199254740993);3. Compare the output! :3
humm well don't see any difference

but  

more I studie the JS more I feel reticent to learn it...I mean I wonder how they would make thie all organized....I have more the impression this will look like a big bunch of codes in ones
I'm actually kinda curious if I could program my own math library so I can get the correct output. Syntax looks like it will take some getting used to but that's to be expected with every language. I'm going to have to get started on some massive studying of the JS language now... Before MV comes out.

@Nio JS returns 9007199254740992 vs 9007199254740993 (trailing number in js is a 2 vs a 3)
How does javascript handle regular expressions?


I was gonna move this to the Learning Ruby and RGSSx forum, but then realized ...


We'll need a few more forums - MV Support, MV Tutorials, Learning Javascript, Javascript Plugins, Javascript Support to name a few.
@Shaz I just had a quick look at this  w3schools js regexp reference but it looks like it will be similar syntax wise.
 JS does not have true inheritance and polymorphism, so when you code tries avoid writing classes like Ruby. I'm glad that they ditch Ruby in favor for HTML 5 and JS.. RM becomes hot again. Hopefully someone can make MMO plugin with node.JS. Seems all previous scripts wont be compatible with MV.
Well, you can do mixing something like this:

function mixin(proto /* , more args */) { for (var i = 1; i < arguments.legth; ++i) {   var obj = arguments;   for (p in obj) {       if (obj.hasOwnProperty(p) && !proto.hasOwnProperty(p)) {           proto[p] = obj[p];       }   } } };and you use it like:

mixin(MyBaseClass.prototype, MyMixin1, MyMixin2, ...);

voilá, ruby-like mixins in javascript
for my personnal opinion once you get used to Function ...it's damn easy to understant haha I am already converting my script for being usable in the futur maker (the litteral coding) not the RGSS exclusive coding (like bgm.new ect) 

so I guess when MV will come out I will be ready ~
One thing I implore in any tutorials you guys use:

Please don't use generic terms like foo and bar, and please use actual working examples, not ones we'll have to fix the syntax for and don't know how because it might be our first tutorial - learning PHP was a nightmare for exactly this reason as most examples were broken anyway and I couldn't learn anything properly from "half-answer" tutorials ;-;
The problem here is, objects only carry properties as strings, so everything is converted into one.
So if I wanted to have an object hold a reference to another object, that's out of the question?

Original thread: https://forums.rpgmakerweb.com/threads/a-warm-up-for-the-upcoming-rpg-maker-mv.43346/