Check plugin command after

Hello,

I want to check for plugin command, but after something. I explain :

I got this 

Spriteset_Map.prototype.createLowerLayer = function() { Spriteset_Base.prototype.createLowerLayer.call(this); map = $dataMap; this.createTilemap();};Which create a tilemap. So next I can do that :

Spriteset_Map.prototype.createLayers = function() { this._tilemap.addChild(child);};Who is working like a charm. But when I do this :

var aliasPluginCommand = Game_Interpreter.prototype.pluginCommand;Game_Interpreter.prototype.pluginCommand = function(command, args) { aliasPluginCommand.call(this, command, args); console.log(this._tilemap);}It's undefined, it seems logical cause it's not the same class, but so how to access to _tilemap ?

Thanks for help !
"this" refer to the function it is inside.

So in:

Spriteset_Map.prototype.createLayers = function() { this._tilemap.addChild(child);};this = Spriteset_Map.prototype

now here:

Game_Interpreter.prototype.pluginCommand = function(command, args) {    aliasPluginCommand.call(this, command, args);    console.log(this._tilemap);}this = Game_Interpreter.prototype

You won't be able to access "Spriteset_Map.prototype._tilemap" using "this" inside of "Game_Interpreter.prototype".

Anyway, you can just make a new function on Spriteset_Map that return the tilemap value, and call it.

Code:
Spriteset_Map.prototype.tilemap = function() {    return this._tilemap}; var aliasPluginCommand = Game_Interpreter.prototype.pluginCommand;Game_Interpreter.prototype.pluginCommand = function(command, args) {    aliasPluginCommand.call(this, command, args);    console.log(Spriteset_Map.prototype.tilemap.call(Spriteset_Map.prototype));}
Calling that function on the prototype will get you nothing, since you want the specific instance. What you could do, is grabbing the current scene from the SceneManager, make sure, that it's a Scene_Map and get the tilemap from there. What you should probably do, is extending Scene_Map.prototype.createSpriteset (or Spriteset_Map.prototype.createLowerLayer) and store a reference to the tilemap on your plugin. Also extend Scene_Map.prototype.terminate to make sure you cleanup that reference properly.

/edit: Also, "this" refers to the object the function is called on, not the function itself. Assuming you would be doing



var test = Game_Interpreter.prototype.pluginCommand;test();"this" inside that function would refer to the current surrounding object (or the window object).
Oh yeah thank you both, now it's working, I use the extending Scene_map method, so now my last problem is to cleanup the reference. I found the function :

Scene_Map.prototype.terminate = function() { Scene_Base.prototype.terminate.call(this); if (!SceneManager.isNextScene(Scene_Battle)) { this._spriteset.update(); this._mapNameWindow.hide(); SceneManager.snapForBackground(); } $gameScreen.clearZoom();};But I don't know how should I do to clean my reference ? And, from this page : http://stackoverflow.com/questions/742623/deleting-objects-in-javascript they said that javascript is using a garbage collector so should I really need to delete it ? Thanks.
A small example would look like this:

(function() {    "use strict";    var ref;    var alias_createSpriteset = Scene_Map.prototype.createSpriteset;    Scene_Map.prototype.createSpriteset = function() {        alias_createSpriteset.call(this);        ref = this._spriteset._tilemap;    };    var alias_terminate = Scene_Map.prototype.terminate;    Scene_Map.prototype.terminate = function() {        alias_terminate.call(this);        ref = undefined;    };})();You clean up the reference, because the garbage collector only delete objects, that aren't referenced anymore.
Oh I see, thanks for this !

EDIT:

Well, there is a problem, when I go to the menu tilemap is deleted and so all my pictures are lost !

Any idea for fixing that ?

Original thread: https://forums.rpgmakerweb.com/threads/check-plugin-command-after.54562/