Search for Item in Inventory (Generic List) (Javascript)

Hello!

I am relatively new to scripting so please forgive me if my question is astupid :slight_smile:

I want to search for a specific Item in my Intentory. My inventory Code looks like this:+

Inventory.js

static var inventory: List. = new List. ();

My items look like this:

#Itemclass.js

public class ItemClass {

	var name : 	String;
	var icon :	Texture2D;
	var description: String;
	var ID: int;

}

Now, if I add some Items to my Inventory, how can i remove specific ones?
I thought of finding the Index and then using RemoveAt, however I cannot find a way to find out the Indexnumber. I used IndexOf:

var MoneyIndex: int = Inventory.inventory.IndexOf (“Money”);

However, that gives me an Error. It also makes sense since only the name variable in the Itemclass is called Money and not the Itemclass itself.

Any Ideas anyone?

Unfortunately Answers gobbles content inside the angled brackets < and >, unless you place a space between the brackets and the contents inside it. Therefore it's ok to write List.< T > in Answers but not List. (which is stupid of me to even try and show since it will not show) :)

–

Im sorry, I dont think I understand a word of what you are saying :/ Could you give me a quick example?

–

Check the first two code snippets. It shows what I assume you tried to type, and what actually is shown here on answers. Answers will "corrupt" text that contains < and > symbols, such as List.< ItemClass > unless there is a space between "<" and "ItemClass" and ">".

–

1 Answer

1

I will assume your code looked like this:

static var inventory: List.< ItemClass > = new List.< ItemClass > ();

instead of

static var inventory: List. = new List. ();

IndexOf won’t work, since IndexOf would be trying to find the index of a given instance already in a list (an instance of a complete ItemClass object). You don’t have an instance to test with, you only want to remove objects that have a property set to “Money”, I guess.

One way to do it is with delegates and use RemoveAll. You can also use for loops, or consider switching to associative structures, such as Dictionary.< TKey, TValue >.

#pragma strict

import System.Collections.Generic;

public class ItemClassExample extends MonoBehaviour 
{
    var inventory : List.< ItemClass > = new List.< ItemClass > ();
    
    function Start() {
        inventory.RemoveAll(function(item) item.name == "Money");
    }
}

public class ItemClass 
{
    var name : String;
    var icon : Texture2D;
    var description : String;
    var ID : int;
}

The Mono Upgrade Details contains some examples that show delegates.

Oh, fantastic, that works. Thank you so much :) And your assumption about the setup of the Inventory was correct, somehow the forum ate the missing parts :)

–

As long as you understand what the code does. I don't want to make any assumptions on your skill level so please don't get offended, but please ensure that you know what the code is doing, why it's working, and where you possibly could use delegates elsewhere. Either in other framework code, or in your own. The reason being, I don't think a lot of UnityScript users are aware of delegates. At least I haven't seen them widely used, while they are quite common for those who dabble with C#. :)

–