Forum rules - please read before posting.

Removing certain category from inventory?

edited November 2017 in Technical Q&A
Hi, I wanted to know if it was possible to remove all items from a certain category from the inventory? 
I also needed help getting the whole 'removing everything from inventory' thing to work anyway,
I read this: Here
I don't understand this part "Then attach it to a new GameObject, and use the Object: Call event Action to reference it. " 

Does the game object need to be in the scene all the time? Does the gameobject need an action list on it or do you call it from a different action list altogether? Sorry I'm pretty dumb when it comes to instructions

Thanks :)

Comments

  • edited November 2017
    Also the plan is that when a timer is up, the inventory is removed, so it's all from an actionlist (apart from the removing from inventory code). 
  • Currently there is no similar function that removes all items limited to a certain category, but I'll add one.  It can still be done in code, though:

    public int categoryToRemoveID;

    public void ClearItemsInCategory ()
    {
      foreach (AC.InvItem item in AC.KickStarter.runtimeInventory.localItems)
      {
        if (item.binID == categoryToRemoveID)
        {
          AC.KickStarter.runtimeInventory.Remove (item);
        }
      }
    }

    Both the Object: Call event and Object: Send message Actions can be used.  If you place the above code inside a new C# MonoBehaviour script, you should be able to make it a prefab by attaching it to a new empty GameObject and moving it into the Assets folder somewhere.  IIRC you can still send events / messages to prefabs in that way, so you can do without having a local scene object.
  • Hey Chris, thanks for that,
    I've put what you said in a script, put that script on a gameobject and put it in assets, now I'm trying to 'call event' on it in an action list but I'm getting a whole bunch of stuff come up, for example:
    image

    Can you think of what I did wrong? I tried it the Object: send message way too and nothing happens in game.

    Thanks :)
  • You're nearly there - you now just need to choose which function gets called.  It's in your screenshot: ClearItemsInCategory ().
  • edited November 2017
    Hi Chris, I tried that and nothing is working. It's a massive actionlist so I won't show all of it but the beginning is the important part-- it will only run if the time is 0:0
    Then it removes everything that's already there and then adds the new ones in again. 
    But in game it doesn't do anything, in fact it adds extra ones instead.
    Also I put ID 5 as the 'Category to remove' option on the gameobject (in fact I tried all of them)
    image
  • Is the code actually running?  Place a Debug.Log inside the function to confirm that it is:

    Debug.Log ("It's running!");

    However, the latest v1.60.2 update now includes a function to clear the items in a given category - so it's better to just use that instead:

    AC.KickStarter.runtimeInventory.RemoveAllInCategory (2); // where "2" is the category's ID
  • edited November 2017
    Hi Chris, I've updated and changed the code to use that, it's still not working?
    What I've found is that the code now removes /most/ of the items and stops anything else from being added afterwards.

    Do you mind copy pasting what the script should look like, if you'd mind?
  • Apologies - there is a bug with the function.

    I'll fix it officially in the next release, but in the meantime you can open RuntimeInventory.cs, and replace the RemoveAllInCategory function (from line 633) to:

    public void RemoveAllInCategory (int categoryID)
    {
        for (int i=0; i<localItems.Count; i++)
        {
            if (localItems[i].binID == categoryID)
            {
                Remove (localItems[i]);
                i = -1;
            }
        }
    }


    The script to call it should then simply be:

    using UnityEngine;
    using System.Collections;
    using AC;

    public class ClearCategoryTest : MonoBehaviour
    {

        public int categoryID;


        public void DoClear ()
        {
            KickStarter.runtimeInventory.RemoveAllInCategory (categoryID);
        }

    }

  • edited November 2017
    Hi Chris,
    Thanks for that. All the items now get removed. 

    Only issue is now is that new items for category 5 don't get added anymore (the script is supposed to clear all the stuff in the inventory and then new items get added after), but if I disable the inventory remove action then items get added like normal (except of course the old items dont get deleted)

    Not sure why...do you have any idea or are you able to recreate it?



    EDIT: Nevermind managed to fix it by putting an Engine:Wait 1 second afterwards before adding the new items. Maybe it was deleting them at the same time they were being added or something.
    Thanks a lot Chris, you're awesome :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.