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


concept: zombie slayer fest.

Lag because of outdated hardware.

Lots todo:
-Terrax lightning should have another step for the flashlight, between up down left right.
-Also Online mv from Nelderson should emit the flashlight from the other player.
-Animation needed for killing the zombie...
Before adding it to #crypchania.
Working on a case. Get all the evidence to prosecute some bad guys. :kaojoy:
But, the game development schedule delayed. :kaoswt2:
The moral of the story: No good deed goes unpunished... I guess...
Except that, nothing to see here. :kaoswt2:
just beat ff7 original for the first time since 2010. having played remake last year, I have a much better appreciation for the original oddly enough. I still like some of the character presentation in remake more, but the original's got a lot going for it.
Ever reach that moment in your life when your juggling a dozen or more projects that you know will never be made but don't have the heart to delete them? Ugh, I hate those moments...
:kaosigh: Why must you hurt me so?

Forum statistics

Threads
111,451
Messages
1,061,206
Members
144,818
Latest member
OMEGASS
Top