Arctica

Veteran
Veteran
Joined
Jul 19, 2021
Messages
155
Reaction score
250
First Language
English
Primarily Uses
RMMZ
Alright so it's been 2 months since I last even looked at JS and I forgot most of the advance stuff, the basics slowly coming back. So there's an idea as to where I am with it.

In anycase, tracing my steps with my old scripts, I noticed I never overridden a method that calls another method. It's always been just one method. I suppose 'past me' was doing this on purpose for this very reason, but 'past me' didn't think about future me needing to know this later. (lol)

Taking out the function body to keep it clean/short, how do I override this? apply the alias to the call as well or does only the first method matter?:

Code:
Game_Actor.prototype.performAction = function(action) {
    Game_Battler.prototype.performAction.call(this, action);
....
}

The way I had done it for single methods were the syntax of:
Code:
(function(alias) {
    function header.. {
        alias.apply(....);
    };
})(function header...);
 

ShadowDragon

Realist
Veteran
Joined
Oct 8, 2018
Messages
5,424
Reaction score
2,135
First Language
Dutch
Primarily Uses
RMMV
I think both if those can work, depening on the structure.

ES5 mostly use the top one (which I use as well), ES6 and other
people that are good uses the bottom one, but I dont quite get
that part.

the first one personally is more userfriendly, but it depends how
you would alias a function, both works, but the one you choose
is the one you feel most comfortable with.
 

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
2,723
Reaction score
2,083
First Language
Portuguese - Br
Primarily Uses
RMMZ
Hi there!

To alias it properly you can do this:
JavaScript:
const Game_Actor_performAction = Game_Actor.prototype.performAction
Game_Actor.prototype.performAction = function(action) {
    Game_Actor_performAction.call(this, action)
    // Do your stuff
}

To override, do this:
JavaScript:
Game_Actor.prototype.performAction = function(action) {
    // Do your stuff
}
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
3,989
Reaction score
4,916
First Language
EN, FR
Primarily Uses
RMMZ
I personally do it like how Eliaquim does it, and your first code block. If it's a plugin only for my project, I sometimes just override existing methods.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
4,969
Reaction score
4,552
First Language
English
Primarily Uses
RMMZ
I do it like Eli does as well, only difference is that I always have a TLB object in my plugins that stores the aliases I write so that other people can see my code if they need to but I still avoid polluting the namespace.
 

Arctica

Veteran
Veteran
Joined
Jul 19, 2021
Messages
155
Reaction score
250
First Language
English
Primarily Uses
RMMZ
Ok thank you, so it's the main method that matters(I even forgot the terminology lol, oh well, at least reading JS docs is very engaging).
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
4,969
Reaction score
4,552
First Language
English
Primarily Uses
RMMZ
Ok thank you, so it's the main method that matters(I even forgot the terminology lol, oh well, at least reading JS docs is very engaging).
It can help to reframe it. What you're doing isn't *aliasing*, per se; you're not giving the function a different name. What you're doing is storing the original function inside a new variable that you can use to refer to it later, then replacing the original with your own. You can do this because the body of a function is an object in Javascript like anything else, so you can assign it to a variable or provide it as an argument to another function.
 

Latest Threads

Latest Posts

Latest Profile Posts

Sometimes it works out to not do work on my game for a while. I ran through a playtest today and got about a page of things to fix or improve...
I got lot's o balancing to do lol... I think I'll redo my skills for the 3 party members for the 5th time lol. but I need them to be more distinct for balancing the RPS element system since fights are 1 on 1 with tagging.
Our latest Make a Game in MZ Stream is here! Today, we're going to create a complex switch puzzle, where flipping one switch also toggles the switches adjacent to it. Join now to see how it's done!

Here's a preview to an upcoming free track. Kind of a hybrid, Middle Eastern, orchestral thingy. Should be released pretty soon!

Time: duration regarded as belonging to the present life as distinct from the life to come or from eternity; finite duration.


1653085842103.png

Me against Time now. No second chances. No distractions. All or nothing. Whoever wins is the best fighter.

Forum statistics

Threads
121,980
Messages
1,145,646
Members
160,255
Latest member
uncmerigold
Top