MZ "TypeError" received while using script calls.

Hi! I am trying to complete examples from a RPG Maker MZ Plugin Tutorial I got from this link and one of the examples tells me to try to run "$dataActors[1].name;" as a script and that it should return the name of the first actor in the Database. However, when I try to run it, I get the message "TypeError Cannot read property '1' of null". I found out this isn't just from "$dataActors[id].name" either, but from every script call I can think of trying related to the database. I am not using any plugins (besides the one I'm practicing with) nor' have I modified the base JS files in any way. I would really like to know what I'm doing wrong because I've been scratching my head over this and trying different things for hours but I haven't been able to find a solution anywhere.
I apologize ahead of time in case the solution is very obvious.
Hi! I am trying to complete examples from a RPG Maker MZ Plugin Tutorial I got from this link and one of the examples tells me to try to run "$dataActors[1].name;" as a script and that it should return the name of the first actor in the Database. However, when I try to run it, I get the message "TypeError Cannot read property '1' of null". I found out this isn't just from "$dataActors[id].name" either, but from every script call I can think of trying related to the database. I am not using any plugins (besides the one I'm practicing with) nor' have I modified the base JS files in any way. I would really like to know what I'm doing wrong because I've been scratching my head over this and trying different things for hours but I haven't been able to find a solution anywhere.
I apologize ahead of time in case the solution is very obvious.
What I suspect you've done here is tried to reference the $data objects in a plugin but not from within any class or function, just a standalone line.

The problem with that is that everything in the outer scope will execute and evaluate before anything else does, so at the point of doing this the data objects don't exist yet.

When it comes time to properly reference those objects in script, you'll likely be editing/writing a function where it will work as intended.
the key to the error is NOT the typeerror, but the "of null" - which basically means that the data object you're trying to reference does not exist.

one of the ways this could happen is if you deleted the actors in the database, but there are other ways as well. For example you could have used the script command with battletest or before the actors are loaded and so on.

so we need a screenshot of the context where you used that script command
What I suspect you've done here is tried to reference the $data objects in a plugin but not from within any class or function, just a standalone line.

The problem with that is that everything in the outer scope will execute and evaluate before anything else does, so at the point of doing this the data objects don't exist yet.

When it comes time to properly reference those objects in script, you'll likely be editing/writing a function where it will work as intended.
I did try to reference $data objects not within any class or function. When I put "$dataActors[1].name;" into a function I was able to run without the error, but when I tried to print what I had in the console by running the function I got the error again. Here is my code:


JavaScript:
function getActorName(){
    let ActorOneName = $dataActors[1].name;
    console.log(ActorOneName);
};
   
getActorName();


I want it printed to the console so I know it's working and I can debug easier with the console later.
I did try to reference $data objects not within any class or function. When I put "$dataActors[1].name;" into a function I was able to run without the error, but when I tried to print what I had in the console by running the function I got the error again. Here is my code:


JavaScript:
function getActorName(){
    let ActorOneName = $dataActors[1].name;
    console.log(ActorOneName);
};
  
getActorName();


I want it printed to the console so I know it's working and I can debug easier with the console later.
You're calling that function before the database has been loaded. If you call the function some time after the game has already properly booted, then it will work. For example:

JavaScript:
(() => {
    const _Scene_Boot_onDatabaseLoaded = Scene_Boot.prototype.onDatabaseLoaded;
    Scene_Boot.prototype.onDatabaseLoaded = function() {
        _Scene_Boot_onDatabaseLoaded.call(this);
        getActorName();
    };

    function getActorName(){
        let ActorOneName = $dataActors[1].name;
        console.log(ActorOneName);
    };
})();
You're calling that function before the database has been loaded. If you call the function some time after the game has already properly booted, then it will work. For example:

