RyanBram

Veteran
Veteran
Joined
May 13, 2012
Messages
246
Reaction score
305
First Language
Indonesian
Primarily Uses
RMMV
Hi.
I have an old code from my friends which function is to simply change actor home formation.

The code as follow:

Code:
    function _p(o){ return o[pluginName] = o[pluginName] || {} };
    
    var _alias_Sprite_Actor_setActorHome = Sprite_Actor.prototype.setActorHome;
    Sprite_Actor.prototype.setActorHome = function(index) {
        _alias_Sprite_Actor_setActorHome.call(this, index);
        var homeX = _p($gameSystem).x ? _p($gameSystem).x : actorHomeX;
        var homeY = _p($gameSystem).y ? _p($gameSystem).y : actorHomeY;
        this.setHome(eval(homeX), eval(homeY));
    };

Is it possible to remove the
Code:
function _p(o){ return o[pluginName] = o[pluginName] || {} };

So I can just directly use
Code:
var _alias_Sprite_Actor_setActorHome = Sprite_Actor.prototype.setActorHome;
...
...

Thank you very much for any help.
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,759
Reaction score
2,108
First Language
EN
Primarily Uses
RMMZ
That function is used in the code and looks good to me, why do you want to remove it? :kaoswt:

You could remove it, but then you'd need to replace all four instances of _p($gameSystem) with $gameSystem[pluginName]. Also, this can't be the full code as-is, because pluginName isn't defined anywhere, so I imagine _p is used elsewhere in the code. In that case you'd need to check the rest of the code and replace other calls as well. :kaoslp:
 

OpheliaEnigma

Villager
Member
Joined
Nov 3, 2020
Messages
22
Reaction score
28
First Language
Portuguese
Primarily Uses
RMMV
I agree with caethyril, you can safely romeve your _p function as long as you replace all of its usages.

Also, there are multiple improvements we can make to your friend's code. Note how, in certain situations, this code _p($gameSystem) can be evaluated a maximum of four consequent times and a minimum of two. We are performing the same computation multiple times without any advantage. We should save its result to a variable and avoid this, like this:

JavaScript:
function _p(o){ return o[pluginName] = o[pluginName] || {} };

var _alias_Sprite_Actor_setActorHome = Sprite_Actor.prototype.setActorHome;
Sprite_Actor.prototype.setActorHome = function(index) {
    _alias_Sprite_Actor_setActorHome.call(this, index);
    const  pluginParameters = _p($gameSystem);
    var homeX = pluginParameters.x ? pluginParameters.x : actorHomeX;
    var homeY = pluginParameters.y ? pluginParameters.y : actorHomeY;
    this.setHome(eval(homeX), eval(homeY));
};

Now, note how we are getting pluginParameters.x and pluginParameters.y twice? Once to check if they are truthy and, if they are, once again to use their value. We can simplify it like this:

JavaScript:
function _p(o){ return o[pluginName] = o[pluginName] || {} };

var _alias_Sprite_Actor_setActorHome = Sprite_Actor.prototype.setActorHome;
Sprite_Actor.prototype.setActorHome = function(index) {
    _alias_Sprite_Actor_setActorHome.call(this, index);
    const  pluginParameters =  $gameSystem[pluginName];
    var homeX = pluginParameters.x || actorHomeX;
    var homeY = pluginParameters.y || actorHomeY;
    this.setHome(eval(homeX), eval(homeY));
};

I would also suggest always using const instead of var whenever you declare a variable that will never change. This way the code is cleaner, more obvious, and if you ever change the value of a const by mistake, the code will error out and warn you immediatly instead of causing silent bugs. applying these rules and removing the _p function, we get this:

JavaScript:
const _alias_Sprite_Actor_setActorHome = Sprite_Actor.prototype.setActorHome;
Sprite_Actor.prototype.setActorHome = function(index) {
    _alias_Sprite_Actor_setActorHome.call(this, index);
    const  pluginParameters = $gameSystem[pluginName];
    const  homeX = pluginParameters.x || actorHomeX;
    const  homeY = pluginParameters.y || actorHomeY;
    this.setHome(eval(homeX), eval(homeY));
};
 
Last edited:

Latest Threads

Latest Posts

Latest Profile Posts

 …I think I might need to switch from standard ATK/DEF up/down buffs to parameter altering states. I just had an actor whose usual attack against an enemy does ~25 do a buffed attack against a debuffed enemy for ~600…
just saw that titan quest is free on steam until the 23rd
Zombie status is now complete. I didn't have to do much in the way of changing how healing is applied to undead targets, was just a simple 3 line code. I thought I had to overhaul Game_Action lol. Everything else was doable by events. So.. Zombie: You turn undead. Your element changes to darkness. You're weak to light, you're strong to physical attacks and healing hurts you and revival items will kill you.
My Overworld might be a mix of inspiration from 2D Final Fantasy games, Breath of Fire, Battlechasers: Nightwar, Super Mario World, and Pillars of Eternity. If I can make it more in-depth than just walking from point A to point B, that might do a lot for the gameplay experience. Hmm...

Forum statistics

Threads
115,226
Messages
1,088,221
Members
149,816
Latest member
RiyuDayo
Top