[SOLVED] How to correctly "aliasing"?

Status
Not open for further replies.

Unqou

Veteran
Veteran
Joined
Mar 28, 2016
Messages
40
Reaction score
5
First Language
Italian
Primarily Uses
RMMV
Hi. I'm completely new to JavaScript and I have one question:

if I would like to "aliasing", for example, DataManager.loadDatabase, is this code is considered correct?
Code:
(function() {   
   var _DataManager_loadDatabase = DataManager.prototype.loadDatabase;
    DataManager.prototype.loadDatabase = function() {
        _DataManager_loadDatabase.call(this);
        //adding here my custom changes
    }
})();
If not, how should I alias it? (I searched on the net, but I didn't find anything. I tried to figure myself reading some official .JS plugins included with the engline)

----------------------

Another unrelated question:
I need to "hack" the data initialization, in order to develop (also for RpgMaker MV) a translation system wich will load "on the fly" the desired translation for all the text around in the game.
On RpgMaker VX Ace I could it: I could alias DataManager.init() for my custom data initialization. But RpgMaker MV does not have an "init" subfunction under DataManager.
Excluding DataManager.loadDatabase (wich is the equivalent of DataManager.load_database in VX Ace), wich function should I "alias" wich could be considered "equivalent to" the old DataManager.init() in VX Ace? (I assume it could be not placed under DataManager but elsewhere).

Thank a lot
 

waynee95

Inactive
Veteran
Joined
Jul 2, 2016
Messages
682
Reaction score
598
First Language
German
Primarily Uses
RMMV
That's almost correct.
DataManager is a static class, so it doesn't have a prototype.

Quick example how I do it most of the times:
PHP:
(function () {
    // Alias isDatabaseLoaded function
    var _isDatabaseLoaded = DataManager.isDatabaseLoaded;
    // Variable for checking if our notetags are processed
    var loaded = false;

    DataManager.isDatabaseLoaded = function () {
        // Check if whole database is loaded
        if (!_isDatabaseLoaded.call(this)) {
            return false;
        }

        // If our notetags are not processed, process them
        if (!loaded) {
            this.processWAYNNotetags($dataSkills);
            this.processWAYNNotetags($dataItems);
            this.processWAYNNotetags($dataWeapons);
            this.processWAYNNotetags($dataArmors);
            this.processWAYNNotetags($dataStates);
            loaded = true; // After that set our variable to true
        }
        return true;
    };

    // Function that processes our notetags
    DataManager.processWAYNNotetags = function (group) {
        for (var n = 1; n < group.length; n++) {
            var obj = group[n];
            var notedata = obj.note.split(/[\r\n]+/);

            for (var i = 0; i < notedata.length; i++) {
                var line = notedata[i];
                // Process obj notetags ...
            }
        };
    }
})();
 

Unqou

Veteran
Veteran
Joined
Mar 28, 2016
Messages
40
Reaction score
5
First Language
Italian
Primarily Uses
RMMV
Hmm... I see. Thank for your kind answer, it could be useful for a future. :hhappy:
But I have a question: how to declare if a "class" will be static or if a "class" will have prototypes in JavaScript?
In c++ you can declare static members of a class (with the keyword static) [usually a c++ class can have both static members and "standard" members, wich will require an actual object instance of the class]

[I edited this message completely from scratch. On a first read I didn't understand why you aliased DataManager.isDatabaseLoaded, but I saw a video wich explained some things I was missing (for example the reason why you used the first if to evaluate if the original function returned true or not)].
 
Last edited:

Unqou

Veteran
Veteran
Joined
Mar 28, 2016
Messages
40
Reaction score
5
First Language
Italian
Primarily Uses
RMMV
Update: I tried to implement this code:
PHP:
(function() {
        var startup_translate = false;
        var _isDatabaseLoaded = DataManager.isDatabaseLoaded
         
       function UtrTranslator() {
           throw new Error("This is a static class");
       }
       
       UtrTranslator.test = function() {
           $gameMessage.add("running test function");
       }
        
        DataManager.isDatabaseLoaded = function () {
        // Check if whole database is loaded
        if (!_isDatabaseLoaded.call(this)) {
            return false;
        }
       
        // If startup translation still not applied, than apply it now
        if (!startup_translate) {
            UtrTranslator.test.call()
            startup_translate = true;
        }
        return true;
    };
}) ();
I enabled the plugin in my test_game, but nothing happened. I expected I would read that message before the main menu, but not. Where is the error in this code snippet?
Thank again.
 

waynee95

Inactive
Veteran
Joined
Jul 2, 2016
Messages
682
Reaction score
598
First Language
German
Primarily Uses
RMMV
MV uses ES5 version of JS. In this version a instances of classes are created by using a constructor function and then adding properties and functions to the prototype of that function constructor. In order to create a static class, we just throw an error when someone would try to call the function constructor to create a new instance of that class, like your UtrTranslator. This would be a static class.

The problem with that code is that there's an error. If you open the console (F8 while game is running) you get more info on that.
When the DataBase gets loaded, the game objects will not be initialized yet, so $gameMessage will be undefined. The game objects are only initialized when you start a new game or load from a save file. So you cannot use any of the $game objects until you are in game.
 

Unqou

Veteran
Veteran
Joined
Mar 28, 2016
Messages
40
Reaction score
5
First Language
Italian
Primarily Uses
RMMV
Thank a lot of your precious help.
First of all the 'F8' key was very useful to have a complete report errors and warnings. I could isolate the problems and fix them.

About the $gameMessage possible replacement:
In order to check the code I used console.log (wich output is displayed only on F8 window). But... is there any way to display a infobox or messagebox like the msgbox_p (or something similar) available in VX Ace?
I tried to use eval (wich I though it was a default function available in javascript) but if I use eval("my string") instead of console.log("my string"), RpgMaker MV returns me an error ("undefined identifier")
 

waynee95

Inactive
Veteran
Joined
Jul 2, 2016
Messages
682
Reaction score
598
First Language
German
Primarily Uses
RMMV
eval is a standard function provided by JavaScript. It is used to evaluate/execute JavaScript Code.

PHP:
var x = 10;
var y = 20;
var a = eval("x * y"); // Would be 200
eval("my string") is not valid because it interprets my string as a variable, but that variable is not defined.

I don't know what you mean by "any way to display a infobox or messagebox like the msgbox_p". When you use altert("my string") instead of console.log("my string") it would open a small window that displays that. But the console is much better for debugging than the use of alert.
 

Unqou

Veteran
Veteran
Joined
Mar 28, 2016
Messages
40
Reaction score
5
First Language
Italian
Primarily Uses
RMMV
what I mean was exactly a function that opens small window (so eval is fine). Thank for clarification about "eval" (as I said I am new to JavaScript). :hhappy:
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
15,354
Reaction score
8,533
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

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

Latest Threads

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,860
Messages
1,017,040
Members
137,569
Latest member
Shtelsky
Top