Inventory Pickup Causes Duplicate Items

Im getting close to having most of what I want working in my scenes now. I have put in code, but I need to test more, on my ground pickup items I had asked about in an earlier post to stop this BUT it is occuring now from the inventory panel window. When I select a clothing outfit thats in inventory and put it on and close out when I load back up it has duplicated the item. How do I turn that off? Thanks again!

*Keep in mind my inventory I dont want multiple items. If the player finds said outfit then it gets added to the inventory panel. I dont want to remove it when they put the item on. It should always be a choice when they can get to the inventory dresser, the only place they can access the inventory.
 
Last edited:
I think I have this one fixed. Was my custom code. But I swear when I started on this it was duplicating stuff in the inventory panel but after fixing a line of code it seems to have stopped.
 
Im still working on this, Unity crashed and undid some changes I think was leading to a fix and Ive not made it back to this. Probably be end of the week or the weekend before getting back to this error. Heres hoping I fixed it, twice... :)
 
Ok its this code Im tryng to use. Ive spent several days trying to figure this out but I cant make it work.
Code:
public void AddSchoolGirlInventory()
{       
    var SchoolDressCheck = InventorySystemManager.GetItemDefinition("SchoolDress");
    if (m_Inventory.HasItem((0, SchoolDressCheck)))
    {
        Debug.Log("I have 0 SchoolGirlDresses in Inventory");
        m_Inventory.AddItem("SchoolDress", 1);
    }
}
As one that I know doesnt work, the debug logs show it just always picks up the school girl dress making hundreds in inventory and filling it up. How do I make this if statement work that once it has ONE item it doesnt do this code anymore?
 
So the HasItem function is not a "does the inventory have excatly that amount" it's a "does that inventory have at least that amount"

So you'll always have at least 0 schooldress.

You have two choices here. Either do GetItemAmount(...) for exact equality (or non equality) or do this:
Code:
public void AddSchoolGirlInventory()
{       
    var SchoolDressDefinition = InventorySystemManager.GetItemDefinition("SchoolDress");
    if (m_Inventory.HasItem((1, SchoolDressDefinition)) == false )
    {
        Debug.Log("I have 0 SchoolGirlDresses in Inventory");
        m_Inventory.AddItem(SchoolDressDefinition, 1);
    }
}

See how I check if the inventory does NOT have at least one item.
Also note how I use the ItemDefinition for the school dress rather than the string name when adding the item.

Instead of hard coding the string in code you could make a serialized field and add the ItemDefinition in the inspector. Then pass in the ItemDefinition as a parameter to your function so you can use the same function for all your items rather than copy paste and replacing the string each time. Just my 2 cents
 
Thank you! Ive been stumbling around with this for a week LOL. I originally had the 1 in there but thought that might have been the culprit seeing how my other if statements work in unity. So when using var SchoolDressDefinition that isnt just a temp variable for this instance? Why I had var SchoolDressCheck I just thought that was a local variable being used?

Im using code this way because its just easier at this time where Im at. The whole plot of my game is that the player shrinks/grows but nothing else does. Unity scales all the clothing items with the parent so Ive had to make renders roughly every 5% change in size. Alot of my clothes take 20 items to make one outfit. So in this case the simple college schoolgirl outfit has 20 renders and then code swaps that based on avatar size to make the apperance that the clothes have gotten larger or smaller. Since Opsive updated the controller from version 2 to version 3 swapping avatars during runtime is much easier now, I have a whole test system on that for level 3 and 4 that uses avatar swapping size and trying to keep the wearables the same size. Before with version 2 I had done some minor adjustments and it didnt work great but it was ok. Version 3 update on the controller just made that so much better so I might move everything to that instead of this system but that will take months so for now thats where Im at. The next big thing will be the assembly video you sent me, that simple short video helped me understand that I need to update and use those and it will make my code so much faster.

Ill do more testing here with this code, what has stumped me is sometimes I get it to work and other times it doesnt. But this is the first time schooldress has worked. Thanks again!
 
So when using var SchoolDressDefinition that isnt just a temp variable for this instance? Why I had var SchoolDressCheck I just thought that was a local variable being used?
yes, var SchoolDressDefinition is just a temporary variable referencing the item definition object. This object is a ScriptableObject and you can assign it in a
Code:
[SerializedField] private ItemDefinition m_MyItemDefinition;
This way you can assign it in the inspector.

That's just a suggestion though, you don't have to do it this way. It's just to make your code reusable instead of having the write the script you wrote above for each item.
 
So Im realizing that now that I set this to 1 yes it stops picking up the item and duplicating but it isnt adding it to inventory anymore period. I guess Im trying the number 2? Changing to 2 didnt work. I think I might just use another variable and toggle it active and use it as a check. This has not been working at all.
 
Last edited:
Could you give a bit more information on what is not working?

Are you not getting the log " I have 0 SchoolGirlDresses in Inventory "?
Or are you getting it but somehow no item is being added?
Or do you get the log only once but you can't see the item in the inventory?

I can't think of any reason the script above would not work.

If you need to send me your script and I can test it on my machine to see if the issue is your script or something else
 
Thanks but yeah Im just not sure and that script relies on quite a few and has about 20k lines so it requires alot and my working folder is 550 gb. Keep in mind I have about 100 outfits to choose from, I have the schoolgirl one working but that seems to have broken all the other ones. Before I had all the other ones working just not schoolgirl. I think its just with the variables? Im just not sure at this point but I know this solution should work Ive just got a couple hours of coding to get it implemented. Here is a snipet of code what Im changing to that should work, I think lol.
Code:
if (hit.collider.gameObject.name == "BlackSportsOutfitGRD")
                    {
                        if (!DialogueLua.GetVariable("InventoryBlackSportsOutfit").asBool)  //Has the player found the clothes yet for inventory?
                        {
                            DialogueLua.SetVariable("InventoryBlackSportsOutfit", true);  //Stop Inventory from Duplicating;  
                            m_Inventory.AddItem("BlackSportsOutfit", 1);
                        }
                        RemoveOutfit();
                        BlackSportsOutfitGRD.SetActive(false);
                        StartCoroutine(WearAfterDelay("BlackSportsOutfit"));
                        return;
                    }

And to answer other questions, the schoolgirl one last I checked was working but then all the others broke. Here is an example of one of the others and when I just take out the if inventory has statement it starts working. This just seems to be what is does, sometimes it works and sometimes it doesnt. This time it just wasnt adding the item to the inventory panel, acting like it already had the item.
Code:
if (hit.collider.gameObject.name == "JeansOutfitGRD")
                    {
                        var JeansOutfitDefinition = InventorySystemManager.GetItemDefinition("JeansOutfit");
                        if (m_Inventory.HasItem((1, JeansOutfitDefinition)))
                        {
                            m_Inventory.AddItem("JeansOutfit", 1);
                        }
                        JeansGRD.SetActive(false);
                        RemoveOutfit();
                        StartCoroutine(WearAfterDelay("JeansOutfit"));
                        return;
                    }
 
Last edited:
Ok I see what was going on finally. The schoolgirloutfit got changed to the actual definition and I didnt do that with all the others.
 
Top