Forum rules - please read before posting.

sorting inventory according to the property value

Hi Chris,

Is there any easy way that when the inventory menu open, all the items can be sort according to the a property integer assigned?

Comments

  • edited March 2022

    Through script, you can access the items as a List and call its Sort function.

    Items at runtime are stored in an InvInstance class, which has a GetProperty function that can be used to access an Integer property's value:

    KickStarter.runtimeInventory.PlayerInvCollection.InvInstances.Sort (delegate (InvInstance a, InvInstance b) { return a.GetProperty ("MyProperty").IntegerValue.CompareTo (b.GetProperty ("MyProperty").IntegerValue); });
    PlayerMenus.ResetInventoryBoxes ();
    
  • Hi Chris,

    Thanks a lot, I have tried your code.
    But I got the error:
    NullReferenceException: Object reference not set to an instance of an object

    So, can you help? Thanks a lot.

  • Try this:

    KickStarter.runtimeInventory.PlayerInvCollection.InvInstances.Sort (delegate (InvInstance a, InvInstance b)
    { 
        return InvInstance.IsValid (a) && InvInstance.IsValid (b) && a.GetProperty ("MyProperty").IntegerValue.CompareTo (b.GetProperty ("MyProperty").IntegerValue);
    });
    PlayerMenus.ResetInventoryBoxes ();
    
  • Hi Chris,

    Thanks a lot.
    But I got this error:
    error CS0019: Operator '&&' cannot be applied to operands of type 'bool' and 'int'

    Would you please help me again, thanks a lot.

  • edited March 2022

    Do you have Items can be arranged in Menus? checked in the Settings Manager? That would cause a NullReferenceException error in the code snippet on March 21.

    If so, either uncheck that setting or try this:

    List<InvInstance> items = KickStarter.runtimeInventory.PlayerInvCollection.InvInstances;
    for (int i = 0; i < items.Count; i++)
    {
        if (!InvInstance.IsValid (items[i]))
        {
            items.RemoveAt (i);
            i--;
        }
    }
    items.Sort (delegate (InvInstance a, InvInstance b) { return a.GetProperty ("MyProperty").IntegerValue.CompareTo (b.GetProperty ("MyProperty").IntegerValue); });
    PlayerMenus.ResetInventoryBoxes ();
    
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.