Working with map data

taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,349
Reaction score
4,631
First Language
English
Out of all the things that I have wanted to do with a plugin I am developing, it manipulate some map data. Nothing actually big, just loading and coping, really. I've poked around at it and failed spectacularly, so I'm hoping there is someone familiar enough with how MV handles its maps to help me out.

What I need to do:
  • load map information from a file
  • copy the map object data to a new map object, tied to one of the objects create by my own code
  • as needed, set the map object as above to be the active map and transfer the player accordingly.
In this way, I hope to not need multiple copies of multiple styles of room for a room-based dungeon runner, as I can't have the same map referenced more than once within a single dungeon, because alterations to one room using that map would obviously be visible in another. Handling the map data in the way that I would like means I'm never modifying the maps made, no looping through to reset self switches, and no issues with map count.

I hope I have not explained this too poorly, and that some awesome person that knows maps inside and out can give me a hand, or quite possibly tell me why this idea is idiotic.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,038
Reaction score
10,254
First Language
Czech
Primarily Uses
RMMV
I don't know maps inside out, I'm still in the process of trying to decode what they mean. But all settings necessary are saved in Data/mapXXXX.json.
 

LTN Games

Veteran
Veteran
Joined
Jun 25, 2015
Messages
645
Reaction score
502
First Language
English
Primarily Uses
RMMV
All data, including map data, is loaded with the DataManager class. That is a perfect place to start if you're looking to load any sort of Data. Now, I would actually recommend you create your own function to load the data, because you need to be able to manipulate it the way you want it and not have MV do what it does with it's own maps.

PHP:
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();
};
This function here will load any data file in the data/ directory. This function however will make a new GLOBAL variable, and we don't want that, so you should create your own function with the same way of loading the file, but without the extra boilerplate.
The following function is from my Core plugin, feel free to use it. It's made with ES6 so it may be a bit confusing but It's rather easy to understand and you only need the core concept to make your own.

PHP:
function ajaxLoadFile (path, type = 'application/json', onload, onerror, context) {
        const xhr = new window.XMLHttpRequest()
        xhr.open('GET', path)
        xhr.overrideMimeType(type)
        xhr.onload = () => {
          if (xhr.status < 400) {
            onload.call(context, xhr.responseText)
          }
        }
        xhr.onerror = (err) => onerror.call(context, err)
        xhr.send()
      }
This one is more standard can can be used to load whatever file you would like and then it will send the results in a callback function. So once the file is loaded the onload argument which should be a function will be called with the result of the data passed into it.

PHP:
  function loadSuccess (response) {
    console.log('Success!', response)
    console.log('Parsed Data', JSON.parse(response))
  }

  function loadFailed (error) {
    console.error(error)
  }

  ajaxLoadFile('directory/to/file', 'application/json', loadSuccess, loadFailed)
The above ajaxLoadFile will look for the file, and the type is a json file, then if it finds it and no errors loading it, then it will call the loadSuccess function, otherwise, it will call loadFailed. The context, which is not used in this case is only required when calling this function from a class / constructor function where you need to call a function which has this keyword in it.

For Example, this is where the context argument would be required.

PHP:
  MyClass.prototype.loadSuccess (response) {
    console.log('Success!', response)
    console.log('Parsed Data', JSON.parse(response))
  }

  MyClass.prototype.loadFailed (error) {
    console.error(error)
  }

  MyClass.prototype.loadFile (path) {
    ajaxLoadFile(path, 'application/json', this.loadSuccess, this.loadFailed, this)
  }
Now that that part is out of the way, to actually transfer your player to that map is a bit different, it's something I never really played around with. Although, I'm guessing you just simply assign the global $dataMap variable to your recently parsed map file from above. Then at some point re-initialize the map so it loads the latest changes. I will have to look into this more, later on, maybe I can come up with a quick prototype of what you're trying to achieve

I'm sure some of this is confusing if you've never dealt with it before so feel free to ask questions. I will also tag @Kino because he knows all about loading files/data files as well and may have already done something like what you're asking.
 

taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,349
Reaction score
4,631
First Language
English
@LTN Games You are fantastic! I will process and work on this tonight/tomorrow. This might well take my project from "eh, I can work with this" to just about perfect. :D
 

taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,349
Reaction score
4,631
First Language
English
Okay... Resurrecting my thread (because it's mine AND because it's still the same stuff I've come back to looking at - remember not to necro other people's stuff, guys!).

First, I have it loading map data (yay). It looks like I even have it properly loading the map data (also yay). However, when I attempt to transfer to the room, it looks like it's loading an unrelated (previous map maybe) set of event data. Some digging shows when it's processing the event data, it ties it to a map ID, which in this case is a bit of an issue. I've been stuck on this and foolishly resisting getting help on it because stubborn. I'm all ears (eyes, whatever) if anyone has some awesome ideas.
 

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

Latest Threads

Latest Profile Posts

"If the lion knew her own strength, hard were it for anyone to rule her". This would be my custom title if I had enough spaces for it. Coming off of personal pain, I'm glad I found the original quote I've tweaked.
Today I found out that ladybirds\ladybugs are apparently named for the Virgin Mary. They're also known as God's cows in some places.
J0pac wrote on CrowStorm's profile.
The sad reaction was not because I want you to credit me. (terms clearly say no need to credit) but because those maps are meant to be used as inspiration or guidence. You are ofcourse alowed to use them however you want :)
I'm an adult married woman

Forum statistics

Threads
93,452
Messages
912,528
Members
122,977
Latest member
Midgar21
Top