Unqou

Veteran
Veteran
Joined
Mar 28, 2016
Messages
40
Reaction score
5
First Language
Italian
Primarily Uses
RMMV
I'm writing a plugin for a little game I am developing that will need to load an additional JSON file into a variable used by the plugin itself.

Code:
(function () {
    function MyManager() {
        throw new Error("This is a static class");
    }
   
    MyManager._mydata = null;
  
    DataManager._databaseFiles.push(
       { name: 'MyManager._mydata',       src: 'custom.json'       }
    )   
   
    [...]
})();

Notes:
- custom.json exists and it is placed in "data/" folder together with the default JSon files.
- custom.json is a valid json file that it was generated by a python script I wrote

----

Well... the problem is that MyManager._mydata is actually null and did not load the json data. I'm reading deeper the core JS files and I tried to understand how DataManager.loadDataFile and DataManager.onLoad but I can't understand how (and where) the strings like "$dataActors'' are able to direct the code to actually fill the (global) variable with that name.

Side note: the data contained in custom.json is not an Array of values, but a dictionary that currently contains two keys and their values.

Here also the current version of custom.json:

{"alfa": {"1": {"stats": {"1,1": 1000}, "actors": {"1,1": [17, 18, 19, 20]}}, "2": {"stats": {"1,2": 200, "1,1": 800}, "actors": {"1,1": [17, 18, 19, 20]}}, "3": {"stats": {"1,2": 450, "1,1": 550}, "actors": {"1,1": [17, 18, 19, 20]}}, "4": {"stats": {"1,2": 1000}, "actors": {}}, "5": {"stats": {"1,2": 400, "1,3": 100, "1,1": 500}, "actors": {"1,1": [17, 18, 19, 20]}}, "6": {"stats": {"1,3": 1000}, "actors": {}}, "7": {"stats": {"1,3": 950, "1,4": 50}, "actors": {}}, "8": {"stats": {"1,2": 220, "1,3": 430, "1,4": 350}, "actors": {}}, "9": {"stats": {"1,2": 50, "1,1": 950}, "actors": {"1,1": [17, 18, 19, 20]}}, "10": {"stats": {"1,5": 30, "1,3": 700, "1,4": 270}, "actors": {}}}, "beta": {"0": {"stats": {"1,2": 450, "1,3": 50, "1,1": 500}, "actors": {"1,1": [17, 18, 19, 20]}}, "1": {"stats": {"1,3": 15.0, "1,4": 265.0}, "actors": {}}}}

The two main keys are "alfa" and "beta". Each key contains "index sub-key". Each of index subjey have a "stats" and "actors" dictorinaries.

So my doubt is: how can I load the JSON data in MyManager._mydata
 

Silva

Scoobityboo
Veteran
Joined
Nov 5, 2018
Messages
400
Reaction score
221
First Language
English
Primarily Uses
RMMV
If you look at the way the DataManager loads data files:

Code:
DataManager.loadDataFile = function(name, src) {
    var xhr = new XMLHttpRequest();
    var url = 'data/' + src;
    xhr.open('GET', url);
    xhr.overrideMimeType('application/json');
    xhr.onload = function() {
        if (xhr.status < 400) {
            window[name] = JSON.parse(xhr.responseText);
            DataManager.onLoad(window[name]);
        }
    };
    xhr.onerror = this._mapLoader || function() {
        DataManager._errorUrl = DataManager._errorUrl || url;
    };
    window[name] = null;
    xhr.send();
};

You'll see that it uses window[name], so it's looking for something that's available in the global scope (ie a property of window) - because you're using an IIFE to declare MyManager it's anonymous so this won't work. Additionally the method being used can't access properties of an object eg "MyManager._data" will replace name and DataManager will check if that property exists on "window", but window only has the property "MyManager" (if declared outwith an IIFE).

You would be better served using just a variable, declared outwith the IIFE, and then pushing that as the name for the database files. You can then attach it MyManager or read from it however you please. eg:

Code:
var $myData = null;

DataManager._databaseFiles.push({name: '$myData', src: 'custom.json'});

function MyManager() {
    throw new Error('This is a static class');
};

MyManager.data = function(key) {
    return $myData[key];
};

In my example I've opted not to use an IIFE as it would appear it's not necessary for your use - you may have another reason for using one in which case ensure that $myData and possibly MyManager are declared outwith it so that the information can still be accessed.
 

Unqou

Veteran
Veteran
Joined
Mar 28, 2016
Messages
40
Reaction score
5
First Language
Italian
Primarily Uses
RMMV
In general I use always IIFE in order to reduce risk of conflicts. I will consider to add a global variable like you suggested or think another solution, thank you for your help.

Currently the json file is generated by a python script that parse Actors.json and collect two note tags for each hero. One of those note tags is not actually used by JS code, but it is used to let the python script to capture a secondary information I need to setup per-hero. The other notetag, instead, is used also by JS code (and also by damage formula).
 

Silva

Scoobityboo
Veteran
Joined
Nov 5, 2018
Messages
400
Reaction score
221
First Language
English
Primarily Uses
RMMV
If you're just collecting information from notetags and you're set on using an IIFE why not just process the notetag information using your js plugin, rather than having to run Python separately?
 
Joined
Dec 16, 2017
Messages
215
Reaction score
730
First Language
English
Primarily Uses
RMMZ
I'm not sure if it's possible to do what you're trying to do without using Silva's method. I'm a novice in js, but I've built a plugin that essentially does what you're talking about, and it works similar to the way Silva describes. Maybe there's another way, but that one will work.

Also, Silva's question about reading the notetag information is a good one. Why create a json file that has to be updated via yet another program whenever you make changes in your database when you could just read the tags from the database and manipulate the data from there?
 

Latest Threads

Latest Posts

Latest Profile Posts

Arcade-Style RPGM Games? RM2k3 Game Jam & Synth-Retro Music by Azakaela | RPG Maker News #39

Just evented my first minigame, no plugins~ :kaojoy:
I feel still screenshots don't do my game justice. the characters don't move, but the filter over them does...
Wonder if this year, there be more MZ plugin than MV or that MZ is still small compare to whole MV collection
Zaj
Hey morning all! I got another quick question..I just switched to RMMZ from VX Ace..so I am wondering why default scripts(is that how u call it) of VXAce is available, but not in MZ's case? How do I figure out ahh I mean could you show me

Forum statistics

Threads
111,346
Messages
1,060,378
Members
144,674
Latest member
WaterIce455
Top