Forum rules - please read before posting.

How to remove all the items from the inventory

I'd like to reset the inventory of the player, removing all the objects.

Is it possible to do that via code?

Thank you very much.
«1

Comments

  • Try this:

    AC.KickStarter.runtimeInventory.items.Clear ();
  • It is possible to do this by Adventure Creator?
    I`m bad in coding
  • No, but it's quite simple: just place that in a function in a new C# script, place it in your scene, and invoke it from your ActionList using the Object: Call event Action.

    Create a new C# script, and paste the code as a function inside it:

    public void ClearItems ()
    {
      AC.KickStarter.runtimeInventory.items.Clear ();
    }


    Then attach it to a new GameObject, and use the Object: Call event Action to reference it.  You should find "ClearItems" as an available option within the Action's UI.  This will then cause the function to run - and the items to clear - when the Action is called.
  • Is this function still valid? I tried to use it but gives me errors...
  • public void ClearItems ()
    {
      AC.KickStarter.runtimeInventory.localItems.Clear ();
    }
  • Thanks now it works!  :)
  • Hi!
    I've tried to follow the aforementioned steps to create the script (with the updated version posted by Chris on Sept 2017), but in the console in get this error message:

    Assets/Inherit/Graphics/ClearItems.cs(5,7): error CS1525: Unexpected symbol void', expectingclass', delegate',enum', interface',partial', or `struct'

    I've copy-pasted the code inside the C# script, so that the script contains only this function (I've deleted the "using ..." lines at the beginning of the script, too).
    Can somebody help me with this?

  • P.s.: sorry I forgot:

    My Unity version is 2018.2.18f1
    My AC version is v1.65.2

  • Also, is it possible to make a button of the Pause menu to run this script?

  • The only thing you should remove from the default script Unity generates are the Start and Update methods. Your script name should also not match the name of the function inside it - I suspect you've named both ClearItems.

    To run code whenever a Button menu element is clicked, you can hook into the OnMenuElementClick custom event. See the Manual's "Menu scripting" and "Custom events" chapter for more on this. A sample script:

    http://pasteall.org/1442358/csharp

    Paste this into a C# file named ClearItemsFromMenu.cs, and replace the "Pause" and "MyButton" strings with the name of your menu and button element respectively.

  • Thanks! there's still an error, though... now it says:

    Assets/Inherit/Graphics/ClearInv.cs(8,52): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration

    Assets/Inherit/Graphics/ClearInv.cs(8,52): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration

    I guess it's talking about the brackets at the end of the function... should I replace those with something?

  • edited December 2018

    I can't tell without seeing the script itself. Copy it into e.g. pasteall.org.

  • Here it is!

    http://pasteall.org/1442368/csharp

    I kept the "public class" part because it looks like it didn't recognize the "void" one.
    Also, I kept the Monobehaviour because it didn't allow me to attach the script to a GameObject without it.

    The one for the menu button works fine, though!

  • The code needs to be wrapped inside a function, i.e.:

    http://pasteall.org/1442837/csharp

    Please bear in mind that this is not a coding forum, however - help on script fundamentals should be left to Unity forums and dedicated coding websites.

    To run this function from an ActionList, attach it to a GameObject and trigger its ClearInventory function using the Object: Call event Action.

  • Sorry, I thought this was strictly AC-related.

    With the script you provided, no errors pop up anymore: there's another issue, though.
    I've attached it to a GamObject in my scene, and in the ActionList Editor I set an action as follows:

    Object: Call event
    UnityEvent()
    Runtime only: No Function
    ClearInv

    But for some reason, the Action doesn't work.
    I've called the C# script "ClearnInv" because, with other names, it didn't let me attach it to a GameObject.
    I've even tried to change the "Runtime only: No Function" to "Runtime only: Monoscript -> ClearInventory", but it didn't work as well.

  • The name of the file must match the class name - in this case, ClearInv. ClearInventory is the name of the function inside it.

    Please post a screenshot of the Action in question, so we can see exactly what you've got. Alternatively, the Object: Send message Action can be used to send the message "ClearInventory" onto the object, with no need to refer to the script itself.

  • Hi!
    Here's a screen shot of the Action:

    https://imgur.com/gseHC3x

    I've tried to use the Send Message, too, but it didn't work. Here's a screenshot of the Action as well:

    https://imgur.com/dIe7nE4

  • Your Object: Call event Action is set to No function, so it's not going to trigger anything. It also looks like you've assigned the script itself in the left field.

    You need to instead assign the GameObject that the script is placed on, and then choose the "ClearInventory" method from within the "ClearInv" script as the function.

  • Oh, I see!
    Thank you Chris, now it works perfectly!

  • Might be worth looking into adding it as an Inventory action node so this can be run from an ActionList?

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.