Defining a subclass in MV

I'm new to both JavaScript and RPG Maker MV, but not to programming in general and the RPG Maker series of programs, but I'm having trouble understanding how RPG Maker MV's plugins create sub classes.

I get how you define a sub class in JavaScript, but it looks like MV has a specific method of creating classes/sub classes, but I'm not sure.

If it helps I hope to define a subclass to... Actor I think (I'm not at my computer to check) so I can create a Actor that uses stats of two other actors.

But basically since I have a hard time getting into a JavaScript mindset (If it was C++, Java or C# I would be fine) I'd like a little clarification on how MV sets up it's classes if I could.

Thanks in advance. 🙂  
I suggest taking a look at the base scripts:

Code:
function Game_Actor() {    this.initialize.apply(this, arguments);}Game_Actor.prototype = Object.create(Game_Battler.prototype);Game_Actor.prototype.constructor = Game_Actor;
If you don't have an "initialize" method (a constructor in other languages), you can write the first line as "var GameActor = {}", instead.
I probably doesn't help I'm mentally deficient after a work day so maybe I should sit down on my day off, but I look at the code for the base classes and I just don't 'get it'. 

I think I'm so used to other languages, having defined variables, classes and functions that looking at Javascripts more dynamic typing confuses me when things get more complicated then simple examples.

Like why do you set Game_Actor.prototype to Object.create(Game_Battler.prototype)?  my understanding of prototype is that it is similar to a class in other languages, parent class.  Why would you want Game_Actor have it's parent be the parent of Game_Battler, instead of Game_Battler?
"prototype" is like "class" in other languages, so when creating the "Game_Actor" class, you tell it to inherit all fields from the "Game_Battler" class.
I probably doesn't help I'm mentally deficient after a work day so maybe I should sit down on my day off, but I look at the code for the base classes and I just don't 'get it'. 

I think I'm so used to other languages, having defined variables, classes and functions that looking at Javascripts more dynamic typing confuses me when things get more complicated then simple examples.

Like why do you set Game_Actor.prototype to Object.create(Game_Battler.prototype)?  my understanding of prototype is that it is similar to a class in other languages, parent class.  Why would you want Game_Actor have it's parent be the parent of Game_Battler, instead of Game_Battler?
No, it's not.

You should think at a function more as the constructor of your instance,

also every function has a prototype object that your instance will magically refer to in order to resolve your method call

Let' s look at the classical JavaScript way to extend a "Class":

function A(){}A.prototype.foo = function(){console.log('foo')}function B(){}B.prototype = new A()B.prototype.bar = function(){console.log('bar')}var a = new A()var b = new B()a.foo() //foob.foo() //foob.bar() //barconsole.log(a.bar) //undefinedconsole.log(a instanceof A) //true

console.log(a instanceof B )//false

console.log(b instanceof A) //true

console.log(b instanceof B ) //true

 

 

As you can see here b inherits both A and B's prototypes methods

the problem here is that in order to keep B an instance of A you have to actually make B.prototype an instanceof A using new and this runs the function,

this works in this example as the A constructor function takes no parameter and do nothing, but there are cases where you don't know the correct number of parameter

or you just don't want to run the constructor because you want to do that later, http://www.3site.eu/doc/ explains this problem very well and basically shows the solution of using an intermediate parameterless function which shares the same prototype of A so that you can later changes B.prototype without reflecting the changes in A.prototype.

This pattern was so common that we now have Object.create that basically does just like that,

Object.create when called with just one parameter can be summarized as:

Object.create = function (proto) { function Intermediate(){} Intermediate.prototype = proto return new Intermediate()}So, no, prototype is not similar to a class, it's is own thing, I can also suggest, apart for Andrea article I linked before to read

http://dmitrysoshnikov.com/ecmascript/javascript-the-core/#constructor if you want to understand the basic inner working of JS
//What I have done, for my own code:


class MyCustomBaseWindow extends Window_Base {


constructor() {


super();


}


}


// Some Were Else:


class WindowINeed extends MyCustomBaseWindow {


constructor() {


super();


}


initialize(...) {


super.initialize(....);


}


...


}
//What I have done, for my own code:class MyCustomBaseWindow extends Window_Base { constructor() { super(); }}// Some Were Else:class WindowINeed extends MyCustomBaseWindow { constructor() { super(); } initialize(...) { super.initialize(....); } ...}
Please don't take this the wrong way, but isn't this part of something that isn't even part of what JS in MV can do? Some new standard yet to be finalized?

If so, this is really disruptive for people learning how to code in JS (like me atm), when those keywords are part of something that's not yet fully supported.

I mean I get why you do and use that, much easier to do like that, but just seems confusing so far really.

Kinda like someone trying to explain how to code in VX, while using VX Ace code examples.
yeah... it's ruby... not JS. 😀 .

i know we all miss nice and easy going ruby... but admit it... we already have to move on to our new complex girl... JS 😀 .
No, it's ES6, which is basically the next JavaScript version. It simplifies some things, by acting as a thin wrapper around ES5 functions. Makes it easier to write code but has some drawbacks:


- You need to compile your ES6 code to ES5, which might be easy to do, but is an additional step, nevertheless and might be error-prone


- Generated code usually is quite badly formatted and makes your plugin hard to read for other people.


- Since you're already using ES6 syntax, you're running the risk of using functions, that are actually new and not supported in ES5.


- You might "unlearn" (or not learn in the first place) how ES5 works, which means you won't understand most of the plugins out there.
No, it's ES6, which is basically the next JavaScript version. It simplifies some things, by acting as a thin wrapper around ES5 functions. Makes it easier to write code but has some drawbacks:

- You need to compile your ES6 code to ES5, which might be easy to do, but is an additional step, nevertheless and might be error-prone

- Generated code usually is quite badly formatted and makes your plugin hard to read for other people.

- Since you're already using ES6 syntax, you're running the risk of using functions, that are actually new and not supported in ES5.

- You might "unlearn" (or not learn in the first place) how ES5 works, which means you won't understand most of the plugins out there.
Exactly.

I mean I understand why Dark is doing that, but as long its not fully supported, I would really not use it as an example to teach new guys JS.
Exactly.

I mean I understand why Dark is doing that, but as long its not fully supported, I would really not use it as an example to teach new guys JS.
All of my scripts use Es6 classes, accept when I have to actually alias a core scripts method. All of my scripts are then compiled down to ES5 using Browserify and Babelify (Browserify plugin). This makes the end script an unreadable, but runnable and extendable mess. But I also do link to the github repo where you can see the source code before its compiled down.

Its the only way I write JS code for both work and personal jazz.
As long as it works, i'd say it's up to you how you write your plugins, but you will probably have to deal with compatibility issues yourself, since nobody is going to read the messy ES5 and only a few will want to learn a "new" language to understand ES6.
All of my scripts use Es6 classes, accept when I have to actually alias a core scripts method. All of my scripts are then compiled down to ES5 using Browserify and Babelify (Browserify plugin). This makes the end script an unreadable, but runnable and extendable mess. But I also do link to the github repo where you can see the source code before its compiled down.

Its the only way I write JS code for both work and personal jazz.
Like I said, I get why you do it and for your own stuff, go for whats most efficent for you ofc.

Just this section is about learning JS, and specificly in combination with MV, so something that isn't supported by default only confuses people I'd say.

When you disscuss something with other experienced coders sure why not, just for a newbie this just produces more questions, rather than answers.
All MV scripts from me will be ES6 compiled to ES5 with a link to source. Thats all I will be saying on this subject. The internet is full of resources to teach. You just have to be willing to take the time to read, learn and ask. Which a lot of you are, which is nice. I was concerned about this community, but it seems like every one is willing to learn.
All MV scripts from me will be ES6 compiled to ES5 with a link to source. Thats all I will be saying on this subject. The internet is full of resources to teach. You just have to be willing to take the time to read, learn and ask. Which a lot of you are, which is nice. I was concerned about this community, but it seems like every one is willing to learn.
Just want to add my 2 cents here:

Nobody will ever have to going to fix the generated ES5 as usually one can just use sourcemaps for that, you can directly fix the ES 2015 code which is just ES5 with some easy to understand sugar (apart for pattern matching that can be confusing at first).

btw nw.js already supports ES 2015 as the last version is based on io.js (I suppose the next will be Node 4), I guess it's will be not very difficult to upgrade the version shipped with RPGMaker, I will eventually look into it once the OSX standalone version will be available, I'd expect though that at some point we will move towards that anyway.

Original thread: https://forums.rpgmakerweb.com/threads/defining-a-subclass-in-mv.49329/