load map.json, assign to variable

Elder

Villager
Member
Joined
Mar 14, 2012
Messages
21
Reaction score
5
First Language
French
Primarily Uses
In VX using Ruby, loading a map and assigning it to a variable was so simple and easy:

map_id = 1@map = load_data(sprintf("Data/Map%03d.rvdata2", map_id))I have some issues doing the same thing in MV. Like probably many of you, I'm inexperience with javascript and web programming, I'll surely learn the trick of the trade, but I'm dealing with a serious deadline right now, and I need to migrate to MV as soon as possible. It's probably one of the only challenge I've met thus far.

Here's some default code snippets from MV to put everyone on the right track.

DataManager.loadMapData = function() {    if (mapId > 0) {        var filename = 'Map%1.json'.format(mapId.padZero(3));        this.loadDataFile('$dataMap', filename);    } else {        this.makeEmptyMap();    }};This is probably the most overwhelming part for most people unfamiliar with web programming.

DataManager.loadDataFile = function(object, filename) {    var xhr = new XMLHttpRequest();    var url = 'data/' + filename;    xhr.open('GET', url);    xhr.overrideMimeType('application/json');    xhr.onload = function() {        if (xhr.status < 400) {            window[object] = JSON.parse(xhr.responseText);            DataManager.onLoad(window[object]);        }    };    xhr.onerror = function() {        DataManager._errorUrl = DataManager._errorUrl || url;    };    window[object] = null;    xhr.send();};Here again, $dataMap make this function inflexible. 

Code:
DataManager.onLoad = function(object) {    var array;    if (object === $dataMap) {        this.extractMetadata(object);        array = object.events;    } else {        array = object;    }    if (Array.isArray(array)) {        for (var i = 0; i < array.length; i++) {            var data = array[i];            if (data && data.note !== undefined) {                this.extractMetadata(data);            }        }    }};
So well, I'm wondering if anyone can put together a quick function to assign a map to a variable and then read its properties. Example: loaded_map.width, etc. My current tests didn't work. 

Thank you for reading and investigating this topic with me. Anyone helping can be credited as special thanks in my game, if interested. (find more about it here: www.blossomsoft.com/ )
 
Last edited by a moderator:

Elder

Villager
Member
Joined
Mar 14, 2012
Messages
21
Reaction score
5
First Language
French
Primarily Uses
Here's what I tried to do thus far as a test:

Code:
DataManager.loadDataFile_2 = function(object, filename) {    var xhr = new XMLHttpRequest();    var url = 'data/' + filename;    xhr.open('GET', url);    xhr.overrideMimeType('application/json');    xhr.onload = function() {        if (xhr.status < 400) {            window[object] = JSON.parse(xhr.responseText);            DataManager.onLoad_zzz(window[object]);        }    };    xhr.onerror = function() {        DataManager._errorUrl = DataManager._errorUrl || url;    };    window[object] = null;    xhr.send();};
Code:
DataManager.onLoad_2 = function(object) {    var array;        this.extractMetadata(object);    array = object.events;            if (Array.isArray(array)) {        for (var i = 0; i < array.length; i++) {            var data = array[i];            if (data && data.note !== undefined) {                this.extractMetadata(data);            }        }    }    };
Code:
	var mapId = 1;	var my_map = {};	var filename = 'Map%1.json'.format(mapId.padZero(3));		DataManager.loadDataFile_2('my_map', filename);		var test = my_map.width; // map doesn't seem assigned.
 
Last edited by a moderator:

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
Please, if you need to load a file, dont use the default loading functions

They are terrible, and only create global variables. You will need, at the very least, a plugin that allows you to load data into the game.

At this time, the only plugin I can think of off the top of my head is the MV Commons plugin. However, as I'm on my phone, I cannot link it to you. Please search for, and download that plugin.

Part of that plugin is some code that lets us download data, which you can to get the map itself. Simply use MVC.ajaxLoadFile("data/Map001.json"), and you will have the map data for map 1.

PLEASE NOTE: This will not give you a Game_Map object. Doing that is considerably more complicated. But this will give you enough to get the raw map data, including the map width and height. You'll be able to access it similarly as in Ace. Whatever variable name you use to store the map data, just use ".width" and ".height" to get the width and bright, respectively.
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
This is the basic function i'm using for loading files:

Code:
var loadFile = function(url, callback) {    var xhr = new XMLHttpRequest();    xhr.open('GET', url);    xhr.overrideMimeType('application/json');    xhr.onload = function() { callback(JSON.parse(xhr.responseText)); };    xhr.onerror = function() { throw new Error("There was an error loading the file '" + url + "'."); };    xhr.send();};
You can call it like this:
Code:
var map;loadFile('data/Map001.json', function(data) { map = data; });
and it will store the loaded map data in "map", once it finishes.
 

Elder

Villager
Member
Joined
Mar 14, 2012
Messages
21
Reaction score
5
First Language
French
Primarily Uses
Zalerinian, wonderful, I will definitely check out this plugin.

Iavra, wow you just saved my life tonight!

Thanks a bunch for your replies!
 

Elder

Villager
Member
Joined
Mar 14, 2012
Messages
21
Reaction score
5
First Language
French
Primarily Uses
Actually, Iavra, there's a small issue. 

based on your example, 

var map;loadFile('data/Map001.json', function(data) { map = data; });if I try to get map.width, it return me this when I store the value in a string to check the value, rather than returning a number

function() { return $gameMap.width; }So how do I get the properties from your example? 
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
I just tried it and it works. Was something else saved in map beforehand? The data is loaded asynchronously, so you'll need to wait until it finishes or write all logic that deals with it in the callback. If you know beforehand, what data you need, i suggest loading it in Scene_Boot.prototype.create and return false in Scene_Boot.prototype.isReady untile the data is loaded.
 

Elder

Villager
Member
Joined
Mar 14, 2012
Messages
21
Reaction score
5
First Language
French
Primarily Uses
Thanks for the support, Iavra. I didn't know the data is loaded asynchronously. 

Zalerinian, I'm currently investigating your document and it looks very interesting.
 

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,309
Reaction score
531
First Language
indonesian
I just tried it and it works. Was something else saved in map beforehand? The data is loaded asynchronously, so you'll need to wait until it finishes or write all logic that deals with it in the callback. If you know beforehand, what data you need, i suggest loading it in Scene_Boot.prototype.create and return false in Scene_Boot.prototype.isReady untile the data is loaded.
this is interesting... any example on how to know that data is finish loaded?

i notice that when using asynchronous load and data not finish loading when we changing map... sometimes it throw $dataMap null error.
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
In my example, as long as map === undefined, it's not loaded. I never got the $dataMap error, but i never change maps, because my "game" only has a single test map :D
 
Last edited by a moderator:

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

Latest Threads

Latest Profile Posts

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
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

Forum statistics

Threads
105,868
Messages
1,017,070
Members
137,577
Latest member
SadaSoda
Top