One instead of two Updates Question

I am trying to understand the order the MV is calling it's functions and there is this weird thing.

As far as I understand .update() is a Javascript function that will be called automatically if it exists.

But there is the Input.update that is called by functions in SceneManager.update(). Shouldn't it be called twice? Once through SceneManager and once through itself?

The whole Input-Class looks the same like the SceneManager class so Update should work there too ... Why is that?

Is it a Javascript-thing that it is smart enough to only make one call when multiple Updates call the function?

Code-Example from the rpg_managers.js and rpg_core.js:

Code:
SceneManager.update = function() {                       //HERE!!	console.log("SceneManager.update");    try {        this.tickStart();        this.updateInputData();        this.updateMain();        this.tickEnd();    } catch (e) {        this.catchException(e);    }};//Calls:SceneManager.updateInputData = function() {	//console.log("SceneManager.updateInputData");    Input.update();    TouchInput.update();};//Calls:Input.update = function() {                   //HERE!	//console.log("Input.update");    this._pollGamepads();    if (this._currentState[this._latestButton]) {        this._pressedTime++;    } else {        this._latestButton = null;    }    for (var name in this._currentState) {        if (this._currentState[name] && !this._previousState[name]) {            this._latestButton = name;            this._pressedTime = 0;            this._date = Date.now();        }        this._previousState[name] = this._currentState[name];    }    this._updateDirection();};
As far as I understand .update() is a Javascript function that will be called automatically if it exists.
That's not right, try creating a random class with an update method and instantiating it, nothing will happen unless you call the update.If you look more, you'll find a method that call SceneManager.update, as well as with every single update method in the whole thing.

Nothing is automatic, it's just a loop calling it so it appear to be automatic, but it's not.
@Masked:

Thanks, that good to know. I googled just for "javascript update" and there was some result that made me convinced that there would be something like that.

But I still wonder where this loop is supposed to be. I first thought that main.js would be called over and over again but it's just called once.

So where is the one function that calls everything? Is it even in one of the 8 .js-files?

I just want to know where this program actually "begins" ....
First, Index.html includes all javascript files:

        <script type="text/javascript" src="js/libs/pixi.js"></script>        <script type="text/javascript" src="js/libs/fpsmeter.js"></script>        <script type="text/javascript" src="js/libs/lz-string.js"></script>        <script type="text/javascript" src="js/rpg_core.js"></script>        <script type="text/javascript" src="js/rpg_managers.js"></script>        <script type="text/javascript" src="js/rpg_objects.js"></script>        <script type="text/javascript" src="js/rpg_scenes.js"></script>        <script type="text/javascript" src="js/rpg_sprites.js"></script>        <script type="text/javascript" src="js/rpg_windows.js"></script>        <script type="text/javascript" src="js/plugins.js"></script>        <script type="text/javascript" src="js/main.js"></script>Almost everything is inside functions, so they don't really do anything yet. They need to be called. Exception being "plugins.js" that hold plugin data for your project, and "main.js":

PluginManager.setup($plugins);window.onload = function() {    SceneManager.run(Scene_Boot);};.onload is a javascript event. It will fire when the container window finishes loading.

In this case, it will call SceneManager.run(Scene_Boot).

Note that it knows what "SceneManager" is because index.html already included all files with all those class definitions.

SceneManager.run = function(sceneClass) {    try {        this.initialize();        this.goto(sceneClass);        this.requestUpdate();    } catch (e) {        this.catchException(e);    }};So, first that that this method does is call for SceneManager.initialize()

SceneManager.initialize = function() {    this.initGraphics();    this.checkFileAccess();    this.initAudio();    this.initInput();    this.initNwjs();    this.checkPluginErrors();    this.setupErrorHandlers();};Those methods are pretty self-explanationary, so we won't delve into them.

It them calls a this.goto(Scene_Boot):

SceneManager.goto = function(sceneClass) {    if (sceneClass) {        this._nextScene = new sceneClass();    }    if (this._scene) {        this._scene.stop();    }};It's the method used for scene transitions. It will set this._nextScene to be a new object of the Scene_Boot class (defined in rpg_scenes.js). But, for now, it doesn't do anything with that yet.

The next method called by init is requestUpdate:

SceneManager.requestUpdate = function() {    if (!this._stopped) {        requestAnimationFrame(this.update.bind(this));    }};Do note, we are not "_stopped" (the value for this starts as false, and nothing yet made it become true). So the condition checks, and we requestAnimationFrame for the function SceneManager.update, being called by SceneManager.

https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

"You should call this method whenever you're ready to update your animation onscreen. This will request that your animation function be called before the browser performs the next repaint. The number of callbacks is usually 60 times per second, but will generally match the display refresh rate in most web browsers as per W3C recommendation."

Ok, but this only class requestAnimationFrame once.... why does it loop? Because the function it calls is SceneManager.Update:

SceneManager.update = function() {    try {        this.tickStart();        this.updateInputData();        this.updateMain();        this.tickEnd();    } catch (e) {        this.catchException(e);    }};More important for this, the this.updateMain:

SceneManager.updateMain = function() {    this.changeScene();    this.updateScene();    this.renderScene();    this.requestUpdate();};First, it checks if there's a need to change scenes (like right now there would be, as we are going to Scene_Boot).

Them, it runs updateScene:

SceneManager.updateScene = function() {    if (this._scene) {        if (!this._sceneStarted && this._scene.isReady()) {            this._scene.start();            this._sceneStarted = true;            this.onSceneStart();        }        if (this.isCurrentSceneStarted()) {            this._scene.update();        }    }};This is the method that starts and updates all scenes.

But that's still not a loop, yet.

But notice that the last call of SceneManager.updateMain is:

this.requestUpdate();And we go back to that. Now we are on a loop, constantly going through it, roughly 60 times per second.

The loop only ever ends if SceneManager._stopped becomes true. And that only happens in error-handling methods (so it only stops when something goes wrong).
@Mellye: Thanks a lot for the really detailed breakdown of that.

I didn't realised that requestAnimationFrame was actually a "special function". I just thought it would be something for refreshing the graphic-sprites or something like that.

Now this whole thing makes sense. Again, thanks a lot! For the useful feedback in the message-window-thread too!

Original thread: https://forums.rpgmakerweb.com/threads/one-instead-of-two-updates-question.49448/