Do something after map is loaded

Hi guys, so I have been using RPG Maker since VX, used Ace but never really got into it too much, but now with MV and with JavaScript as the scripting language I have gotten back into the RPG Maker series.

So, I have started creating a plugin to make certain Region IDs act as "walls" (I know there are some things like that around already, but I'm doing it more for the practice), and to do that I have given each map a tag ("unw") that gives the ID of what region should act as "unwalkable" (example: <unw:3>), then I'm running the command:

$dataMap['meta'].unw to get the id of each tag, however, what happens is, when I run this command and start the game, the script seems to happen before the map loads returnign the message:

"Cannot read property 'meta' of null", interrupting the rest of the script and preventing it from happening.

So I have been searching with no results for a way of running the "$dataMap['meta'].unw" after the map is loaded, any suggestions?
The best place to put it would be to load that information somewhere in the Scene_Map.prototype methods


For example, you can put it inside of the Scene_Map.prototype.initialize; this method is called whenever a player goes to the map scene. So, in that case.



var SceneMap_initliaze = Scene_Map.prototype.initialize;
//We are aliasing here, so we can get any previous code inside of the initialize function before we change things.

//Next
Scene_Map.prototype.initialize = function() {
SceneMap_initialize.call(this);
//Calling the original code here
//Now, we can write code to look for the regions aka $dataMap stuff.
$dataMap['meta'].unw -- etc
};




This would make sure that $dataMap exists at the time of the call, and is not null.


$dataMap only exists once you're on a map scene anyway.
I'm not sure what you're attempting to do.  If you add a <unw:3> tag in your map's notes, it is already placed into the metadata when you load the map.  You don't have to make it read it or look for it on loading the map.


All you have to add is the logic to check that tag when the player or an event attempts to move onto a tile.  This would be something along the lines of if $dataMap.meta.unw === $gameMap.regionId(x, y) in the Game_CharacterBase.isPassable function, or something like that.


Make sure there's no space between the : and the 3 in the tag, or it may be that you'll need to convert $dataMap.meta.unw to a number in order to compare.  I think if there is no unw metadata (you didn't put the tag on a map) it'll just return null, but that'd be worth verifying too.
The best place to put it would be to load that information somewhere in the Scene_Map.prototype methods


For example, you can put it inside of the Scene_Map.prototype.initialize; this method is called whenever a player goes to the map scene. So, in that case.




var SceneMap_initliaze = Scene_Map.prototype.initialize;
//We are aliasing here, so we can get any previous code inside of the initialize function before we change things.

//Next
Scene_Map.prototype.initialize = function() {
SceneMap_initialize.call(this);
//Calling the original code here
//Now, we can write code to look for the regions aka $dataMap stuff.
$dataMap['meta'].unw -- etc
};




This would make sure that $dataMap exists at the time of the call, and is not null.


$dataMap only exists once you're on a map scene anyway.

Hi,

Thanks for the reply, I will try this once I get home, and I will reply with results

_____________________

I'm not sure what you're attempting to do.  If you add a <unw:3> tag in your map's notes, it is already placed into the metadata when you load the map.  You don't have to make it read it or look for it on loading the map.


All you have to add is the logic to check that tag when the player or an event attempts to move onto a tile.  This would be something along the lines of if $dataMap.meta.unw === $gameMap.regionId(x, y) in the Game_CharacterBase.isPassable function, or something like that.


Make sure there's no space between the : and the 3 in the tag, or it may be that you'll need to convert $dataMap.meta.unw to a number in order to compare.  I think if there is no unw metadata (you didn't put the tag on a map) it'll just return null, but that'd be worth verifying too.


Hey,

Thanks for the reply, I understand that this is how it is supposed to work, however when I launch the game, the game starts by LAUNCHING ALL THE PLUGINS first, and THEN loading the first map.
As a result, the $dataMap['meta'].unw returns null for the very first seconds, resulting in the entire plugin crashing before even having a chance to load a map.

(PS: After writing that I just remembered that maybe a simple 'if' can avoid all this)
The map metadata is loaded when the map is loaded.  If you do everything properly - in the right order - it just works.  If it's not working, I would question why you're trying to refer to metadata on a map prior to the map being completely loaded?


Where exactly are you referring to this info?  I suspect it's not the correct place.
The map metadata is loaded when the map is loaded.  If you do everything properly - in the right order - it just works.  If it's not working, I would question why you're trying to refer to metadata on a map prior to the map being completely loaded?


Where exactly are you referring to this info?  I suspect it's not the correct place.

I have now been able to fix this situation, there were some obvious mistakes that at the time I failed to notice.
Thanks for the help
awesome!  Finding the mistakes yourself is so much better than having other people point them out.  You learn much more from the experience 🙂  


Hope it keeps working for you from here 😉

Original thread: https://forums.rpgmakerweb.com/threads/do-something-after-map-is-loaded.64708/