With this new code Default Weapon Won't Add Its Stats Damage!

With this new code system it won’t add additive damage from default weapon like un-armed weapon.

After debugging with break points and all stuff, I found out the reason. This is because of default weapon leave Equippable Slot UI empty and under ‘StatsEquipment.cs’ when it check for additive stat, it calls method ‘GetAllPopulatedSlots()’ which return nothing so foreach loop won’t run nor ‘GetItemInSlot()’ since the slot is empty!!! So this is the reason why default weapon not add itself damage stat!!!

So I would like to ask for the proper solution to this problem, anyone? please @Brian_Trotter @sampattuzzi

Really not sure why no one every asked about this? Or I misunderstood on some part, Thanks!

This debugging show ‘equippedItem count == 0’


The best way to work this in is to create an Unarmed WeaponConfig. Make this unarmed WeaponConfig the default weapon, and for good measure, make an Animator Override for it (just don’t override any methods). When we add sound effects a bit later, create a prefab with just the AudioSource logic on it and set it as the Weapon’s model

@Brian_Trotter Thanks for the reply. I’m not quite sure what you mean by that because up till this point we already got the un-armed as weapon config with all the properties like its own animator override and stuff just like others weapon. Also use it as default weapon in Fighter class but still the problem is that when we equip it in Fighter class as default weapon that case the equiappable item slot has to be EMPTY so how would I do that to make it know that there is default weapon? So this stats system can know that it need to take this default weapon additive stat in calculation?

Hmmm… let’s try making some adjustments to make sure that the default weapon is added to the Equipment…

In SetupDefaultWeapon()

private Weapon SetupDefaultWeapon()
{
     equipment.AddItem(EquipLocation.Weapon, defaultWeapon); //this will trigger UpdateWeapon
}

and in UpdateWeapon()

{
  var weapon = equipment.GetItemInSlot(EquipLocation.Weapon) as WeaponConfig;
  if(weapon==null)
  {
     SetupDefaultWeapon();
  }
  else
  {
      EquipWeapon(weapon);
  }
}

Now there is one problem with this approach… when you equip a new weapon, a copy of the default weapon will be added to the Inventory… If you then unequip the new weapon, yet another copy of the default weapon will be created in the Equipment… Certain weapons you really don’t want to wind up in inventory (Unarmed), and if you start the player with a rusty pocketknife, the player can create infinite copies…

For this, we’ll need to borrow an interface from the Shops and Abilities course, intended to allow us to pick up coins and have them deposited automagically in the Purse…

namespace GameDevTV.Inventories
{
    public interface IItemStore
    {
        int AddItems(InventoryItem item, int number);
    }
}

Next a small change to AddToFirstEmptySlot() in Inventory.cs:

        public bool AddToFirstEmptySlot(InventoryItem item, int number)
        {
            foreach (var store in GetComponents<IItemStore>())
            {
                number -= store.AddItems(item, number);
            }
            if (number <= 0) return true;

            int i = FindSlot(item);

            if (i < 0)
            {
                return false;
            }

            slots[i].item = item;
            slots[i].number += number;
            if (inventoryUpdated != null)
            {
                inventoryUpdated();
            }
            return true;
        }

And finally implementing the IItemStore Interface in Fighter.cs

public class Fighter: MonoBehavior, IAction, IItemStore

and to implement IItemStore

        public int AddItems(InventoryItem item, int number)
        {
            return item == defaultWeapon ? 1 : 0;
        }

So what this does is check to see if the item being added to Inventory (from the process of equipping another item or dragging the default weapon from Equipment into the inventory) is the default weapon. If it is the default weapon, then we just drop it from the transaction.
This means you’ll need to be careful about what you make as the default weapon. If it’s a sword you could otherwise find in the world, the player will get cranky if it dissappears. (IMO, the best starting weapon is always unarmed)

2 Likes

Wow appreciated for your answer! I’ll try out after finish this section, look pretty interesting! :handshake:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms