MZ [Solved] $dataMap showing up as 'null' inside a plugin

I am trying to see how many events I have from inside a plugin. Yet, the code errors out saying I am accessing a null.

JavaScript:
console.log($dataMap.events.length)

However when I ran a console and examine $dataMap (via console.log(top)) it clearly shows that this object is not null.

Could anyone please let me know what I am doing wrong?

Thank you.

Spoiler: Screenshots
DataMap.png



errornull.png
I'm not an expert, but I think $dataMap refers to the list of maps you have created in your game.

To see how many events a specific map has, then you should use $gameMap (corresponding to the current active map). In that case, use this:

Code:
const mapEvents = $gameMap.events() // Gets an array of map events
console.log(mapEvents.length)
@r66r thank you, but it is a bit different.
So, $dataMap is what the world is, and $gameMap is how $dataMap changes during the gameplay.

I need events from $dataMap, but the whole thing is strangely null.
And you're sure you're running it at a time when the player is loaded into a map? That's the only thing I can think of.
@ATT_Turan indeed, my proof is console showing the $dataMap is already initialized -- per screenshot. You can even see the event queue there, the length of which I am requesting.

And yet, when I am referring to $dataMap right after I call "console.log(top)" I get a null.
Absolutely beyond my noob knowledge-level.


DataMap.png
Can you show the stacktrace of the console when the error happens? (When you get that big yellow message, hit F8 and copy *that*. It'll be way more informative to us about where the error is and what's causing it)
I saw your screenshot of the console, but that necessarily can't be happening at the exact moment your code was being run. That's why I asked.
@Trihan, of course.

I attached two items: screenshot of the call stack and the "plugin" itself.

CallStack.png
Of course that's going to break; with the plugin just being that line of code and nothing else, it will try to evaluate the line as soon as the plugin is loaded, since it's not part of a function or class. Plugins are loaded before the $data variables are initialised.
@Trihan, thank you, this nailed it:

"Plugins are loaded before the $data variables are initialised."

The F8 showing $dataMap's components was throwing me off.
What you would need to do is put that line into something like Game_Map.prototype.setup.
Or if you want to have all events for all maps, called at any time during the game, here is the (unoptimized) code I would use:
Code:
MyPlugin.getEvents = function() {
    let events = [];
    $dataMapInfos.forEach((dataMapInfo, i) => {
        if (dataMapInfo) {
            let mapObject = new Game_Map();
            mapObject.setup(dataMapInfo.id);
            const mapEvents = mapObject.events();
            events = events.concat(mapEvents);
        }
    });
    return events;
}

const events = MyPlugin.getEvents(); -> called from a common event or a map event.
console.log(events);

The result is an array of all event objects from all maps.
@Trihan oh, you are the god who gave us "Jump into Javascript"? Awwww!!!!!!
@Trihan oh, you are the god who gave us "Jump into Javascript"? Awwww!!!!!!
I am! It's nice to be recognised. XD I'll be starting that back up soon actually, now that the game jam's over.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-datamap-showing-up-as-null-inside-a-plugin.140482/