Auto Unequip Weapon

RaeJjin0

Member
Hello, i would like to know how to make the character having it's weapond "unequipped" at the game start ?
I mean by that, that the sword should be places in the character's holster when you start the game, right now when you hit Play Mode, the character has it's sword automatically equipped (meaning he's holding it with his hands).

I tried to check "Immediate Unequip" in the Item Equip Verifier ability, with no success
 
Maybe you can try uncheck the “automatically equip” on that sword’s inspector (I don’t remember exactly where that property is), then in your code, pick up that sword instead of have it from the start.
 
I tried so, but like you said it makes me not having it from the start, and the silly me doesn't know how to make the character auto-equipping through script it without having it instantly toggled nor picking it up on the ground uh... It's painful because i've managed to do it few months ago but my previous project got corrupted and i can't remember how i did such a thing ?
 
C#:
using Opsive.UltimateCharacterController.Events;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Items;
using Opsive.UltimateCharacterController.Traits;
using Opsive.UltimateCharacterController.Items.Actions;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Character.Abilities.Items;
using Opsive.UltimateCharacterController.Utility;
using Opsive.UltimateCharacterController.FirstPersonController.Items;

public class TestEquipUnequip : MonoBehaviour
{
    [SerializeField] private UltimateCharacterLocomotion _characterLocomotion;
    [SerializeField] private InventoryBase _inventory;
    [SerializeField] private ItemType _rifleItemType;
  
    private void Start()
    {
        //First, pickup your item.
        _inventory.PickupItemType(_rifleItemType, 1, -1, true, false);


        //If pickuped item is in equip state, just unequip it. If the pickuped item has already in unequip state, you do not need the code below.
        var equipUnequipAbilities = _characterLocomotion.GetAbilities<EquipUnequip>();
        int itemSetIndex = 0;    //If 0 does not work, try 1, 2, ... This index is your weapon item set index.
        if (equipUnequipAbilities != null && equipUnequipAbilities.Length > 0)
        {
            for (int i = 0; i < equipUnequipAbilities.Length; ++i)
            {
                if (equipUnequipAbilities[i].ItemSetCategoryIndex == 0)
                {
                    equipUnequipAbilities[i].StartEquipUnequip(itemSetIndex, true, true);
                    return;
                }
            }
        }
    }
}

Here is the example. And don't forget to let your inventory default loadout blank.

Screen Shot 2020-06-16 at 12.33.37 PM.png
 
For more details:

In the above code, ItemSetCategoryIndex == 0 means "Weapons", because my rifle is belong to Weapons category item type.

Screen Shot 2020-06-16 at 12.41.05 PM.png
 
Thank you for helping me so much ?

I let my inventory default loadout blank and after trying to create the script, i got this error :
Assets\Scripts\TestEquipUnequip.cs(21,20): error CS1061: 'InventoryBase' does not contain a definition for 'PickupItemType' and no accessible extension method 'PickupItemType' accepting a first argument of type 'InventoryBase' could be found (are you missing a using directive or an assembly reference?)

And when you put the ItemType like this :
Code:
    [SerializeField] private ItemType _rifleItemType;
Is it because you do have an ItemType called rifleItemType, and put a "_" with it on that line ?
If that's so, then i'd be right to make it this way :
Code:
    [SerializeField] private ItemType _Sword;
if my ItemType is simply called Sword, right ?
 
if my ItemType is simply called Sword, right ?

No, it is just the variable name, it is not required to be named like that. You can name your variable "_myHolySword" while it is actually a gun.
The important part is in TestEquipUnequip inspector, click on that "_myHolySword" and choose your weapon type on the popup window.

'InventoryBase' does not contain a definition for 'PickupItemType' and no accessible extension method 'PickupItemType' accepting a first argument of type 'InventoryBase'
That's strange. It should be "...accepting a first argument of type 'ItemType'..." Are you sure you didn't change a thing on that script? And make sure to check the function "PickupItemType" in InventoryBase.cs to fix this error.
 
And, don't forget using UnityEngine; using System.Collections; ... and any default-things which a new class requires.
 
That's pretty weird, i just checked the InventoryBase script, and i don't have any function " PickupItemType " :oops: I checked on the script using Ctrl F and it returned 0 results
 
That's pretty weird, i just checked the InventoryBase script, and i don't have any function " PickupItemType " :oops: I checked on the script using Ctrl F and it returned 0 results
The full name is "Opsive.UltimateCharacterController.Inventory.InventoryBase", it lied in
Assets/Opsive/UltimateCharacterController/Scripts/Inventory

If your file and path is correct, maybe the cause is your version is older or newer than my version.
 
Yep it's the same path, and i don't see any update available for me x) that's pretty unlucky, the precise function i needed to finally make it work probably has been modified/erased on newer versions ahah
 
I have checked the docs. It is renamed to "Pickup". The parameters are the same.
_inventory.Pickup(_yourSword, 1, -1, true, false);

Here is the docs:
 
Thank you i have no errors anymore !
I just placed my character in the Character Locomotion and Inventory tabs, and the Sword Type in the item type field, but after hitting play the character does have the sword equipped even tho i left the inventory blank like expected, but it is equipped in both meanings, the sword is unleashed x)
 
That part of code will equip the item if it is unequipped and vice versa, unequip the item if it is equipped.
In your case, I think your sword is unequipped, so the code has equipped it...
 
I did try to remove that part, believe me or not the sword gets equipped whether or not i keep that part of code... My brain is burning right now x)
 
That's strange. I used that code only and my rifle always starts with the unequipping state.
Maybe your sword's itemSetIndex is not 0? Or it's ItemSetCategoryIndex is not 0?
I think you need to put some debug logs to reveal it.
 
If i put any other value than 0 for itemSetIndex, the game starts with the weapon unequipped, but it's like i don't have it at all, i can't draw it.
This means that 0 was the right value, and that since any other numbers won't work, having the Inventory default loadout left blank does works.
But then, if i am not supposed to have a weapon because of the blank inventory loadout, and if 0 was the right itemSetIndex, why doesn't it work with or without the last part of the code omg :unsure:
 
You might want to try manually calling the StartEquipUnequip method after loading into the game:

 
Top