RMMV run script on map load / save changes on $dataMap ?

So, i made a plugin for my game to simplify a process. What it does is basically search for specific tags and add commands in those events, depending on the tags. I mainly made it to simplify dialogue handling. I could go into more details but i doubt it would give anyone any more insight.

The plugin works fine and it can handle whatever i throw at it. However, i have to call it every time i enter a map, leave and reenter it, or save and load the game.

I can think of 2 solutions to this but i don't know how to do them:

1. Run my script whenever a new map is loaded automatically. The problem here is that whatever i tried sees the $dataMap.events as null. This would also have the drawback of spending time to check all the events of the map each and every time.

2. Save the changed contents of $dataMap permanently. This would probably be the best solution, since the script would only run once per map.

2b. If i went with 2, then a good question would be if i could alter all maps from the beginning of the game. Although i don't think that's possible since $dataMap doesn't hold the map's ID and $gameMap doesn't hold the events' command list.

PS: The "changed contents" are basically the same as before - no new events were added - just with extra commands in their command list.
Hi!
I think that save in $dataMap may not be a good idea, since it will "remove" your extra data from there every time you close the game.

Instead, you can try to create your own data and store the information there. Or, use the ones that RM MV puts in the save file($gameSystem, $gameTimer... etc).

and $gameMap doesn't hold the events' command list.
You can access the event command list with this:
$gameMap.event(ID).list()
or
$gameMap.event(ID).page().list

I don't know exactly what you are doing, but isn't a way for you to check if the event has a tag, then you do your thing. Otherwise not?
I think you can try to check and do this, maybe in the Game_Event class?
Hi!
I think that save in $dataMap may not be a good idea, since it will "remove" your extra data from there every time you close the game.

Instead, you can try to create your own data and store the information there. Or, use the ones that RM MV puts in the save file($gameSystem, $gameTimer... etc).


You can access the event command list with this:
$gameMap.event(ID).list()
or
$gameMap.event(ID).page().list

I don't know exactly what you are doing, but isn't a way for you to check if the event has a tag, then you do your thing. Otherwise not?
I think you can try to check and do this, maybe in the Game_Event class?

Hi, thanks for the reply. Just checked it and as soon as i change properties of $dataMap, the properties of $gameMap are also changed. But when i exit and return to the map (or save/load) they are both as they were before my changes.

Now if i add properties to anything else but the list, i have no idea of how to include those commands to the list afterwards. I mean, they have to be at specific places, so i splice them in after the appropriate tag.

About what you said about the tag, that's what i'm already doing. I have tags in the event, and my script looks for any such tags in any event/event page and splices some new commands in there. That's simply to avoid putting the same commands over and over again manually. It works fine with a parallel event (run script -> erase event) but then this would mean that it would have to check all events every single time, to add the same commands - so i thought that's a waste. That's why i wanted to find a way to do it just once.

PS: Also thanks for telling me about the list, i don't know how i missed that
Hi, thanks for the reply. Just checked it and as soon as i change properties of $dataMap, the properties of $gameMap are also changed. But when i exit and return to the map (or save/load) they are both as they were before my changes.
Yes, exactly. That's why I think you should use another global variable to store the changed data($gameSystem... etc) or make your own.

Now if i add properties to anything else but the list, i have no idea of how to include those commands to the list afterwards. I mean, they have to be at specific places, so i splice them in after the appropriate tag.
A simple way of doing that, is to save the changes in a global variable then when load/save the game(or transfer map) you restore/store theses changes.
for example, create a global variable:
Code:
$gameSystem._savedLists = [];

When you made the changes in the list, you can save this list(or the entire event) in the global:
Code:
$gameSystem._savedLists.push($gameMap.event(1))

