MeowCatMeow

Certified Dunce
Member
Joined
Apr 14, 2015
Messages
23
Reaction score
2
First Language
English
Primarily Uses
N/A
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.
 

Trihan

Speedy Scripter
Regular
Joined
Apr 12, 2012
Messages
6,516
Reaction score
7,023
First Language
English
Primarily Uses
RMMZ
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.
 

Andar

Regular
Regular
Joined
Mar 5, 2013
Messages
39,285
Reaction score
11,469
First Language
German
Primarily Uses
RMMV
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
 

MeowCatMeow

Certified Dunce
Member
Joined
Apr 14, 2015
Messages
23
Reaction score
2
First Language
English
Primarily Uses
N/A
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.
 

Arthran

Regular
Regular
Joined
Jun 25, 2021
Messages
1,085
Reaction score
1,567
First Language
English
Primarily Uses
RMMZ
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);
    };
})();
 

MeowCatMeow

Certified Dunce
Member
Joined
Apr 14, 2015
Messages
23
Reaction score
2
First Language
English
Primarily Uses
N/A
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.
 

Arthran

Regular
Regular
Joined
Jun 25, 2021
Messages
1,085
Reaction score
1,567
First Language
English
Primarily Uses
RMMZ
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.
 

kyonides

Reforged is laughable
Regular
Joined
Nov 17, 2019
Messages
1,740
Reaction score
888
First Language
English
Primarily Uses
RMXP
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?
 

Arthran

Regular
Regular
Joined
Jun 25, 2021
Messages
1,085
Reaction score
1,567
First Language
English
Primarily Uses
RMMZ
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.
 

kyonides

Reforged is laughable
Regular
Joined
Nov 17, 2019
Messages
1,740
Reaction score
888
First Language
English
Primarily Uses
RMXP
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.
 

MeowCatMeow

Certified Dunce
Member
Joined
Apr 14, 2015
Messages
23
Reaction score
2
First Language
English
Primarily Uses
N/A
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.
 

Arthran

Regular
Regular
Joined
Jun 25, 2021
Messages
1,085
Reaction score
1,567
First Language
English
Primarily Uses
RMMZ
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.
 

Latest Threads

Latest Profile Posts

When you're watching a stream with four adult, apparently native English speakers. One has never heard the word "wheedle" and the other three can't readily define it for him. :rolleyes:
Anyone want to do a skill trade with me?
Teach me illustration/drawing/pixel art then I'll teach Javascript/Plugin Development to you :hhappy:
I found drawing is very hard lol.
September is shaping up to be one of the least and most anxiety-inducing months of my life so far. Crazy how much can happen within a single month :rtear:
I've started proper work on Snapdrake. Got the hang of tiling and im feeling alright about these
Sally 1D[face].png
All 8 portraits done, and 8 faces to go with. Put the blonde here to please a couple of members XD

Forum statistics

Threads
134,689
Messages
1,249,758
Members
177,432
Latest member
frank88
Top