MZ How to individually set column positions on their own X/Y coordinates

Hi JS Wizards,

I've got a question for you. I'm working in the Equipment Slot window in the Equipment Scene and I'm having trouble individually positioning my 3 slots. I have maxCols set to 3 but have no idea how to code it so I can set each individual slot on an X/Y coordinate. Right now, all 3 slots are on the same X plane and put in even columns and I'm trying to be able to individually set them at their own location.

Any ideas on how I would code that?

Thanks,
Zer0


JavaScript:
// ----------------------------------------------------------
// These functions draw the actor data/value and refreshes it
function Window_EquipSlot() {
    this.initialize(...arguments);
}

Window_EquipSlot.prototype = Object.create(Window_StatusBase.prototype);
Window_EquipSlot.prototype.constructor = Window_EquipSlot;

Window_EquipSlot.prototype.initialize = function(rect) {
    Window_StatusBase.prototype.initialize.call(this, rect);
    this._actor = null;
    this.refresh();
};

Window_EquipSlot.prototype.setActor = function(actor) {
    if (this._actor !== actor) {
        this._actor = actor;
        this.refresh();
    }
};

Window_EquipSlot.prototype.update = function() {
    Window_StatusBase.prototype.update.call(this);
    if (this._itemWindow) {
        this._itemWindow.setSlotId(this.index());
    }
};

Window_EquipSlot.prototype.maxItems = function() {
    return this._actor ? this._actor.equipSlots().length : 0;
};

Window_EquipSlot.prototype.maxCols = function() {
    return 3;
};

Window_EquipSlot.prototype.item = function() {
    return this.itemAt(this.index());
};

Window_EquipSlot.prototype.itemAt = function(index) {
    return this._actor ? this._actor.equips()[index] : null;
};

Window_EquipSlot.prototype.drawItemName = function(item, x, y, width) {
    if (item) {
        const iconY = y + (this.lineHeight() - ImageManager.iconHeight) / 2;
        //const textMargin = ImageManager.iconWidth + 4;
        //const itemWidth = Math.max(0, width - textMargin);
        this.drawIcon(item.iconIndex, x, iconY);
    }
};

Window_EquipSlot.prototype.drawItem = function(index) {
    if (this._actor) {
        const slotName = this.actorSlotName(this._actor, index);
        const item = this.itemAt(index);
        const slotNameWidth = this.slotNameWidth();
        const rect = this.itemLineRect(index);
        const itemWidth = rect.width - slotNameWidth;
        this.drawText(slotName, rect.x, rect.y, slotNameWidth, rect.height);
        this.drawItemName(item, rect.x + 20, rect.y, 50);
    }
};

Window_EquipSlot.prototype.slotNameWidth = function() {
    return 138;
};

Window_EquipSlot.prototype.isEnabled = function(index) {
    return this._actor ? this._actor.isEquipChangeOk(index) : false;
};

Window_EquipSlot.prototype.isCurrentItemEnabled = function() {
    return this.isEnabled(this.index());
};

Window_EquipSlot.prototype.setStatusWindow = function(statusWindow) {
    this._statusWindow = statusWindow;
    this.callUpdateHelp();
};

Window_EquipSlot.prototype.setItemWindow = function(itemWindow) {
    this._itemWindow = itemWindow;
};

Window_EquipSlot.prototype.updateHelp = function() {
    Window_StatusBase.prototype.updateHelp.call(this);
    this.setHelpWindowItem(this.item());
    if (this._statusWindow) {
        this._statusWindow.setTempActor(null);
    }
};
//=======================================================================
I have no idea how you intend for your menu to look just based on the description you gave.

However I can say a bit about how Window_EquipSlots works. Like every other type of Window that inherits from Window_Selectable (which is basically every window with the moveable flashing cursor), the number of selectable options generated by the window is based on its "maxItems()" function.

As you can see in the code for EquipSlots, it checks an actor's equipment slots to decide this number (and if it hasn't been passed an actor, will return 0 items). In your case, you say you have given them 3 slots, so it will generate 3 items.

The position for these items is dependent on the "itemRect()" function. The window's "drawItem()" function references "itemLineRect()", which itself references "itemRect()". Where is the code for itemLineRect() and itemRect()?

Those functions are in the superclass Window_Selectable.

JavaScript:
Window_Selectable.prototype.itemRect = function(index) {
    const maxCols = this.maxCols();
    const itemWidth = this.itemWidth();
    const itemHeight = this.itemHeight();
    const colSpacing = this.colSpacing();
    const rowSpacing = this.rowSpacing();
    const col = index % maxCols;
    const row = Math.floor(index / maxCols);
    const x = col * itemWidth + colSpacing / 2 - this.scrollBaseX();
    const y = row * itemHeight + rowSpacing / 2 - this.scrollBaseY();
    const width = itemWidth - colSpacing;
    const height = itemHeight - rowSpacing;
    return new Rectangle(x, y, width, height);
};

