DataManager loading a custom JSON file

Unqou

Veteran
Veteran
Joined
Mar 28, 2016
Messages
37
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
394
Reaction score
214
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
37
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
394
Reaction score
214
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
115
Reaction score
292
First Language
English
Primarily Uses
RMMV
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?
 

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

Latest Threads

Latest Posts

Latest Profile Posts

purefury wrote on hiddenone's profile.
Hiddenone you are simply incredible, and an inspiration to the community!
https://s12.directupload.net/images/200715/34vwhkoz.png Finally have a concept I am happy with, now I just have to decide wheather I really want to do it as vector art.
I just keep coming up with weird ideas, and then spend the time trying to make sense out of it.:kaocry:
Hi, Erm... My name is zozobie! I just had a snack.
Face masks are to be compulsory when inside shops in England from the 24th of July. People can be fined £100 if they fail to comply.

Forum statistics

Threads
99,692
Messages
968,072
Members
131,381
Latest member
3dprintindex
Top