Forum rules - please read before posting.

Get label to display an inventory item property + another variable

Hello,
I currently have a column with labels for displaying the "stats" (i.e property values) of highlighted inventory items.

Ideally, I'd like to display what the player's stats would be if they equipped it; i.e the property value + the global variable that holds the player's stat in question. Normally it's easy to include an operator to add two variable integers, but how do I turn the highlighted inventory item's properties into a variable?

Comments

  • edited May 11

    The currently-highlighted item instance can be read with the HoverInstance property:

    AC.KickStarter.runtimeInventory.HoverInstance
    

    From there, you can extract the intended property and set it to e.g. a Text variable with:

    if (InvInstance.IsValid (AC.KickStarter.runtimeInventory.HoverInstance))
    {
        int value = AC.KickStarter.runtimeInventory.HoverInstance.GetProperty ("MyProperty").IntegerValue;
        GlobalVariables.GetVariable ("MyVariable").TextValue = value.ToString ();
    }
    
  • edited May 12

    Sorry, but I don't understand why I would want to make the values strings instead of ints? My idea is that I set the label to display a value that is the sum of a global variable and the property value of the highlighted inventory item.

    This is what I have so far (never mind the different nomenclature; the stats were renamed at one point).

    using System.Collections;
    using System.Collections.Generic;
    using AC;
    using UnityEngine;
    
    public class UpdateLabel : MonoBehaviour
    {
        private void Start()
        {
            // Check if the hovered item is valid
            if (InvInstance.IsValid(AC.KickStarter.runtimeInventory.HoverInstance))
            {
    
                GlobalVariables.GetVariable("HoverSoft").IntegerValue = (AC.KickStarter.runtimeInventory.HoverInstance.GetProperty("Maturity").IntegerValue + GlobalVariables.GetVariable("Maturity").IntegerValue);
                GlobalVariables.GetVariable("HoverSophistication").IntegerValue = (AC.KickStarter.runtimeInventory.HoverInstance.GetProperty("Sophistication").IntegerValue + GlobalVariables.GetVariable("Sophistication").IntegerValue);
                GlobalVariables.GetVariable("HoverStreet").IntegerValue = (AC.KickStarter.runtimeInventory.HoverInstance.GetProperty("Cunning").IntegerValue + GlobalVariables.GetVariable("Cunning").IntegerValue);
                GlobalVariables.GetVariable("HoverMental").IntegerValue = (AC.KickStarter.runtimeInventory.HoverInstance.GetProperty("Pullups").IntegerValue + GlobalVariables.GetVariable("Pullup").IntegerValue);
    
    
            }
        }
    }
    

    If the code seems legit, I need to find a way to update this so that it repeatedly checks for highlighted items (right now the script has Start, but I need an update method here)

  • Ok, after fixing some typos I got this to work - my method right now is that I add a prefab object with the script every time I open the Menu and remover it when I close the menu again. Then the I use the Update() method to check for the highlighted item.

  • Update:
    Rats, I now realize it wasn't this simple. Using this method, I can only show what the character's stats would be if the highlighted item was the only item equipped. I forgot that I need to take into account the already equipped items of other types.

    I currently have one container for every part of the body that can be equipped. But now I need to check if each container has an item, and, if it's the same type (a fifth property with the string "body", "head" etc) as the highlighted item, ignore its bonuses (since it's the item that is potentially replaced) but items of other types should have their bonuses accounted for...

    All stats have a corresponding "base" variable to keep track of the unaugmented stat.

    I think I might need a hint or two to pull this off...

  • edited May 13

    Sorry, but I don't understand why I would want to make the values strings instead of ints?

    No problem, I'm just sharing generic example code - adapt as needed.

    If the code seems legit, I need to find a way to update this so that it repeatedly checks for highlighted items (right now the script has Start, but I need an update method here)

    Renaming Start to Update should be enough. We can see about moving to events later, to avoid it running every frame, but for now it's important to just get the calculation correct.

    I currently have one container for every part of the body that can be equipped. But now I need to check if each container has an item, and, if it's the same type (a fifth property with the string "body", "head" etc) as the highlighted item, ignore its bonuses (since it's the item that is potentially replaced) but items of other types should have their bonuses accounted for...

    Generally speaking, you'd have a reference to each of the Containers, iterate through them and tot up their stats onto the hoverInstance. Something like:

    void Calculate ()
    {
        InvInstance hoverInstance = KickStarter.runtimeInventory.HoverInstance;
        if (!InvInstance.IsValid (hoverInstance)) return;
    
        int totalValue = hoverInstance.GetProperty ("MyProperty").IntegerValue;
        foreach (Container container in containers)
        {
            InvInstance invInstance = container.InvCollection.GetInstanceAtIndex (0);
    
            // Ignore if no item in Container
            if (!InvInstance.IsValid (invInstance)) continue;
    
            // Ignore if "Type" property matches the hover item
            if (invInstance.GetProperty ("Type").TextValue == hoverInstance.GetProperty ("Type").TextValue) continue;
    
            // Increase the total value of "MyProperty"
            totalValue += invInstance.GetProperty ("MyProperty").IntegerValue;
        }
    }
    
  • I think I'm following. But a few questions:
    I assume I have to repeat the above for each of the 4 stats properties? So something like this:

    void Calculate ()
    {
        InvInstance hoverInstance = KickStarter.runtimeInventory.HoverInstance;
        if (!InvInstance.IsValid (hoverInstance)) return;
    
    
        int totalValueMaturity = hoverInstance.GetProperty ("Maturity").IntegerValue;
        int totalValueSophistication = hoverInstance.GetProperty ("Sophistication").IntegerValue;
        int totalValueCunning = hoverInstance.GetProperty ("Cunning").IntegerValue;
        int totalValuePullups = hoverInstance.GetProperty ("Pullups").IntegerValue;
    
        foreach (Container container in containers)
        {
            InvInstance invInstance = container.InvCollection.GetInstanceAtIndex (0);
    
            // Ignore if no item in Container
            if (!InvInstance.IsValid (invInstance)) continue;
    
            // Ignore if "Type" property matches the hover item
            if (invInstance.GetProperty ("Type").TextValue == hoverInstance.GetProperty ("Type").TextValue) continue;
    
            // Add the bonus value of each property to the respective total value
            totalValueMaturity += invInstance.GetProperty ("Maturity").IntegerValue;
            totalValueSophistication += invInstance.GetProperty ("Sophistication").IntegerValue;
            totalValueCunning += invInstance.GetProperty ("Cunning").IntegerValue;
            totalValuePullups += invInstance.GetProperty ("Pullups").IntegerValue;
        }
    
    
    }
    

    If I'm on the right track, I'll again set the total values to the global variables.

    Two more questions:
    1. How do I define or reference the containers?
    2. How do I call the Calculate ()?

  • edited May 14

    Update:
    I'm slowly progressing here, and this is what I have so far:

    using System.Collections;
    using System.Collections.Generic;
    using AC;
    using UnityEngine;
    
    public class UpdateLabel : MonoBehaviour
    {
        //public List<Container> containers = new List<Container>();
    
        void Update()
        {
            InvInstance hoverInstance = KickStarter.runtimeInventory.HoverInstance;
    
            if (!InvInstance.IsValid(hoverInstance)) return;
    
    
            GlobalVariables.GetVariable("HoverSoft").IntegerValue = hoverInstance.GetProperty("Maturity").IntegerValue + GlobalVariables.GetVariable("MaturityBase").IntegerValue;
            GlobalVariables.GetVariable("HoverSophistication").IntegerValue = hoverInstance.GetProperty("Sophistication").IntegerValue + GlobalVariables.GetVariable("SophisticationBase").IntegerValue;
            GlobalVariables.GetVariable("HoverStreet").IntegerValue = hoverInstance.GetProperty("Cunning").IntegerValue + GlobalVariables.GetVariable("CunningBase").IntegerValue;
            GlobalVariables.GetVariable("HoverMental").IntegerValue = hoverInstance.GetProperty("Pullups").IntegerValue + GlobalVariables.GetVariable("PullupsBase").IntegerValue;
    
            Container[] containers = FindObjectsOfType<Container>();
    
            foreach (Container container in containers)
            {
                InvInstance invInstance = container.InvCollection.GetInstanceAtIndex(0);
    
                // Ignore if no item in Container
                if (!InvInstance.IsValid(invInstance)) continue;
    
                // Ignore if "Type" property matches the hover item
                if (invInstance.GetProperty("Type").TextValue == hoverInstance.GetProperty("Type").TextValue) continue;
    
                // Add the bonus value of each property to the respective total value
                GlobalVariables.GetVariable("HoverSoft").IntegerValue += invInstance.GetProperty("Maturity").IntegerValue;
                GlobalVariables.GetVariable("HoverSophistication").IntegerValue += invInstance.GetProperty("Sophistication").IntegerValue;
                GlobalVariables.GetVariable("HoverStreet").IntegerValue += invInstance.GetProperty("Cunning").IntegerValue;
                GlobalVariables.GetVariable("HoverMental").IntegerValue += invInstance.GetProperty("Pullups").IntegerValue;
            }
    
    
        }
    }
    

    I feel like I'm on the right track, although I can't get the exact right numbers yet. Or actually, they aren't right at all - most of them are much too high, and I'm trying to find out what the problem is. It feels like part of the problem is that the base values are counted twice, but that alone isn't enough.

    PS: As you can see I first tried making a list with the containers, but that proved complicated since the containers are already in use in another script (this one: https://adventurecreator.org/forum/discussion/11681/equipment-window/p2)
    so I'm going with this findobjectsoftype method now.

  • edited May 14

    The use of FindObjectsOfType may be the issue - this will search for all Containers in the scene.

    If the numbers are wrong, I'd recommend simplifying and building things up from there.

    Start with just one property, and store it in a temporary int as I was earlier. You can update the GlobalVariable with the final int value at the end of the function, and you can then place Debug.Log statements at each stage to help show what's going on.

    Somethine like:

    using System.Collections;
    using System.Collections.Generic;
    using AC;
    using UnityEngine;
    
    public class UpdateLabel : MonoBehaviour
    {
        void Update()
        {
            InvInstance hoverInstance = KickStarter.runtimeInventory.HoverInstance;
    
            if (!InvInstance.IsValid(hoverInstance)) return;
    
            int hoverSoft = hoverInstance.GetProperty("Maturity").IntegerValue + GlobalVariables.GetVariable("MaturityBase").IntegerValue;
            Debug.Log ("Hover maturity: " + hoverInstance.GetProperty("Maturity").IntegerValue + ", Base: " + GlobalVariables.GetVariable("MaturityBase").IntegerValue + ", Total before containers: " + hoverSoft);
    
            Container[] containers = FindObjectsOfType<Container>();
    
            foreach (Container container in containers)
            {
                InvInstance invInstance = container.InvCollection.GetInstanceAtIndex(0);
    
                // Ignore if no item in Container
                if (!InvInstance.IsValid(invInstance)) continue;
    
                // Ignore if "Type" property matches the hover item
                if (invInstance.GetProperty("Type").TextValue == hoverInstance.GetProperty("Type").TextValue) continue;
    
                // Add the bonus value of each property to the respective total value
                hoverSoft += invInstance.GetProperty("Maturity").IntegerValue;
                Debug.Log ("Container " + containers + " found with maturity: " + invInstance.GetProperty("Maturity").IntegerValue + ", New total: " + hoverSoft, container);
            }
    
            Debug.Log ("Final total: " + hoverSoft);
            GlobalVariables.GetVariable("HoverSoft").IntegerValue = hoverSoft;
        }
    }
    
  • edited May 14

    Ok, this is super confusing. Trying out your code, it works perfectly.
    I add the second variable and property, but this one won't add up right. I'm struggling to see a pattern, but it's like it's completely messing up which values to add.

    using System.Collections;
    using System.Collections.Generic;
    using AC;
    using UnityEngine;
    
    public class UpdateLabel : MonoBehaviour
    {
        void Update()
        {
            InvInstance hoverInstance = KickStarter.runtimeInventory.HoverInstance;
    
            if (!InvInstance.IsValid(hoverInstance)) return;
    
            int hoverSoft = hoverInstance.GetProperty("Maturity").IntegerValue + GlobalVariables.GetVariable("MaturityBase").IntegerValue;
            int hoverSophistication = hoverInstance.GetProperty("Sophistication").IntegerValue + GlobalVariables.GetVariable("SophisticationBase").IntegerValue;
    
            //Debug.Log("Hover maturity: " + hoverInstance.GetProperty("Maturity").IntegerValue + ", Base: " + GlobalVariables.GetVariable("MaturityBase").IntegerValue + ", Total before containers: " + hoverSoft);
    
            Container[] containers = FindObjectsOfType<Container>();
    
            foreach (Container container in containers)
            {
                InvInstance invInstance = container.InvCollection.GetInstanceAtIndex(0);
    
                // Ignore if no item in Container
                if (!InvInstance.IsValid(invInstance)) continue;
    
                // Ignore if "Type" property matches the hover item
                if (invInstance.GetProperty("Type").TextValue == hoverInstance.GetProperty("Type").TextValue) continue;
    
                // Add the bonus value of each property to the respective total value
                hoverSoft += invInstance.GetProperty("Maturity").IntegerValue;
                hoverSophistication += invInstance.GetProperty("Sophistication").IntegerValue;
    
                //Debug.Log("Container " + containers + " found with maturity: " + invInstance.GetProperty("Maturity").IntegerValue + ", New total: " + hoverSoft, container);
            }
    
            //Debug.Log("Final total: " + hoverSoft);
            GlobalVariables.GetVariable("HoverSoft").IntegerValue = hoverSoft;
            GlobalVariables.GetVariable("HoverSophistication").IntegerValue = hoverSophistication;
        }
    }
    

    I'm commenting out the debugs for now, since I can easily monitor all the values by looking at the variables while I test the code

  • edited May 14

    I'm gonna try to explain how the values are changed with the code from above, to see if you can work out the pattern.

    Just for testing purposes, I've set all the base stats to 5. Just to be clear, I'm only testing the first two stats/variables/properties now.

    Hovering over an item with the stats (Soft 0, Sophistication 0):
    Correctly showing HoverSoft as 5
    Incorrectly showing HoverSophistication as 7 (?)

    Hovering over an item with identical stats, but this time a different "type" ("body" instead of "head")
    Both variables = 5 (correctly

    Hovering over an item with the stats (0, 2)
    HoverSoft = 5
    HoverSophistication = 9

    With one garment equipped (2, 0) bringing the first base stat up to 7:
    Hovering over an item with (0, 0):
    Showing both as 7 (should be 7, 5)

    Hovering over an item with (0, 2):
    HoverSoft = 7
    HoverSophistication = 9 (effectively doubling the hover value)

    If I add Street/cunning, I get similar results. Sophistication and Street almost always get extra points, sometimes exactly doubling the hover-value, but sometimes not.

  • Re-add the logs for the Sophistication - we need to see exactly what's being added and from where.

  • edited May 14

    Trying with base value 5, hovering over a +2 item:

    Logs:
    Hover sophistication: 2, Base: 5, Total before containers: 7 [correct]
    Container AC.Container[] found with sophistication: 2, New total: 9 [incorrect, there are no containers with items in them, let alone with sophistication bonuses]
    Final total: 9

    Update:
    Similar results with Street/Cunning, only now it's
    "Container AC.Container[] found with Street: 4"

  • edited May 14

    Update:
    Ok, I solved this for now.
    First I made a list of names of containers that the script should search for, and then it almost worked, and finally I spotted the error; there was an old naming confusion where I had accidentally mixed up two containers.

    It now works as intended, although I fear the code is quite clunky :)

    Edit:
    Argh, I spoke too soon. To be updated.
    Ok it looks now that the HeadContainer isn't included in the mix, for whatever reason. I've checked the spelling a gazillion times. The current code:

    using System.Collections;
    using System.Collections.Generic;
    using AC;
    using UnityEngine;
    
    public class UpdateLabel : MonoBehaviour
    {
        public string[] expectedContainerNames = { "HeadContainer", "NeckContainer", "FaceContainer", "BodyContainer", "HandContainer" };
    
    
        void Update()
        {
            InvInstance hoverInstance = KickStarter.runtimeInventory.HoverInstance;
    
    
            if (!InvInstance.IsValid(hoverInstance)) return;
    
            int hoverSoft = hoverInstance.GetProperty("Maturity").IntegerValue + GlobalVariables.GetVariable("MaturityBase").IntegerValue;
            int hoverSophistication = hoverInstance.GetProperty("Sophistication").IntegerValue + GlobalVariables.GetVariable("SophisticationBase").IntegerValue;
            int hoverStreet = hoverInstance.GetProperty("Cunning").IntegerValue + GlobalVariables.GetVariable("CunningBase").IntegerValue;
            int hoverMental = hoverInstance.GetProperty("Pullups").IntegerValue + GlobalVariables.GetVariable("PullupsBase").IntegerValue;
    
            Debug.Log("Hover sophistication: " + hoverInstance.GetProperty("Sophistication").IntegerValue + ", Base: " + GlobalVariables.GetVariable("SophisticationBase").IntegerValue + ", Total before containers: " + hoverSophistication);
            //Debug.Log("Hover Street: " + hoverInstance.GetProperty("Cunning").IntegerValue + ", Base: " + GlobalVariables.GetVariable("CunningBase").IntegerValue + ", Total before containers: " + hoverStreet);
    
            Container[] containers = FindObjectsOfType<Container>();
    
            foreach (Container container in containers)
            {
    
                // Check if the container name matches any of the expected names
                bool isExpectedName = false;
                foreach (string expectedName in expectedContainerNames)
                {
                    if (container.name.Equals(expectedName))
                    {
                        isExpectedName = true;
                        break;
                    }
                }
    
                // If the name doesn't match any expected name, skip processing this container
                if (!isExpectedName) continue;
    
    
                InvInstance invInstance = container.InvCollection.GetInstanceAtIndex(0);
    
    
                // Ignore if no item in Container
                if (!InvInstance.IsValid(invInstance)) continue;
    
                // Ignore if "Type" property matches the hover item
                if (invInstance.GetProperty("Type").TextValue == hoverInstance.GetProperty("Type").TextValue) continue;
    
                // Add the bonus value of each property to the respective total value
                hoverSoft += invInstance.GetProperty("Maturity").IntegerValue;
                hoverSophistication += invInstance.GetProperty("Sophistication").IntegerValue;
                hoverStreet += invInstance.GetProperty("Cunning").IntegerValue;
                hoverMental += invInstance.GetProperty("Pullups").IntegerValue;
                Debug.Log("Container " + containers + " found with sophistication: " + invInstance.GetProperty("Sophistication").IntegerValue + ", New total: " + hoverSophistication, container);
                //Debug.Log("Container " + containers + " found with street: " + invInstance.GetProperty("Cunning").IntegerValue + ", New total: " + hoverStreet, container);
            }
    
            Debug.Log("Final total: " + hoverSophistication);
            //Debug.Log("Final total: " + hoverStreet);
            GlobalVariables.GetVariable("HoverSoft").IntegerValue = hoverSoft;
            GlobalVariables.GetVariable("HoverSophistication").IntegerValue = hoverSophistication;
            GlobalVariables.GetVariable("HoverStreet").IntegerValue = hoverStreet;
            GlobalVariables.GetVariable("HoverMental").IntegerValue = hoverMental;
    
        }
    }
    

    Edit #123123:
    Ok, it's still not working, but now I realized why it was so messed up before; there was indeed another container in the room, that was discontinued literally years ago when I changed how the wardrobe works. I noticed this by checking which containers are being processed by the script.

    This log also tells me that after the point where the script only processes the containers on that list, the HeadContainer is no longer included. Inexplicably.

  • Last update from me, and sorry about the clutter:
    When I remove the name requirement, everything works as intended. There shouldn't be any containers storing garments with properties in the game, so for now I'm considering this solved!
    Thanks for your patience :)

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.