If you take the time to read it, you will see that the position of an item to be drawn is based mostly on constant values like "maxCols" and "itemWidth()" that never change. The only values that really change are "scrollBaseX/scrollBaseY" and "index".

The "scrollBase" values will change if there are more items on a list than fit in the window and the player needed to scroll down or sideways to see more. The "index" value is really just the number when iterating through "maxItems()". Since you have 3 equip slots, the first slot will be index 0, the second slot will be index 1 and the third slot index 2. You can see the code by default tells items to be positioned left-to-right, then top-to-bottom.

I do not know exactly how you want it to look like, but if you want items to be displayed in a custom fashion instead, this is likely the function you will need to alter.
@Traverse ,

Thanks for the explanation, I really appreciate it.

I was able to "kind of" accomplish what I was trying to do, but now my slots IDs are out of order. I have a total of 12 slots now, 1 being my weapon, 2 my shield slot, and the rest are armor slots.

Using this piece of code I was able to rearrange all of the slots and it seems to work perfectly, however, I can't seem to setup initial equipment anymore or use the "Change Equipment" event command, the items just simply don't appear.

The "Change Armor" and "Change Weapon" event commands work fine, they just aren't equipped initially. Any ideas?

JavaScript:
Game_Actor.prototype.equipSlots = function() {
    const res = [11, 3, 2, 9, 4, 10, 1, 6, 5, 7, 8, 12];
    if (this.isDualWield()) res[2] = 1;
    return res;
};
Any equipment that is deemed "unequippable" on the actor is blocked from being equipped when initializing equipment or using the Change Equipment event command. See the function "Game_Actor.prototype.releaseUnequippableItems" to see how it works. This function checks the actual array of equipment against the array of slot types to see if the equipment type matches the slot type and disallows it if there's a mismatch. And it gets called during initializing the equips and during Change Equipment.

During initialization, your actual equips array is not going to be in the same order of your equipSlots array.

By default, when it is trying to equip items during initialization, it fetches the actual equips list as it shows up in the database. Meaning the first item on an actor's actual equips list, as per the database, is the weapon. And then shield. And then head. And so on.

Your first slot type is no longer a weapon. So upon initializing, when it tries to stuff the first item from the actual equips list, the weapon, into the first slot, it detects the mismatch and blocks it.

Meanwhile, the Change Equipment command always assumes the slot index you are trying to put something in is going to be the same as the item's equipment type ID (as it appears in the database) minus 1. Since you've rearranged the slots, this will obviously no longer be the case.

See the code yourself:
JavaScript:
Game_Actor.prototype.changeEquipById = function(etypeId, itemId) {
    const slotId = etypeId - 1; // <-- Assumes slot index to target is equipment type ID minus 1
    if (this.equipSlots()[slotId] === 1) {
        this.changeEquip(slotId, $dataWeapons[itemId]);
    } else {
        this.changeEquip(slotId, $dataArmors[itemId]);
    }
};
Fantastic reply and we'll written, I understood every word. And thanks for posting the function also.

Besides the initialization issue (which isn't a big deal for our project), can you think of any other issues this change could affect?

Do you recommend an alternative to the code I plugged in?
I mean, any plugins you might install later that also expect the default behavior of equipment type ID being the same as slot index like Change Equipment does will likewise run into issues. There's not much you can do about that other than manually try to fix the compatibility problems.

The alternative approach to what you did would be to alter Window_EquipSlot to visually display the slots in the order you want, without changing the actual slot order within Game_Actor. This would probably be more work, as you would have to not only alter Window_EquipSlot's drawItem() or drawAllItems() to display them in a different order from the equipSlots array, but also the backend logic triggered when selecting the slot, as stuff like Scene_Equip's executeEquipChange() function and Window_EquipItems would still use the order contained in the actor's equipSlots array and show a list of weapons for the first slot and swap weapons when selected, despite what you force Window_EquipSlot to label as the first slot on screen.

It could potentially prevent conflicts with any plugins that expect unmodified ordering from an actor's equipSlots, but would be more work (or at least, a different kind of work from the approach you are currently taking).
Wow!! I really appreciate the in-depth answers you keep giving me. That makes complete sense. I think I'm going to go with what I have because it works and works well. Not being able to initialize the slots on load isn't a big deal, scripts and events can take care of most things after the game starts.

Thanks again for your help!! 🙂

Original thread: https://forums.rpgmakerweb.com/threads/how-to-individually-set-column-positions-on-their-own-x-y-coordinates.157214/