New account registration is temporarily disabled.

[SCRIPTING] [RMMV] CANNOT READ PROPERTY "WIDTH" OF NULL

Posts

Pages: 1
So this is an error I get sometimes in my game - "UncaughtType Error: Cannot read property 'width' of null".

After a lot of thought and some research, here is what I think happens:

Sometimes I use a script command to show a picture, and immediately after that I use another command to manipulate that picture (usually move picture, often in a script call too). The loading of the picture is not instantaneous, and if the move picture is called before the picture is loaded, this error happens.

My biggest trouble dealing with this error is it's not consistent. It happens rarely, and sometimes it happens once in a given moment but if you play it again it runs fine.

I have a friend playtesting my game and he's getting this error too often. I asked if his computer is slow, he said yes. So I think it takes even longer to load pictures, and that's why it's more prone to error.

IF THAT WHAT'S REALLY HAPPENING (and this is the reason I'm making this topic), a possible solution could be:
Alway preloading every picture I show (I can just use a show picture command with 0 opacity) in every part of the game.
It should work
BUT
Considering how far I am in development, backtracking every show pic and doing that will be A LOT of work.

So I just want to know if my reasoning makes sense, so I won't waste time.
Yup, your reasoning makes sense, I've had maybe not the same issue but very similar issues, with things that are even less consuming. Like, even drawing an enemy on screen sometimes is kind of iffy, because I must ask for the graphic, and then the next frame maybe maybe might have the bitmap ready, not immediately, unlike previous engines.

I had to start to mess up with RMMV/Z's resource requests, so... I'll have to guess the plugin you're using might not have taken that into consideration.

Preloading would be a safe bet, but yeah, it'd take a while to go through all of your image cutscenes, especially considering I know how many your game has. :'D

--

The optimal solution would be to take a look at the code, and see how to implement asyncronicity support. So when it tries to do the thing, it checks first if the bitmap is loaded, and if not, put it on a queue of sorts and execute some special code when the loading finishes. IIRC the bitmap loader has a way to run code whenever a bitmap finishes loading, which is common practice in asynchronous operations.
Marrend
Guardian of the Description Thread
21806
The error message, by itself, indicates to me that the object the variable wants to be (An instance of a Bitmap object?) doesn't actually exist. What is baffling me about this is this inconsistency in getting the error. If the object really didn't exist, the error should persist each time.

I'm wondering how it is that you are showing/moving pictures with scripts in the first place? Personally speaking, I'd probably do it through an instance of Window_Base. Maybe getting rid of the window borders, depending on the picture.
My show picture scripts are like

$gameScreen.showPicture(7, "cutscenes/S15B1P1", 0, 0, 0, 100, 100, 255, 0);

And my move pictures are like:

$gameScreen.movePicture(54, 0, 206, 25, 100, 100, 255, 0, 1);

It's hard to pinpoint exactly where the glitch happens because, like I said, very inconsistent.

----

But I often use the standard move picture command instead of script.
Marrend
Guardian of the Description Thread
21806
I'm taking a look at the MZ equivalents of the "Show Picture" and "Move Picture" event-commands, and they both call functions like those you're using from $gameScreen. What I'm not 100% on is the "name" variable. I'm not necessarily worried about the lack of file extension, I'd be kinda more worried that it might randomly be looking in <game_directory>/cutscenes, rather than the (probably) intended directory of <game_directory>/Graphics/Pictures/cutscenes.

That aside, I don't know if something like this exists in MV/MZ, but, in Ace, there was a Cache class that contained shortcuts to data directories, such as Graphics/Faces, Graphics/Battlers, Graphics/Pictures, and the like. It seemed to me that getting images through that class speed things up a bit than calling them through other means.
So checking the console during the error, I found out it was related to a specific plugin I'm using, which is Kido's Picture Color Change plugin which I'm linking right here.

Apparently the plugin overwrites the basic picture load function, and because of some cache stuff it might generate the bug. The approximate block in which the error occurs is as follows:

        var bitmap = Object.create(Bitmap.prototype);

bitmap.initialize(this.defaultBitmap.width, this.defaultBitmap.height);
bitmap.smooth = true;
bitmap.blt(this.defaultBitmap, 0, 0, this.defaultBitmap.width, this.defaultBitmap.height, 0, 0);
this.bitmap = bitmap;


The plugin has no update, and I can't seem to find another plugin that does the same thing. Can someone help me maybe edit this plugin? Or suggest me a solution? Because worst case scenario I'll just abandon the plugin and manually change the HUE of some of the pics (which means I'll have to duplicate them, but it's a lesser evil).
Marrend
Guardian of the Description Thread
21806
Since the error concerns itself with "width" being undefined, let's do some backtracking of our own.

bitmap.initialize(this.defaultBitmap.width, this.defaultBitmap.height);

This is the code I'm suspecting is causing the error. If "this.defaultBitmap" is returning a null object, then we must find the cause of this. So, where is "this.defaultBitmap" defined?

if(isPictureChanged){
//Save raw bitmap
  this.defaultBitmap = this.bitmap;
}
var bitmap = Object.create(Bitmap.prototype);
// rest of code

Here, just above the code-block outlined. This is the only place in the code where this object gets set a value that I can see. What "isPictureChanged" does, in theory, is...

var picture = this.picture();
var isPictureChanged = picture && picture.name() !== this._pictureName;

...check if the object "this.picture()" exists, and also if the property of "this.picture().name()" is not the same object/value as "this._pictureName". I cannot find any hits in-script for what value "this.picture()" could be, so, I'd be inclined to put in a breakpoint after the line of "var picture = this.picture();", then, see what values that has.
Pages: 1