JavaScript:
(() => {
    const _Scene_Boot_onDatabaseLoaded = Scene_Boot.prototype.onDatabaseLoaded;
    Scene_Boot.prototype.onDatabaseLoaded = function() {
        _Scene_Boot_onDatabaseLoaded.call(this);
        getActorName();
    };

    function getActorName(){
        let ActorOneName = $dataActors[1].name;
        console.log(ActorOneName);
    };
})();
Thank you! I didn't know the Database had to be loaded or how to do it. This really helps and now I'm not getting the error at all! I appreciate the help a lot and now my issue is solved.
Thank you! I didn't know the Database had to be loaded or how to do it. This really helps and now I'm not getting the error at all! I appreciate the help a lot and now my issue is solved.
No problem. Like Trihan said, in actual practice, it's not an issue that you are likely to need to worry about too often. In a realistic situation where you would need to reference the data objects, the database will likely have already been loaded anyway.
I would like to add something important here...

Where did you plan to use that function?
Why can't you simply call something like $gameActors.actor(actorId) instead?
I would like to add something important here...

Where did you plan to use that function?
Why can't you simply call something like $gameActors.actor(actorId) instead?

In the code snippets shown thus far, that wouldn't work, because $gameActors still wouldn't exist yet--it is created even later than $dataActors is.

As far as in a more general sense, the primary difference between $gameActors.actor(1).name() and $dataActors[1].name is that the former is specific to the game save in question (i.e. if your game allows the player to rename characters, it will fetch the most current name), whereas the latter will only fetch the default name that is defined in the database. The best one to use just depends on what you're trying to do, though I think that $gameActors.actor(1).name() would be the more commonly used one.
In the code snippets shown thus far, that wouldn't work, because $gameActors still wouldn't exist yet--it is created even later than $dataActors is.

As far as in a more general sense, the primary difference between $gameActors.actor(1).name() and $dataActors[1].name is that the former is specific to the game save in question (i.e. if your game allows the player to rename characters, it will fetch the most current name), whereas the latter will only fetch the default name that is defined in the database. The best one to use just depends on what you're trying to do, though I think that $gameActors.actor(1).name() would be the more commonly used one.

Well, I'm sorry but my post was strictly directed at @MeowCatMeow so the talking cat could clarify what the code's actual goals or intentions are. Then it'd be easier to tell the cat avatar what to do next. I mean, who needs to run code right after Scene_Boot finishes its job? Perhaps there's another place, another function where it'd be a lot more convenient to make the call to that custom function. Even so, it's good for that cat in online mode to read your comments on the matter.
I would like to add something important here...

Where did you plan to use that function?
Why can't you simply call something like $gameActors.actor(actorId) instead?
I apologize. I know a wee-bit of JavaScript from a previous job so I'm not jumping into this entirely blind but I might as well be because this is my first time ever attempting to write a plugin. Finding written resources is difficult for me and I run into a new problem every 40-50 seconds after solving my previous one which I get stuck for 4-5 hours on. Honestly I didn't even know that was an option. I have been stuck on an entirely different problem mere minutes after I got help that can prolly' be solved just as fast by someone who knows what they're doing.

My goal here is to develop my own plugin for a problem I had while developing something but I first need to get familiar with all the tools I have so I am simply "playing" with them and seeing if I can make things that function while slowly crawling towards the intended solution to my initial problem. I'm not the type who can just watch an hour long tutorial on it and understand it as I always need to be doing it and seeing it happen in front of me with my own actions or I won't understand.

I also don't want to use premade plugins to accomplish things because I feel that would hinder my growth in the long run if I become reliant on them as that's exactly what happened when I used RMVX and Ace.

I apologize for the confusion.
I mean, who needs to run code right after Scene_Boot finishes its job?
It's not really that uncommon. There are plenty of scenarios where a plugin might need to initialize some variables based on data from the database, and it's an especially good time to parse note tags.

Original thread: https://forums.rpgmakerweb.com/threads/typeerror-received-while-using-script-calls.154670/