Issues aliasing the same DataManager function in two plugins

Status
Not open for further replies.

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
What is the correct method to use if I want to alias DataManager.onLoad in multiple plugins?


If I use this for both of them:


var _DataManager_onLoad = DataManager.onLoad;
DataManager.onLoad = function(object) {
_DataManager_onLoad(object);
// do my stuff here
};


the method in the second plugin calls the method in the first plugin, which calls the original method.  All three pass $dataActors as the object.  But I get an undefined is not a function error on rpg_managers 141: this.extractMetadata(data);


If I give them different alias names:


var _DataManager_onLoad1 = DataManager.onLoad;
DataManager.onLoad = function(object) {
_DataManager_onLoad1(object);
// do my stuff here
};


and


var _DataManager_onLoad2 = DataManager.onLoad;
DataManager.onLoad = function(object) {
_DataManager_onLoad2(object);
// do my stuff here
};


I get the same results.


On the this.extractMetadata(data) line it's telling me this is Window, but it's not listing extractMetadata as an available function.  When I disable both plugins and put a breakpoint on that line, it's telling me this is DataManager.  So I can see the context/scope is being shifted somewhere.


If I pass in this as a first argument:


var _DataManager_onLoad1 = DataManager.onLoad;
DataManager.onLoad = function(object) {
_DataManager_onLoad1(this, object);
// do my stuff here
};


and


var _DataManager_onLoad2 = DataManager.onLoad;
DataManager.onLoad = function(object) {
_DataManager_onLoad2(this, object);
// do my stuff here
};


the second plugin says this is DataManager and object is $dataActors, but when it gets through to the first plugin, it says this is Window and object is DataManager.  It then passes DataManager through to the original function instead of $dataActors, so nothing gets processed.


It doesn't seem to matter whether I use the same variable name for the aliases or not.


The second function is calling the first function, and the first is calling the original, but I can't seem to get the right results, whether I include this as an argument or leave it out.  What do I need to do so it passes through the correct arguments and knows that this is DataManager in each case?
 
Last edited by a moderator:

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
You're not calling your aliases correctly. When you call an alias like that, you'd want to actually do so using the "call" function, because it can let you pick the scope that "this" takes. If you don't do so, I believe it copies the current scope form when its created, not 100% sure on that, but that's what would make sense here (as the alias is created in a global (and thus, window) scope). But anyway, the solution you're looking for is:

Code:
  var _DataManager_onLoad = DataManager.onLoad;
  DataManager.onLoad = function(object) {
    _DataManager_onLoad.call(this, object);
    // do my stuff here
  };
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
Brilliant!  Such a simple mistake.  Been a while since I've written one.


Thanks heaps :)
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
No problem, JavaScript can take a little getting used to, but once you understand how it works, you can make some pretty fancy stuff.
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,299
Reaction score
11,713
First Language
English
Primarily Uses
RMVXA
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Don't forget, aspiring writers: Personality isn't what your characters do, it is WHY they do it.
Hello! I would like to know if there are any pluggings or any way to customize how battles look?
I was thinking that when you start the battle for it to appear the eyes of your characters and opponents sorta like Ace Attorney.
Sadly I don't know how that would be possible so I would be needing help! If you can help me in any way I would really apreciate it!
The biggest debate we need to complete on which is better, Waffles or Pancakes?
rux
How is it going? :D
Day 9 of giveaways! 8 prizes today :D

Forum statistics

Threads
106,049
Messages
1,018,546
Members
137,835
Latest member
yetisteven
Top