taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,433
Reaction score
5,228
First Language
English
Primarily Uses
RMMZ
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,147
Reaction score
10,708
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

Indie Studio
Veteran
Joined
Jun 25, 2015
Messages
708
Reaction score
638
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,433
Reaction score
5,228
First Language
English
Primarily Uses
RMMZ
@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,433
Reaction score
5,228
First Language
English
Primarily Uses
RMMZ
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.
 

Latest Threads

Latest Profile Posts

Happy Friday, y'all! I just got home from work and am sitting down with a nice cider, and I'm ready to chill and enjoy myself. Not sure if I'm gonna dive into any of my game-related projects or not tonight.... we'll see how I feel after unwinding for a bit I guess!
Just found out that mint tea made from chocolate mint tastes a bit like hot cocoa with candy cane stirred in. As I'm allergic to chocolate this makes me very happy.
It's special day for me tomorrow so time to vanish for 24 hours lol.
So Deltarune Chapter 2 is supposed to release today... if it's out already, has anyone played it? And I can't because I have a stinking Mac and not a Windows PC.

Forum statistics

Threads
115,140
Messages
1,087,559
Members
149,653
Latest member
bboi
Top