Forum rules - please read before posting.

How to read the contents of an item property (through scripting)?

edited May 2016 in Technical Q&A
Hi everyone, just wondering if anyone knows how to manually read the contents on an item property so that it can be saved in a variable and used to display its contents on label? That would also help in using the variables content to do checks (i.e.: if property contents is "Clean" then show clean Icon in the UI properties panel, if content is "dirty" then display dirty icon in the UI properties panel, etc). 

My items will have from 1 to 3 properties (depending on the type), each property will have several states. I'll have a pane where an icon appears to let the player know of the properties of the item (and the current state, again, imagine a property state as dirty, clean, ruined, etc). If an Item doesn't have one of the 3 properties I want the icon for that property to either not appear or to get grayed out. I think I have an idea of how to do it, but I need to know how to get the content of the item's property. I'll try to take a look at the scripting guide but if anyone knows already and can tell me, that would be a lot of help.

Anyways, as usual, any advice would be greatly appreciated.

Comments

  • An item's property can be retreived with the GetProperty function:

    AC.InvItem myItem = AC.KickStarter.runtimeInventory.GetItem (3); // The item with ID = 3
    InvVar myProperty = myItem.GetProperty (2); // The property with ID = 2;
    string valueAsText = myProperty.GetValue ();
  • edited May 2016
    Thanks, I'll give it a try!

    PS: Ah, by the way, how would I get the ID of the currently highlighted item? (to automate the process)
  • Eh... I'm not sure how I'm supposed to use those last two... I tried typecasting an invItem to store them, but if I try to then get the invitem.id into an int variable I get an error... so, how do I store their data into an int to use it? 

    Anyway, this is the function I'm trying to make in my UIItemPropertiesHandler script:

    [SerializeField]
    private int[] MyPropertyIDs;

    string GetMyProperty(int propertyID){
    //get selected item ID
    AC.InvItem myCurrentItem = AC.KickStarter.runtimeInventory.hoverItem;
    //optional method
    //AC.InvItem myCurrentItem = AC.KickStarter.runtimeInventory.selectedItem;

    int myID = myCurrentItem.id;
    Debug.Log ("current ID " + myID.ToString());
    //cast the item 
    AC.InvItem myItem = AC.KickStarter.runtimeInventory.GetItem (myID);
    //get the right property
    InvVar myProperty = myItem.GetProperty (propertyID);
    //get the property content into a string
    PropertyAsText = myProperty.GetValue ();
    //return the property value
    return PropertyAsText;
    }

    void SetItemDescription(string Description){
    AC.GlobalVariables.SetStringValue (DrescriptionVariableID, Description);
    }

    It compiles, but when I run I get the next error:

    NullReferenceException: Object reference not set to an instance of an object
    UIItemPropertiesHandler.GetMyProperty (Int32 propertyID) (at Assets/02 Custom Actions Scripts/General Scripts/UIItemPropertiesHandler.cs:99)
    UIItemPropertiesHandler.Start () (at Assets/02 Custom Actions Scripts/General Scripts/UIItemPropertiesHandler.cs:48)

    -Line 99 is:
    int myID = myCurrentItem.id;
    -Line 48 is (position 0 is the int number 0, my description property ID):
    SetItemDescription (GetMyProperty (MyPropertyIDs[0]));/ 
  • You don't need to call GetItem with "myID" once you've declared myCurrentItem - you can skip that to just call myCurrentItem.GetProperty.

    Also be aware that hoverItem (and so also myCurrentItem) will not always be filled, so you must also do a null check for it.  If you don't want to return an empty string when this is the case, you can instead store the string whenever it's non-empty:

    private string PropertyAsText;

    string GetMyProperty (int propertyID)
    {
        //get selected item ID
        AC.InvItem myCurrentItem = AC.KickStarter.runtimeInventory.hoverItem;
       
        if (myCurrentItem != null)
        {
            //get the right property
            InvVar myProperty = myCurrentItem.GetProperty (propertyID);

            //get the property content into a string
            PropertyAsText = myProperty.GetValue ();
        }
       
        //return the property value
        return PropertyAsText;
    }

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.