Hey all!
I’m having a problem getting my head around how I might assign a unique ID to an item for my inventory system.
There’s a lot of other stuff that’s completely irrelevant to what I need help with so I’ll make it as short as I can.
I need to check a List of integers (List inventoryList) for the integers that are present and add a new one to the list that isn’t already in there. It must be unique ie. no doubles.
Im struggling to get my head around how I might check each item in the list and add a new integer to the list in a sensible fashion. By that I mean a new integer that isn’t ridiculously high or even a negative integer.
My first thought was to make a foreach loop and find the lowest number (say ‘n’ for example) and create the new number at ‘n-1’ but I would have to make sure that ‘!n-1 =< 0’ etc. Then if it is 0 i would have to reverse the whole process and find the highest number- and that’s where I realise I am way over complicating it.
How would I find the lowest unused number in a List of integers?
Without just assigning it a higher value than the last that was added (obviously items in the inventory will be destroyed and removed from the list leaving unassigned numbers.)
I might just blow my unfocused and inexperienced brain out of my skull if I struggle with this much longer.
Cheers!
Halbera.
EDIT: Quick thought, would starting a foreach check at 0 work? If the current number is not found on the list then add it?
public void AddItem(int _baseID){
//assign a unique ID
int _lowest = 1;
int _newID;
foreach (int _listItem in inventoryList){
if (_listItem != _lowest){
_newID = _lowest;
}
else {
_lowest =+ 1;
}
}
inventoryList.Add (_newID);
}