[SKIP TO BELOW PROBLEM 1 BENEATH MY BIG BLOCKS OF CODE, TO KNOW EXACTLY WHAT I’M STILL STRUGGLING WITH]
No worries, thanks again Brian. Apologies for the trouble, and thank you for being patient with me
OK so my guess is that Inventory.RemoveFromSlot() is the function that takes care of replacing stuff in the same slot, which I’m assuming will solve half my 2h problems. My question is, how do we get the index for the item slot to feed that function?
I also re-wrote my Unequip functions, so now they look like this:
/// <summary>
/// Unequips the 2-Handed weapon and equips the shield, if we want to equip a shield
/// </summary>
/// <param name="inventory"></param>
/// <param name="itemToEquip"></param>
public void Unequip2HWeaponAndEquipShield(Inventory inventory, EquipableItem itemToEquip) {
EquipLocation weaponSlot = EquipLocation.Weapon;
EquipLocation shieldSlot = EquipLocation.Shield;
Equipment playerEquipment = this;
EquipableItem equippedWeapon = playerEquipment.GetItemInSlot(weaponSlot);
EquipableItem equippedShield = playerEquipment.GetItemInSlot(shieldSlot);
if (inventory.HasSpaceFor(GetWeaponAndShieldAsIEnumerable()))
{
if (equippedWeapon != null)
{
inventory.AddToFirstEmptySlot(equippedWeapon, 1);
RemoveItem(weaponSlot);
}
/* if (equippedShield != null)
{
inventory.AddToFirstEmptySlot(equippedShield, 1);
RemoveItem(shieldSlot);
} */
AddItem(shieldSlot, itemToEquip);
inventory.RemoveItem(itemToEquip, 1);
}
}
/// <summary>
/// Unequips the shield and equips the 2-Handed weapon, if we want to equip a 2-Handed Weapon
/// </summary>
/// <param name="inventory"></param>
/// <param name="itemToEquip"></param>
public void UnequipShieldAndEquip2HWeapon(Inventory inventory, EquipableItem itemToEquip) {
EquipLocation weaponSlot = EquipLocation.Weapon;
EquipLocation shieldSlot = EquipLocation.Shield;
Equipment playerEquipment = this;
EquipableItem equippedWeapon = playerEquipment.GetItemInSlot(weaponSlot);
EquipableItem equippedShield = playerEquipment.GetItemInSlot(shieldSlot);
if (inventory.HasSpaceFor(GetWeaponAndShieldAsIEnumerable())) {
if (equippedWeapon != null) {
inventory.AddToFirstEmptySlot(equippedWeapon, 1);
RemoveItem(weaponSlot);
}
if (equippedShield != null) {
inventory.AddToFirstEmptySlot(equippedShield, 1);
RemoveItem(shieldSlot);
}
AddItem(weaponSlot, itemToEquip);
inventory.RemoveItem(itemToEquip, 1);
}
}
And after a little bit of research, and finding a ‘FindSlot()’ function in ‘Inventory.cs’ that I turned from ‘private’ to ‘internal’, I slightly modified my ‘Equipment.MaxAcceptable()’ function to look like this (Changes are labelled with a comment of ‘// New line…’:
public int MaxAcceptable(EquipLocation equipLocation, InventoryItem item)
{
Inventory inventory = GetComponent<Inventory>();
if (item is EquipableItem equipableItem)
{
if (!equipableItem.CanEquip(equipLocation, this)) return 0;
switch (equipLocation)
{
case EquipLocation.Shield:
if (GetItemInSlot(EquipLocation.Weapon) is WeaponConfig weapon)
{
if (weapon == null) {
Debug.Log("No weapon equipped, wearing the shield");
return 1;
}
if (!weapon.isRightHanded) {
Debug.Log($"{weapon} is left handed, not going to wear the shield");
inventory.RemoveFromSlot(inventory.FindSlot(weapon), 1); // New line here, finding inventory slot to attempt to replace
Unequip2HWeaponAndEquipShield(inventory, equipableItem);
return 0;
}
if (weapon.isTwoHanded) {
Debug.Log($"{weapon} is two-handed, not going to wear the shield");
inventory.RemoveFromSlot(inventory.FindSlot(weapon), 1); // New line here, finding inventory slot to attempt to replace
Unequip2HWeaponAndEquipShield(inventory, equipableItem);
return 0;
}
Debug.Log($"{weapon} is right-handed, wearing the shield");
return 1;
}
return 1;
case EquipLocation.Weapon:
if (item is WeaponConfig weaponConfig)
{
if (!weaponConfig.isRightHanded) {
inventory.RemoveFromSlot(inventory.FindSlot(weaponConfig), 1); // New line here, finding inventory slot to attempt to replace
UnequipShieldAndEquip2HWeapon(inventory, weaponConfig);
return 0;
}
if (weaponConfig.isTwoHanded) {
inventory.RemoveFromSlot(inventory.FindSlot(weaponConfig), 1); // New line here, finding inventory slot to attempt to replace
UnequipShieldAndEquip2HWeapon(inventory, weaponConfig);
return 0;
}
return 1;
}
return 1;
default: return 1;
}
}
return 0;
}
As of right now, I reversed the slot hunting function… It only caused more issues
But all 3 issues:
- [THIS ONE WAS SOLVED BY RE-WRITING THE UNEQUIP FUNCTIONS (new one is the code block above the code block, right above this paragraph), AND BY TUNING InventorySlotUI.OnPointerClick(), AS SHOWN UNDER (2)] Data loss, which occurs when trying to wield a 2h weapon in a full inventory, replacing a weapon that’s already in the weapon slot (because I can’t replace stuff in the exact same slot, because I don’t know how to get the index for ‘Inventory.RemoveFromSlot()’ (the solution above failed))
[FROM HERE ONWARDS IS UNRESOLVED] Apparently wielding a normal sword over a 2h places the 2h in the inventory slot that belonged to the normal sword (WHICH IS EXACTLY HOW I WANT IT), but wielding a 2h to replace a normal slot finds the first inventory slot, and places it there, NOT UTILIZING THE FACT THAT THIS SLOT JUST BECAME FREE (although both have exactly the same code). After further investigation, after we implemented this whole 2h system, ONLY THE NORMAL SWORD equipping places other weapons in its slot (THE MECHANIC I WANT), the slot of the normal sword that’s about to be equipped. All other weapons, for some reason (I checked the inspector settings), search for an empty slot first, although this problem didn’t even exist before we introduced this system. As a result, if my inventory is full, or 1 slot away from full, I must spam click my weapons (EXCEPT THE NORMAL SWORD) to be able to wield them, or empty an extra slot to utilize for the number of items about to be unequipped
So, for example, if you’re unequipping a sword and a shield for example, you need 2 empty slots, instead of it being 1 empty slot, and the other slot being the replacement slot of what is yet-to-be-an-empty-slot (by the weapon that’s going to the equipment now (which is what works for the quiver arrows and the NORMAL SWORD, but somehow not for the rest of the weapons, and I am clueless of why)), so something is entirely wrong here…
- [SOLVED] Spam click issue miraculously making something that’s been coded to ensure that it’s supposed not to work, suddenly work (as much as I’d love to ignore this one, it becomes a serious issue when the players’ inventory becomes full). After a few code updates, this issue becomes extremely obvious when the players inventory is full, and pretty much null when even a single empty slot is available
Edit: I also solved the Spam Click issue, by going into ‘InventorySlotUI.OnPointerClick()’, and tuning the ‘Invoke(nameof(TimesUp), .5f);’ function, as follows:
public void OnPointerClick(PointerEventData eventData) {
if (LastUIClicked != this) {
LastUIClicked = this;
Invoke(nameof(TimesUp), .01f);
Debug.Log($"{index} was clicked once");
}
else {
HandleDoubleClick();
}
}
By making the timer significantly lower, the chances of successful spam issues showing up is zero, virtually eliminating the problem
and
- The shield being replaced by a ‘Base Shield’ (something we used to hide visibility of the shield in our game, when 2h weapons override the shield, but is now an InventoryItem that shows up when our game restores its save state, and it needs to be eliminated) when I restore my game save file (I think we had a similar issue in the quiver system once). This one is a serious problem
are still malfunctioning unfortunately