JS-Noob and the instant plugin failure

I'm a noob who's trying to learn javascript through RMMV. So far I have worked through the issues I encountered with the aid of multiple tutorials, but now I have a problem and I can't for the life of me find any mention of something like this no matter how much I google.

My plugins crash instantly whenever I start the game.

The lines causing the problem work fine if used manually through the developer console in the middle of the game.

I imagine there's something simple I'm missing, but I can't figure out what and so far I haven't found a tutorial that mentions something like this.

Just to prove my point I made a 1 line plugin. When I playtest the game and hit F8. I am instantly told by the console: ".../js/plugins/1test.js:1 Uncaught TypeError: Cannot read property 'actor' of null"

if I type it manually?

"$gameActors.actor(1).hp" results in "100", which is the hp of the main character.

This is the entirety of the plugin:

$gameActors.actor(1).hp
Your plugin is probably executed before the game is started.


But $gameActors only exist after either "new game" or loading of a savefile.


You need to make sure that the plugin is only executed after a game is started, not during the title screen.


I've moved this thread to Learning Javascript. Please be sure to post your threads in the correct forum next time. Thank you.


(Plugin Support is for existing plugins, not when you try to write it)
$gameActors gets created the first time during Scene_Boot.start, because MV actually creates a new game, before entering Scene_Title.
I figured it was something like that.

As I said, I'm a noob.

How do I tell the plugin to wait until something is loaded?

Any kind of workaround would be greatly appreciated.

Edit:

Actually it seems this worked. No error message on playtest.

while ($gameActors === undefined) {

setTimeout(

$gameActors.actor(1).hp

, 1000) }

 

Edit#2: No, not really. Seems I can't put functions (or at least multiple functions) inside that.
For the rest are you trying to just get the character hp, yes? 
Question is what time do you want to check it? Usually the best place would be during a scene.
As Iavra said, $gameActors doesn't exist until Scene_Boot.start.
So if you wanted to check Actor HP at the start of the game in the console, or modify it.

//Alias Scene_Bootvar Scene_Boot_start = Scene_Boot.prototype.start;Scene_Boot.prototype.start = function() {//Lets Scene boot run it's original codeScene_Boot_start.call(this);//Outputs game actor 1 to the consoleconsole.log($gameActors.actor(1));//Output game actor hp?console.log($gameActors.actor(1).hp);//Update gameActor hp$gameActors.actor(1).hp = 600;};So during the Scene_Boot, the hp will be updated to 600 in this case. The actor object, and actor hp will also appear in the console.
For the rest are you trying to just get the character hp, yes? 

Question is what time do you want to check it? Usually the best place would be during a scene.

As Iavra said, $gameActors doesn't exist until Scene_Boot.start.

So if you wanted to check Actor HP at the start of the game in the console, or modify it.

//Alias Scene_Bootvar Scene_Boot_start = Scene_Boot.prototype.start;Scene_Boot.prototype.start = function() {//Lets Scene boot run it's original codeScene_Boot_start.call(this);//Outputs game actor 1 to the consoleconsole.log($gameActors.actor(1));//Output game actor hp?console.log($gameActors.actor(1).hp);//Update gameActor hp$gameActors.actor(1).hp = 600;};So during the Scene_Boot, the hp will be updated to 600 in this case. The actor object, and actor hp will also appear in the console.
I'm not actually trying to accomplish anything specific. I just seemed to hit that brick wall no matter what I tried.

But in essence the point of this test script was just to check if my character has a hp value and this seems to have done it. I'll have to put some time into using this new information. A thousand thanks!

EDIT: in case someone else uses this solution, it appears you cannot have multiple lines like "Scene_Boot_start.call(this)". Had the test plugin active and tried making another that uses the same starting time and got an error about the call length.
To answer that, the reason why is because, the code is running itself again when you do another Scene_Boot...call(this).

It reruns a functions code, so if you had multiple..it executes itself multiple times, until it reaches the maximum stack size, or call length. It can be helpful, if you want to use recursion, but I wouldn't use it in this case.
if someone has a better solution, it is welcome. Altough, for now, this one is working. As long as I use each version only once, I can get my plugin to work as intended.

Original thread: https://forums.rpgmakerweb.com/threads/js-noob-and-the-instant-plugin-failure.54637/