Forum rules - please read before posting.

"Add All" inventory items to Container?

I don't post often but I read constantly and this forum (and AC!) continues to be an amazing resource.

I have a gameplay situation where there are multiple player characters who can die/be eliminated from the game. When this happens, I would like any and all inventory items the Active Player was carrying to be passed to a "Lost & Found" Container I've designated.

There are the "Remove All" options for both Container and Inventory which keep dancing around what I want but my brain has been unable to make a blanket solution for this so far.

I have the long way working where an ActionList checks to see if the character has every single potential inventory item and then, if so, sends it to the container but this is obviously not the most efficient way.

Is there a way to accomplish this without a custom script that I am just missing? Thanks!

Comments

  • I'm not sure there is a very direct way of doing this with the default actions. But a simple custom script like this should do:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using AC;
    
    
    public class ItemsUponDeath : MonoBehaviour
    {
        public Container lostAndFound;
    
        void TransferToLostAndFound()
        {
            lostAndFound.InvCollection.TransferAll(KickStarter.runtimeInventory.PlayerInvCollection);
        }
    
    }
    

    Add it to a gameobject, assign the desired Lost and Found container, and use the action Object > Send message > Custom > Method name: TransferToLostAndFound to transfer the active player's inventory to it.

    I haven't tested the script (I've just written it here), so please test it, but it should work.

  • Thanks so much! Trying to get this working now.

    The way you have it written, which gameobjects should I be attaching this script component to? The player characters, inventory items, or the container itself? Apologies if this should be obvious.

  • Basically, you can add this script to any gameobject in the scene, and then you use the "send message" action to run a method within the script. Let me run you through the script so you understand what is going on:

    public Container lostAndFound;

    This means that we are declaring the existence of a container called lostAndFound. Because it's a public container, you will be able to see it in the inspector, and you can assign a value to it there (i.e. the container object).

    void TransferToLostAndFound()

    This means we created a method called TransferToLostAndFound. A method is a block of code that will run when that method is called.

    lostAndFound.InvCollection.TransferAll(KickStarter.runtimeInventory.PlayerInvCollection);

    This is the code you run when you call the method.

    So we have the lostAndFound container, and all containers have their own InvCollection, which is where item data is stored. Usefully, the InvCollection class has a TransferAll function, which is used to transfer all items from another collection to it. So what we are doing here is give the TransferAll function the KickStarter.runtimeInventory.PlayerInvCollection parameter, which represents the player inventory collection during runtime.

    The reason it doesn't matter which gameobject you attach this script to is that the script doesn't make reference to its own gameobject! Yes, I could have written it differently, for example by making the lostAndFound container private (won't be displayed in the inspector), and then writing a line of code to assign the container we declared to the container component attached to the same gameobject as this script. Then you'd need to attach the script to the same gameobject as the container for it to work! But as is, it just needs to exist in the scene.

  • edited December 2023

    Scripting guide links for the mentioned classes and functions: Container, InvCollection and TransferAll.

    Yep, Rairun's code will do it - just bear in mind that you'll need to run it while the Player whose items you want to transfer is still the "active" one, i.e. before any Player: Switch Actions.

    If it's after the fact, you'd need to use the GetItemsFromPlayer function to extract the Inventory data from an inactive Player.

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.