ReferenceError: Object is not defined? (SOLVED)

Hello again!

From my two previous questions I have actually managed to do what I wanted to do. So I could just move on with my life. However, I wanted to produce a plugin out of it and I got stuck.

I decided to create an object with several variables and methods. In the plugin it looks like this:

Code:
var MyAwesomeObject = {
    array1: [],
    array2: [],
    var1: value1,
    var2: value2,
    boolean: false,
    method1: function() {
        // what it does goes here
        },
    method2: function() {
        // what it does goes here
        }
    };

but when I try to call one of its methods from the Event editor, as soon as the game launches I receive the error message:

Code:
MyAwesomeObject.method1();
"ReferenceError: MyAwesomeObject is not defined"

But I defined it in the plugin. It's supposed to be a global object, because I didn't define it inside any function. So what's the problem?
Let me guess. You wrapped the whole plugin inside the IIFE (also known as self-invoking function), didn't you?

For the record, it looks like this:
Code:
(function(){
//your code})()
Let me guess. You wrapped the whole plugin inside the IIFE (also known as self-invoking function), didn't you?
I didn't. And I've proofread it again and again to make sure I'm closing all the parenthesis, brackets and so on.
I'm afraid we won't be able to help you without seeing the actual script file then.
Yea, var defines a variable on the enclosing {block} or the window if it's outside all blocks; this looks like a scope issue. 😴

Also you might want to be careful with your property names, boolean is listed here as a reserved keyword from an older standard:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
Thank you for your replies.

After a long time paying close attention to every single line of code (since I never meant for you to carry that burden for me), I was able to detect the scope issue. I have corrected the scope for the Object, but now the problem is I'm unable to make this aliasing work.

The idea is to be able to load a picture from a subfolder. I'm currently trying this, but without success.

Code:
var albumPic = "";
(function(alias) {
    var replaceFolder = function(folder) {
        if (albumPic.length > 0) {
            switch (folder) {
                case 'img/pictures': return "img/pictures/" + albumPic;
                default: return folder;
                };
            };
        };
    ImageManager.requestBitmap = function(folder, filename, hue, smooth) {
        folder = replaceFolder(folder);
        return alias.call(this, folder, filename, hue, smooth);
        };
    }) (ImageManager.requestBitmap);

I did virtually the same thing to play Music from subfolders (by aliasing the AudioManager.createBuffer method), but the same strategy isn't working now.
Looks like albumPic is an empty string "", so albumPic.length is zero, meaning replaceFolder will return undefined. I assume that contributes to the error you're seeing? 😅
I thought that might have been the problem, but after several tries I still can't succeed. Currently, the game doesn't crash, but it still fails to load the image. I have isolated this portion on its own .js file:

Spoiler
Code:
// Loading audio file from subfolder //
var albumMe = "me";
(function(alias) {
  var replaceFolder = function(folder) {
    switch (folder) {
      case 'me': return albumMe;
      default: return folder;
    }
  };
  AudioManager.createBuffer = function(folder, name) {
    folder = replaceFolder(folder);
    return alias.call(this, folder, name);
  };
})(AudioManager.createBuffer);

// Loading image file from subfolder //
var albumPic = "img/pictures";
(function(alias) {
    var replaceFolder = function(folder) {
        switch (folder) {
            case 'img/pictures': return albumPic;
            default: return folder;
            }
        };
    ImageManager.requestBitmap = function(folder, filename, hue, smooth) {
        folder = replaceFolder(folder);
        return alias.call(this, folder, filename, hue, smooth);
        };
    }) (ImageManager.requestBitmap);

Then I write this script in an Event to load both files from the subfolders:

Spoiler
Code:
albumMe = "me/meSubfolder";
var song = {name: "file_in_meSubfolder", volume: 100, pitch: 100, pan: 100};
AudioManager.playMe(song);

albumPic = "img/pictures/picturesSubfolder";
$gameScreen.showPicture(1, "file_in_picturesSubfolder", 0, 0, 0, 100, 100, 255, 0);

The audio file plays fine from the meSubfolder, but I get a "Loading Error - Failed to load: img/pictures/file_in_picturesSubfolder.png", meaning it's looking for the image file in the pictures folder, ignoring the aliasing function completely.

As you can see, I'm doing (or at least that's what I think) the exact analogous thing in both cases, but the other fails where the one succeeds.

~ o ~ o ~ o ~ o ~ o ~
UPDATE

I GOT IT !!!

I was aliasing the wrong method. The correct script, if any one is interested in knowing, is the following:

Code:
var albumPic = "img/pictures/";
(function(alias) {
    ImageManager.loadPicture = function(filename, hue) {
        return this.loadBitmap(albumPic, filename, hue, true);
        };
    }) (ImageManager.loadPicture);
😅 Do you need to alias the loadBitmap method as well? I think requestBitmap only handles image reservation/caching. 😮‍💨
Do you need to alias the loadBitmap method as well?

Not as well. It seems loadBitmap is the only method you need to alias.

So, this is solved as far as I can tell, and unles I've done something terribly wrong that may come back to haunt me in the future.
1. The reason I was getting the ReferenceError message was not closing brackets properly.
2. In order to load pictures from subfolders you need to alias the ImageManager.loadBitmad method.
I believe resource caching is recommended in case of large images or a slow system: can help to prevent delays between saying "load this image" and seeing the image appear. I don't know much about the technical side of that stuff, though. 😅
Aliasing the loadBitmap method is dangerous, because literally every loadSomething method of ImageManager uses loadBitmap. It could be safer to define a custom method, which only fires ImageManager.loadBitmap. It makes for a more readable code too, the (function stuff drives me crazy.
@Poryg - in fairness, only loadPicture is being aliased here (didn't realise it was only for "picture" images else I'd've suggested that). Also, if you want to change the source directory during runtime like this, I'm not sure how this approach could be made less "dangerous"? 😅

I take your point with the IIFE stuff, though, it's definitely a style thing! I just find it easier to give people a handy "this shouldn't interfere with anything else" block of code rather than make sure they're setting up their namespaces etc properly... 😴
Also, if you want to change the source directory during runtime like this, I'm not sure how this approach could be made less "dangerous"? 😅
Quite simply.
Code:
ImageManager.loadPicFromSubfolder = function(folder, filename, remainingParams){
return this.loadBitmap("/img/pictures/ + folder + filename, remainingParams);
}
Or there's an even simpler option. loadPicture accepts param filename and fires loadBitmap, passing "/img/pictures/" as its folder parameter and filename as its name. LoadBitmap, all it does with this, is encodeURIComponent on the filename (so replaces for example empty space with %20), and then adds it to the folder. So you can simply do this:
Code:
var image = ImageManager.loadPicture("subfolder/filename", remainingParams);
and the resulting filepath will be "/img/pictures/subfolder/file.png"
No need to even alias anything. 😀

Original thread: https://forums.rpgmakerweb.com/threads/referenceerror-object-is-not-defined-solved.115184/