Forum rules - please read before posting.

Return an inventory item

Ok, more newbie questions here.
My container has this code.

private void OnContainerAdd(Container container, InvInstance containerItem)
{
    if (containers.Contains(container))
    {
        UpdateStats();
    }

}

I want to check which item containerItem is, but I can't get the right syntax. So something like

if (containerItem == "name of inventory item")
{

}

Comments

  • edited November 2021

    The containerItem variable is of the type InvInstance, which is a wrapper class for inventory item instances.

    You can access its InvItem property to extract its label for comparison:

    if (invInstance.InvItem.label == "name of inventory item")
    {
        //
    }
    
  • Thanks! And the same question but with the container name :)

    Basically my idea is that the character sprite will have child objects with separate clothes/equipment sprites so that the equipment will have visual representations. As in, equiping a cap will really put a cap on her head.

    Turning an object visible is easy because I can do that with an action list (via the "use" interaction in the inventory panel), but when I empty the container again (i.e. unequip the item) I have to script the effect.

    My idea is that I can tag all game object children that hold the head-related sprites with e.g. "Headgear", and then use OnContainerRemove to first check if the container in question is indeed the HeadContainer, and then use GameObject.FindWithTag("HeadGear") to turn all those objects invisible.

    Am I on the right track? Can I find gameObjects like this, and how do I get them invisible? Do I have to use transform instead of gameobject?

    I'm unsure how I to do this, though, so some guidance would be appreciated!

  • For the container name, you can read the "container" parameter that the OnContainerAdd event provides:

    container.gameObject.name
    

    If you want to avoid scripting where possible, you're probably best off using sub-layers in your Animator to control the visibility of your various child objects through animation. Each body part (hat, shirt) etc would have its own sub-layer, so that each can be controlled independently. An integer parameter for each layer could then be used to set which e.g. hat type is shown at any one time.

    To control which one is visible, you'd then just need to update the correct parameter value, which can be done with scripting, or AC's Character: Animate / Object: Animte Actions.

    Is this for a 2D or 3D game, and what is your character's "Animation engine" set to?

  • I see, but I've made sure that no piece of clothing/equipment is affected by the animation - this is all based on handpainted, hi-res 2d, and animating the character with all various outfits would be far too overwhelming.
    So stuff like hat, purse etc will just move along the sprite and flip left/right. So if I only learn how to control the visibility of the child objects from the container script, I'd be happy.

  • edited November 2021

    Update:
    Ok, so I can successfully use SetActive(false) in the code to "turn off" a child object, but now I can't turn it visible again in the AC action list (since it doesn't exist, obviously). It seems there's no action to "set active", only make an object visible/invisible.

    This means I must a) find an AC action to activate an object, or b) set the visibility in the container code.

    Edit:
    If this turns out to be unnecessarily complicated, I'll of course try that Animator sub-layer method instead!

  • edited November 2021

    It may be possible to use the Object: Call event Action to set an object's "active" state, but in my experience it can bring issues when trying to locate an object to activate it again.

    You can use the Object: Visibility Action to enable/disable an object's Renderer component without this issue, though - this includes the Sprite Renderer type.

    On the animation approach: I wasn't suggesting creating alternative animations for your character, but using sub-layers to control each equipment sprite independently.

    These sub-layers can run at the same time as the character's "standard" animations on the base layer, so if all the sub-layers are doing are controlling the visibility of your equipment, you can still rely on your character's existing standard animations at the same time.

  • edited November 2021

    Thanks again for your reply. I solved it myself by using this code:

    private void OnContainerRemove(Container container, InvInstance containerItem)
    {
    if (containers.Contains(container))
    {
    UpdateStats();
    }
    if (container.gameObject.name == "HeadContainer")
    {
    gear = GameObject.FindGameObjectWithTag("HeadGear");
    Renderer rend = gear.GetComponent();
    rend.enabled = false;
    }
    }

    This mimics the action Object: Visibility. The problem was I can't use AC's action here, since moving an object from a container to the inventory doesn't allow for action lists, only script (the other way around, however, gives me access to an action list (the "use" interaction) which is handy).

    As far as I can tell, this does the job, but if you spot anything problematic let me know!

  • edited November 2021

    Update:
    Ok, the above code didn't work because it would only find one object, so I had to change the code a little:

    private void OnContainerRemove(Container container, InvInstance containerItem)
    {
        if (containers.Contains(container))
        {
            UpdateStats();
        }
        if (container.gameObject.name == "HeadContainer")
        {
            GameObject[] gear = GameObject.FindGameObjectsWithTag("HeadGear");
    
    
            foreach (GameObject obj in gear)
            {
                obj.GetComponent<Renderer>().enabled = false;
            }
        }
    }
    
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.