Hello all, this is my first time here. Usually I am able to solve my code issues but this one is eluding me.
I have a Generic List which makes up my Player Inventory, this List contains Item Classes; which in turn have have strings I wish to access, ie Item Names.
I am having trouble accessing strings inside the classes using List.Contains.
My Item Class is as follows:
#pragma strict
public class Item_Class
{
public var Item_Name:String;
public var Item_Desc:String;
public var Item_Type:String;
public var Item_Size:String;
public var Item_Icon:Texture2D;
public var Item_Prefab:String;
}
I thought that I would be able to access the Item_Name string by doing something along the lines of:
if (_myinventory.Contains(Item_Class.Item_Name("")))
{
// Do something.
}
I am having no luck, help would be much appreciated. Thanks.
Instead of a List<>, try using a Dictionary, where you assign Values to Keys, then you could search for a named Key. It wont work if all the Values aren’t string though…
I got around this a little to suit my needs by doing the following:
function Update()
{
// Quickbar 1.
if (Input.GetKeyDown("1"))
{
_1KeyRecentlyPressed = true;
for(var a = 0; a < _inventory._playerInventory.Count; a++)
{
if (_inventory._playerInventory[a].Item_Type == "StimPack")
{
_guiStimPackIndex = a;
}
}
}
if (Input.GetKeyUp("1"))
{
if (_1KeyRecentlyPressed && _guiStimPackCount != 0)
{
_playerControllerScript.UseItem("StimPack");
_inventory._playerInventory.RemoveAt(_guiStimPackIndex);
CheckQuickbarQuota();
_1KeyRecentlyPressed = false;
}
}
}
function CheckQuickbarQuota()
{
_guiStimPackCount = 0;
_guiMedicalKitCount = 0;
_guiReconDroneCount = 0;
for(var e = 0; e < _inventory._playerInventory.Count; e++)
{
if (_inventory._playerInventory[e].Item_Type == "StimPack")
{
_guiStimPackCount++;
}
if (_inventory._playerInventory[e].Item_Type == "MedicalKit")
{
_guiMedicalKitCount++;
}
if (_inventory._playerInventory[e].Item_Type == "ReconDrone")
{
_guiReconDroneCount++;
}
}
}