MV How to simplify this code?

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.
That function is used in the code and looks good to me, why do you want to remove it? 😅

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. 😴
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));
};

Original thread: https://forums.rpgmakerweb.com/threads/how-to-simplify-this-code.129485/