Skip to content

Instantly share code, notes, and snippets.

@BenjaminUrquhart
Last active December 2, 2024 06:02
Show Gist options
  • Save BenjaminUrquhart/bf22468ce489bcf313055a749648fc30 to your computer and use it in GitHub Desktop.
Save BenjaminUrquhart/bf22468ce489bcf313055a749648fc30 to your computer and use it in GitHub Desktop.
Plugin to fix the dreaded "Cannot read property 'width' of null." crash for "In Stars And Time"
/*:
@plugindesc Fix 'cannot read property "width" of null' crash in Olivia_AnimatedPictures.
@author Benjamin Urquhart
*/
{
// Add functions that causes the crash here.
let functions = ["resetFrame", "updateAnimatedPictureFrame"]
for(let name of functions) {
console.log("Patching " + name);
(function(name) {
let orig = Sprite_Picture.prototype[name];
Sprite_Picture.prototype[name] = function(bitmap) {
// The plugin relies on Sprite_Picture.loadBitmap returning fast enough to
// set the bitmap field on the sprite object. This is usually the case,
// but not guaranteed. If the function takes a bit longer to return
// than normal, the field is not set and we crash.
// However, Bitmap.prototype._callLoadListeners provides the bitmap
// as an argument, so we can just use that here instead.
// tl;dr race condition.
if(!this.bitmap) {
if(bitmap) {
console.warn("Load listener " + name + " was called before return of Sprite_Picture.loadBitmap");
this.bitmap = bitmap;
}
else {
// Bail
console.warn("Attempted call to " + name + " with no bitmap");
return;
}
}
return orig.call(this, ...arguments);
}
})(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment