Hi everyone, this question is in regards to an inventory system
So for context: You remove a GameObject
from one list and add it to another. I want it to be able to check through the entire list until it finds a value that is null
and in that list to add that GameObject
to that index location.
If needs be I will add a code snippet to this but I would like to know if this is even possible in the first place as I have not found a clear answer regarding this, cheers!
It sounds like you want to use an Array
instead of a List
. List are dynamically sized so when you remove an element it will get smaller. Arrays are fixed size when you remove an element it will leave an empty spot.
Here is some code on on approach to how you could do this. I made some assumptions but hopefully this gives you a rough idea on how to work with arrays.
GameObject m_Belt;
GameObject m_Sword;
GameObject m_Chicken;
GameObject[] m_PlayerOneInventory;
GameObject[] m_PlayerTwoInventory;
void Start()
{
// initialize the arrays, in this case they are both length 3.
m_PlayerOneInventory= new GameObject[] { m_Belt, m_Sword, m_Chicken };
m_PlayerTwoInventory = new GameObject[3];
}
void GiveChicken()
{
int chickenIndex = -1;
// check to see if the player oen invenstory has a chicken, if not the value will stay at -1
for(int i = 0; i < m_PlayerOneInventory.Length; i++)
{
if(m_PlayerOneInventory[i] == m_Chicken)
{
chickenIndex = i;
}
}
// if we found a chicken, now we need to find the first empty spot in player two's inventory
if (chickenIndex != -1)
{
// find the first index that is null
int openInventoryIndex = FindOpenSpotInInventory(m_PlayerOneInventory);
// if we found a null value openInventoryIndex will not be set to -1
if (openInventoryIndex != -1)
{
// set the value of the open spot to the chicken and set the chicken spot to null
m_PlayerTwoInventory[openInventoryIndex] = m_PlayerOneInventory[chickenIndex];
m_PlayerOneInventory[chickenIndex] = null;
}
}
}
int FindOpenSpotInInventory(GameObject[] inventory)
{
for (int i = 0; i < inventory.Length; i++)
{
if (inventory[i] == null)
{
return i;
}
}
return -1;
}
You may find useful some LINQ methods, they are often very powerful, but less performant than using regular loops. However, some of them are fine and can be used for such operations.
Well anyway, You can make separate method to find null object in a list.
Make sure to add using System.Linq;
int GetIndexOfNull(List<GameObject> list)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i] == null)
return i;
}
return 0;
}
List<int> GetIndexesOfNull(List<GameObject> list)
{
List<int> emptySlots = new();
for (int i = 0; i < list.Count; i++)
{
if (list[i] == null)
emptySlots.Add(i);
}
return emptySlots;
}
void InsertToEmptySlot(GameObject item, List<GameObject> second)
{
int indexOfEmpty = GetIndexOfNull(second);
second.Insert(indexOfEmpty, item); // this is where you use linq.
}
// LINQ WAY:
int GetIndexOfNull_LINQ(List<GameObject> list)
{
int index = list.FindIndex(gameObj=> gameObj == null);
return index;
}
First of all, a lot is possible with C# (and most programming languages) and the problem is usually finding easiest and performant way, and often there is no perfect way of achieving something.
Second of all, not sure if you are already doing it, but I would recommend creating separate classes of Item and a Slot. It is quite good because you can just do this:
public class Item
{
int index;
string name;
Sprite icon;
}
public class Slot
{
int index;
Item item;
}
With this you instead check if slot.item == null.