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

Status
Not open for further replies.

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
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.

DataMap.png


errornull.png
 
Last edited:

r66r

Fantasy dreamer... sometimes.
Veteran
Joined
Jan 5, 2020
Messages
44
Reaction score
40
First Language
French
Primarily Uses
RMMZ
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)
 
Last edited:

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
@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.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,785
Reaction score
1,632
First Language
English
Primarily Uses
RMMV
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.
 

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
@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
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,917
Reaction score
3,153
First Language
English
Primarily Uses
RMMZ
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)
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,785
Reaction score
1,632
First Language
English
Primarily Uses
RMMV
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.
 

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
@Trihan, of course.

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

CallStack.png
 

Attachments

  • event_count_test.js
    56 bytes · Views: 3

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,917
Reaction score
3,153
First Language
English
Primarily Uses
RMMZ
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.
 

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
@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.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,917
Reaction score
3,153
First Language
English
Primarily Uses
RMMZ
What you would need to do is put that line into something like Game_Map.prototype.setup.
 

r66r

Fantasy dreamer... sometimes.
Veteran
Joined
Jan 5, 2020
Messages
44
Reaction score
40
First Language
French
Primarily Uses
RMMZ
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.
 

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
@Trihan oh, you are the god who gave us "Jump into Javascript"? Awwww!!!!!!
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,917
Reaction score
3,153
First Language
English
Primarily Uses
RMMZ
@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.
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
9,115
Reaction score
5,968
First Language
Dutch
Primarily Uses
RMXP

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.

 
Status
Not open for further replies.

Latest Threads

Latest Profile Posts

Mod name: "[Game name] Gameplay Overhaul"
What they actually mean: "Here is a mod that makes the game harder."
I decided to reinstall one of my most favourite game of all times, Gothic 2. Time to get back to Khorinis!
So much game make... I needs to rest. The foods call.
-Casino with bunnysuit green check.png
1638563168795.png
Monday December 6th 2021 at 7PM Hawkzombie will stream A Thief's Voyage on his Twitch.

Hawkzombie Twitch

Check it out.​

Forum statistics

Threads
117,163
Messages
1,105,063
Members
153,249
Latest member
Jon_Doe
Top