So whenever a map starts you do:
Code:
if($gameSystem._savedLists.contains($gameMap.event(1)){
$gameMap.event(1) = $gameSystem._savedLists[index]
}

I'm trying to give to you a way. Maybe instead of saving the entire event, you can also save only some properties:
Code:
$gameSystem._data = [
null, // index 0
{event[Id]: list}, // index 1 - You can use the index to reference the map id, or just create a mapId object too.
{event[Id]: list}, // index 2
]
And so on...

About what you said about the tag, that's what i'm already doing. I have tags in the event, and my script looks for any such tags in any event/event page and splices some new commands in there. That's simply to avoid putting the same commands over and over again manually. It works fine with a parallel event (run script -> erase event) but then this would mean that it would have to check all events every single time, to add the same commands - so i thought that's a waste. That's why i wanted to find a way to do it just once.
So if you saved the changes in a global variable, you just have to check if this global variable already contains a saved list. If yes, then you skip the part to add the new commands to events, just copy the list from the global.

Don't know if it will help. Sometimes I get confused to explain something in English xD
Data files generally shouldn't be changed. They represent the "base" game data that is used when you initialize a new game.

All instance data should be saved as external data. If it should be tied to individual save instances, put it with the save file or something.

You could have an "extra map data" object that specifies custom data should be added to each event on map load.

It works fine with a parallel event (run script -> erase event) but then this would mean that it would have to check all events every single time, to add the same commands - so i thought that's a waste. That's why i wanted to find a way to do it just once.

It's probably not going to be an issue if you only do it once when you load the map. Even on weaker devices.
Yes, exactly. That's why I think you should use another global variable to store the changed data($gameSystem... etc) or make your own.


A simple way of doing that, is to save the changes in a global variable then when load/save the game(or transfer map) you restore/store theses changes.
for example, create a global variable:
Code:
$gameSystem._savedLists = [];

When you made the changes in the list, you can save this list(or the entire event) in the global:
Code:
$gameSystem._savedLists.push($gameMap.event(1))

So whenever a map starts you do:
Code:
if($gameSystem._savedLists.contains($gameMap.event(1)){
$gameMap.event(1) = $gameSystem._savedLists[index]
}

I'm trying to give to you a way. Maybe instead of saving the entire event, you can also save only some properties:
Code:
$gameSystem._data = [
null, // index 0
{event[Id]: list}, // index 1 - You can use the index to reference the map id, or just create a mapId object too.
{event[Id]: list}, // index 2
]
And so on...


So if you saved the changes in a global variable, you just have to check if this global variable already contains a saved list. If yes, then you skip the part to add the new commands to events, just copy the list from the global.

Don't know if it will help. Sometimes I get confused to explain something in English xD

Thanks i'm going to try it out. Not sure if it's going to work as intended - but it seems like a step forwards. Thanks!

Data files generally shouldn't be changed. They represent the "base" game data that is used when you initialize a new game.

All instance data should be saved as external data. If it should be tied to individual save instances, put it with the save file or something.

You could have an "extra map data" object that specifies custom data should be added to each event on map load.



It's probably not going to be an issue if you only do it once when you load the map. Even on weaker devices.

I even tried altering the game interpreter while it was executing commands (and it did work! Just not that well xD). Then my next idea now would be to change the list through $gameMap, but isn't that the same thing? I mean, from what i gather, $gameMap gets its properties through $dataMap, so in this particular occasion, it's pretty much the same thing, right? Sorry in general i'm a junior in JS
I even tried altering the game interpreter while it was executing commands (and it did work! Just not that well xD). Then my next idea now would be to change the list through $gameMap, but isn't that the same thing? I mean, from what i gather, $gameMap gets its properties through $dataMap, so in this particular occasion, it's pretty much the same thing, right? Sorry in general i'm a junior in JS

Ya, I think Game_Event just holds a reference to the same pages and commands on each page (doesn't do a deep copy or anything), so if you were to have two instances of the game running at the same time pulling from the same $data objects in memory, you'll get some weird results.

I generally treat the data objects as a "template", and try to exclusively work with the Game objects especially since the Game objects are being dumped into a savefile.
Ya, I think Game_Event just holds a reference to the same pages and commands on each page (doesn't do a deep copy or anything), so if you were to have two instances of the game running at the same time pulling from the same $data objects in memory, you'll get some weird results.

I generally treat the data objects as a "template", and try to exclusively work with the Game objects especially since the Game objects are being dumped into a savefile.

Is it possible to alter an event's list by altering game objects? I just checked the objects.js now and i can't find anything of the sort in there.

Actually, maybe i should also say this. The reason i went into such lengths to do all this, was because i wanted to run a common event through a script, before the interpreter finishes running all the commands. So basically something like this: script (reserves a common event) - > message box
but the common event runs before the message box opens up.

However, i couldn't find how to "force" the common event to run before, or during, the message box command.

(So then i got into what the interpreter does, then i tried pushing the common event at the end of the list, then inserting it right after the script command, etc. etc. and... after many hours of scripting i ended up with this tag-seeking thing)
Common events are handled differently in different contexts.
When you run the "call common event" command in an event, it runs it immediately as part of the interpreter processing.

When you call common event anywhere else (eg: using an item), they will be reserved (only one can be reserved by default) and then executed when the game is "ready". Scene_Map has common event handling, and it won't run common events that are reserved while another event is currently running.

Is it possible to alter an event's list by altering game objects? I just checked the objects.js now and i can't find anything of the sort in there.

Take a look at the map### json files in the data folder and look for your events. There you can see how they are stored. That's basically what an event looks like. Each command is just an object inside the command "list" array.
Common events are handled differently in different contexts.
When you run the "call common event" command in an event, it runs it immediately as part of the interpreter processing.

When you call common event anywhere else (eg: using an item), they will be reserved (only one can be reserved by default) and then executed when the game is "ready". Scene_Map has common event handling, and it won't run common events that are reserved while another event is currently running.

Right... eh i think i'll just go the way Eliaquim suggested. Save the map's id, event's id, page and list and then simply replace the list when you enter the map. Or i'll just abandon this idea altogether - it's starting to look like it will be easier to just add the commands as pseudocode manually xD

Anyway, thanks for your great replies, both of you!

PS:


Take a look at the map### json files in the data folder and look for your events. There you can see how they are stored. That's basically what an event looks like. Each command is just an object inside the command "list" array.

Yeah, i had looked into those before, but i think it's best not to mess with them xD

In theory, i could make an ahk script to open it and add those commands in after i'm done. The script would just open it, look for the tag, then add the appropriate commands in by typing them, as you would do manually. I just think it's best to leave them alone so as not to mess up everything xD
Yeah, i had looked into those before, but i think it's best not to mess with them xD

In theory, i could make an ahk script to open it and add those commands in after i'm done. The script would just open it, look for the tag, then add the appropriate commands in by typing them, as you would do manually. I just think it's best to leave them alone so as not to mess up everything xD

It's for reference to see what an "event command" looks like.
What you're basically doing is creating your own event command object and then inserting it into the list

This is my classic example for editing commands: it intercepts the choice processing and edits the commands-on-the-fly to combine multiple "Show Choice" commands into one.


1595719310279.png
The method @Eliaquim suggested makes sense if this data you're storing is something that could change as the game progresses, but if it's fixed data that will never change, I think altering it on load in $dataMap (without saving $dataMap) is a reasonable solution. Even the engine core code does this kind of thing (when generating the "meta" property from note tags in the "note" box).

If you need to run something when the map has finished loading, I think the best way is to alias Scene_Map.prototype.isReady, like this:
JavaScript:
const old_mapReady = Scene_Map.prototype.isReady;
Scene_Map.prototype.isReady = function() {
    let result = old_mapReady.call(this);
    if(result) {
        // put your code here
    }
    return result;
};
It's for reference to see what an "event command" looks like.
What you're basically doing is creating your own event command object and then inserting it into the list

This is my classic example for editing commands: it intercepts the choice processing and edits the commands-on-the-fly to combine multiple "Show Choice" commands into one.


View attachment 152703

Thanks for this. I was using Yanfly's ExtMes1 which does this? But yeah, i get it, i'm going to study this per your permission? Thanks!


The method @Eliaquim suggested makes sense if this data you're storing is something that could change as the game progresses, but if it's fixed data that will never change, I think altering it on load in $dataMap (without saving $dataMap) is a reasonable solution. Even the engine core code does this kind of thing (when generating the "meta" property from note tags in the "note" box).

If you need to run something when the map has finished loading, I think the best way is to alias Scene_Map.prototype.isReady, like this:
JavaScript:
const old_mapReady = Scene_Map.prototype.isReady;
Scene_Map.prototype.isReady = function() {
    let result = old_mapReady.call(this);
    if(result) {
        // put your code here
    }
    return result;
};

This does exactly what i wanted. Something to constantly do something when the map has finished loading. Thanks! I just called the function i made in "put your code here" and it alters all tags successfully. This opens the path to new ideas too. I had other stuff i wanted whenever a map opens up so it's great! 😀

Original thread: https://forums.rpgmakerweb.com/threads/rmmv-run-script-on-map-load-save-changes-on-datamap.124498/