Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,639
First Language
Czech
Primarily Uses
RMMV
Even after I took out the getters and setters entirely it still is NaN.
And how do you fire the update function?

Through
Code:
DayNight.prototype.update()
like in the code you showed in your previous post?
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,639
First Language
Czech
Primarily Uses
RMMV
As I have already said, it cannot work if you do it like that.

Btw. Inheritance in JS is not any more annoying than in any other language. In fact if we transform

Code:
function customClass() {
//code
}

customClass.prototype.customMethod = function () {
//code
}

customClass.prototype.customMethod()
into its C++ counterpart
Code:
class customClass {
public:
customMethod () {
//code
}
};

int main () {
customClass.customMethod();
return 0;
}
and run it through a compiler, the compiler will throw an error, because you're trying to call a non-static function from a class. And Javascript's prototype functions are non-static, meaning they cannot be called from a class itself, they have to be called from an object that's an instance of a class.
If you're interested in learning how object oriented programming works, I suggest you take a C++ course as they cover object oriented programming.
 

Hayolee

Villager
Member
Joined
Jan 18, 2015
Messages
28
Reaction score
0
First Language
English
Primarily Uses
I would say that explains it then. C++ is my least favorite language object-oriented language compared to Java and C#.
 

Hayolee

Villager
Member
Joined
Jan 18, 2015
Messages
28
Reaction score
0
First Language
English
Primarily Uses
Alrighty, posting final modularized and functioning code for anyone who has been following the thread or anyone who finds it in the future and wants a basic example of JS inheritance.

For anyone's future reference, obviously incrementing a counter variable and checking if it's over a certain number is inefficient, but if you haven't read through completely then know I'm quite lazy which is probably very evident.

There's a lot that could be added, but I'm pleased with how it is currently.

Code:
var Hayolee = window.Hayolee || {};

Hayolee.tintSpeed = 60;
Hayolee.dayLength = 5000000;
Hayolee.nightLength = 2500000;


function DayNight(){
    this._currentTime = 0;
    this._isDay = true;
}

DayNight.prototype = Object.create(DayNight.prototype);
DayNight.prototype.constructor = DayNight;

DayNight.prototype.update = function(){

    if ( this._isDay == true){
            if (this._currentTime >= Hayolee.dayLength){
                $gameScreen.startTint([-136, -136, -35, 70], Hayolee.tintSpeed);
                this._isDay = false;
                this._currentTime = 0;
            }
        }
        else {
        
            if (this._currentTime >= Hayolee.nightLength){
                $gameScreen.startTint([0, 0, 0, 0], Hayolee.tintSpeed);
                this._isDay = true;
                this._currentTime = 0;
            }
        }
    this._currentTime ++;
}

//////////////////////////////////////////////
var clock = new DayNight();


const Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
    Game_Interpreter_pluginCommand.call(this, command, args);
    if (command.toLowerCase() === "run") {
        //$gameMessage.add("In the run.");
       clock.update();
    }
 };
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,115
First Language
English
Primarily Uses
RMMV
and wants a basic example of JS inheritance.
Since this is in the Learning Javascript section... :)

Your example doesn't contain any inheritance at all. Inheritance is when one class inherits properties and methods from another, for example, Dog inherits from Mammal inherits from Animal. Inheritance is all throughout the MV code such as Window_Gold inherits from Window_Base.

These lines below are unnecessary, if you were inheriting then you would create the Dog prototype from the Mammal prototype and then add your Dog specific methods to it.
DayNight.prototype = Object.create(DayNight.prototype); DayNight.prototype.constructor = DayNight;
In your case, DayNight isn't inheriting from anything, that line is just creating a DayNight prototype from a DayNight prototype.

Deciding whether you need to bother with that at all is a question of how many times are you going to create an instance of the class? If you're only going to have one instance of DayNight, then you can simplify all of it to use a Plain Old Javascript Object.

Code:
const DayNight = {
    _currentTime: 0,
    _isDay: true
}

DayNight.update = function(){

    if ( this._isDay ){
            if (this._currentTime >= Hayolee.dayLength){
                $gameScreen.startTint([-136, -136, -35, 70], Hayolee.tintSpeed);
                this._isDay = false;
                this._currentTime = 0;
            }
        }
        else {
        
            if (this._currentTime >= Hayolee.nightLength){
                $gameScreen.startTint([0, 0, 0, 0], Hayolee.tintSpeed);
                this._isDay = true;
                this._currentTime = 0;
            }
        }
    this._currentTime ++;
}

Hayolee.sceneMapUpdate = Scene_Map.prototype.update
Scene_Map.prototype.update = function() {
  
   Hayolee.sceneMapUpdate.apply(this, arguments)
   DayNight.update()
}
 

Hayolee

Villager
Member
Joined
Jan 18, 2015
Messages
28
Reaction score
0
First Language
English
Primarily Uses
Thanks!

That's a good idea, you're right. I really don't need any inheritance in this situation. I'll look more into classes and objects in Javascript. I'm too used to using JS for functional programming.
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,115
First Language
English
Primarily Uses
RMMV
I think functional programming has a lot of advantages, and that JS is highly suited to functional programming. There are often concepts where inheritance doesn't make sense in the real world. There's a popular phrase "favor composition over inheritance".

In video games, and especially MV, there's a lot of OOP and inheritance because it actually makes sense, like Game_Actor and Game_Enemy inherit from Game_Battler because they actually do get a lot of benefits of inheriting the properties.

I agree with Poryg that it would help to take a free OOP course online somewhere, but it doesn't have to be C++, you can do Java or Python or whatever you're familiar with.

Lastly I encourage you to check out the class syntax in JS available since ES2015. Unfortunately the MV codebase is written in pre-2015 syntax that's kind of ugly and confusing.

The class syntax more resembles Java
Code:
class Dog extends Animal {
   constructor() {
      super()
      this.noise = "bark"
   }
}
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Are we allowed to post about non-RPG Maker games? And, if so, would any of you be interested in a short, proof of concept type non-euclidian puzzle game?
I should realize that error was produced by a outdated version of MZ so that's why it pop up like that
Ami
i can't wait to drink some ice after struggling with my illness in 9 days. 9 days is really bad for me,i can't focus with my shop and even can't do something with my project
How many hours have you got in mz so far?

A bit of a "sparkle" update to the lower portion of the world map. :LZSexcite:

Forum statistics

Threads
105,883
Messages
1,017,232
Members
137,607
Latest member
Maddo
Top