Function Alias with local variables

Hello,

I'm quite new to programming when it comes to JS-Plugins in RPG Maker MV, and I tried to write my own plugin.

In "rpg_windows.js" there is this function:
Code:
Window_Base.prototype.drawItemName = function(item, x, y, width) {
    width = width || 312;
    if (item) {
        var iconBoxWidth = Window_Base._iconWidth + 4;
        this.resetTextColor();
        this.drawIcon(item.iconIndex, x + 2, y + 2);
        this.drawText(item.name, x + iconBoxWidth, y, width - iconBoxWidth);
    }
};

I made an alias-function of it and it works, it looks like this now:
Code:
    var alias_Window_Base_drawItemName =  Window_Base.prototype.drawItemName;

    Window_Base.prototype.drawItemName = function(item, x, y, width) {
        
        alias_Window_Base_drawItemName.call(this, item, x, y, width);
        
    }

Now I'd like to edit the "iconBoxWidth"-Variable from the original function, so it would perform a bit different in the alias-function. (I want to increase it by a specific number).

Is this even possible with alias-functions? If yes - how?

Greetings,
Kirimoar
No, you cannot access a private scope. However, in this case, adding to iconBoxWidth will have the same effect as adding/subtracting some value to/from the method's x and width arguments...and they are accessible! So you could try something like this:
Code:
    var alias_Window_Base_drawItemName =  Window_Base.prototype.drawItemName;
    Window_Base.prototype.drawItemName = function(item, x, y, width) {
        var offset = 20;
        alias_Window_Base_drawItemName.call(this, item, x + offset, y, width - offset);
    };
Hi,

thank you for your answer!

The problem is that I only want to add the value to x in the function "drawText", not in the function "drawIcon" that is called before that. So when I add an offset to x, both are increased - which is a problem for displaying.

Do you maybe have another idea?

What I want to achieve is just a higher horizontal (so x) padding between the icon and the item-name.

Greetings,
Kirimoar
Oh, I see! In that case I think you'll need to make an override instead, e.g.
Code:
Window_Base.prototype.drawItemName = function(item, x, y, width) {
    width = width || 312;
    if (item) {
        var iconBoxWidth = Window_Base._iconWidth + 4 + 20;  // <-- edit 1 of 1
        this.resetTextColor();
        this.drawIcon(item.iconIndex, x + 2, y + 2);
        this.drawText(item.name, x + iconBoxWidth, y, width - iconBoxWidth);
    }
};
Bleh, nevermind. What I typed up will not work without mucking things up.
Thank you very much for your solution, @caethyril

It works perfectly and like expected!
I don't know why I didn't think of just overriding it by myself, as it seems to be quite a good solution for this case.

The thread can be closed as it is solved 🙂

Greetings,
Kirimoar

Original thread: https://forums.rpgmakerweb.com/threads/function-alias-with-local-variables.115716/