Forum rules - please read before posting.

AC scripting - How to access Container label etc

So I am using the shop system example to implement a shop system of my own. I changed a few things and I was wondering how I can set my ShopHeader.Text to the name of the container's label. That way whenever I go to different shops, the header for that shops name is different.

The label comes from the Container script attached. And I have the ShopBase and ShopBuy scripts from the shop system example.

I used this code which seems to work (ShopBase.cs):

            private void OnMenuTurnOn(Menu menu, bool isInstant)
            {
                if (thisMenu == null) thisMenu = KickStarter.playerMenus.GetMenuWithCanvas(GetComponent<Canvas>());
                if (thisMenu == null || menu != thisMenu) return;

                MenuInventoryBox shopInventoryBox = menu.GetElementWithName(shopItemsElementName) as MenuInventoryBox;
                for (int i = 0; i < shopInventoryBox.InvInstances.Count; i++)
                {
                    if (!InvInstance.IsValid(shopInventoryBox.InvInstances[i]))
                    {
                        slotPriceLblTexts[i].gameObject.SetActive(false);

                    }
                    else
                    {
                        slotPriceLblTexts[i].gameObject.SetActive(true);
                        //int slotPrice = shopInventoryBox.InvInstances[i].Count * shopInventoryBox.InvInstances[i].GetProperty(0).IntegerValue;
                        int slotPrice = shopInventoryBox.InvInstances[i].GetProperty(0).IntegerValue;
                        slotPriceLblTexts[i].text = "$" + slotPrice.ToString();
                    }
                }

                headerText.text = shopInventoryBox.OverrideContainer.label;


                // When this menu is turned on, cache variables

                totalCostVar = GlobalVariables.GetVariable(totalCostGVariable);
                amountToBuyVar = GlobalVariables.GetVariable(quantityGVariable);
                OnMenuTurnOn(menu);

                totalCostVar = GlobalVariables.GetVariable(totalCostGVariable);
                amountToBuyVar = GlobalVariables.GetVariable(quantityGVariable);
            }

The line of code I am talking about is the one that says
headerText.text = shopInventoryBox.OverrideContainer.label;

And that seems to work but I don't know if its the right way and if its something that can screw up some other stuff. Is there another solution or an explanation of this "OverrideContainer" and what it is etc.

Am I using the right method?

Comments

  • Yes, that's the way - you're just reading data so it's not going to interfere with anything.

    Details of the API can be found in the Scripting Guide.

    OverrideContainer is a property of the MenuInventoryBox class, which lets you map a specific Container to it - as opposed to the "default" opened with the Container: Open Action.

  • edited December 2023

    @ChrisIceBox awesome thanks.

    While I'm on this container topic, I also wanted to know how can I give custom variable/property for a container?

    Reason is, I wanna give it a boolean like bool perishableInventory. So that can be used to determine certain shops where they have limited items and if you buy from it, it gets removed from the shop inventory too.

    But not all shops are limited, some have unlimited. So does container have something like a custom property similar to inventory Items?

    Ofcourse I can just add the bool in the default container.cs script but not really keen on breaking that script incase

  • You'd need to add your property as a separate component to the Container object. This could be a custom script, or just a regular AC Variables component.

    When the item is transferred, you'd then hook into the appropriate event and then make adjustments after checking for the presence of the component and property.

    You can check if a given InvInstance is associated with a Container with the GetSourceContainer function.

  • Alright thanks, I was also thinking about applying the variables or custom script component but I thought maybe I missed a built in feature with the container in the editor that perhaps allows me to do that.

    Thanks again

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.