- Home /
How to equate (link) 2 different data types?
I've been a bit confused about this for a while now. Say you have a gameobject and a int, and you want that specific game object to be represented as a int id how would you do that?
I'm delving into coding an inventory system. So far I'm going to use a array that has 16 slots. When a player picks up a item which is a gameobject im going to need to id that and put it in the array . But here is the confusion. You cant do gameobject this = int that . So how do you make the gameobject relate to a certain int id? Hopefully someone can clear up this confusion for me. Thanks.
Not sure if this is what your looking for, so I'll comment it. Why don't you create a new array eg:
public GameObject[] inventory = new GameObject[16];
When you add a new gameobject to the array, you will be able to call it using inventory[0], [1], [2], [3] etc. Is that sort of what you were after?
Answer by raybarrera · May 31, 2013 at 05:56 PM
You'd need an alternate way to handle the size of the inventory (easy to do), but I would recommend using a dictionary for this:
http://www.dotnetperls.com/dictionary
Just use the id as the key, and the game object as the value, so you could retrieve the item like so:
myInventoryDictionary[0]
Ok thats rad. any way to see that dictionary in the inspector?
Unfortunately, you can't serialize Dictionaries. But, you can have a list or array, then at runtime (probably in Awake), copy each element into the dictionary for easy access.
Answer by Broheim · May 30, 2013 at 03:53 PM
keeping track of two lists (one for IDs, one for the gameobjects) is messy imo.
You can create a custom class(Item) that stores the ID and the gameobject in one place. then you just have a list of Items which you can cycle through to find corresponding GameObjects and IDs. somewhere in your code there might be something like this:
public Item GetItemByID (int id)
foreach (Item i in myInventory)
{
if (i.id == id)
{
return i;
}
Debug.Log("Warning: Item ID not found:" + id);
return null;
}
public int GetIDbyGO (GameObject go)
foreach (Item i in myInventory)
{
if (i.go == go)
{
return i.id;
}
Debug.Log("Warning: ID not found for gameobject " + go.name );
return -1;
}
I kind of understand that code now. But I can't add a GameObject to the myInventory list. So ill need a gameobject list to hold the items from hit.collider i picked up. That is where I don't understand how that other list will relate to yours.
myInventory would be a list of "Item" (or "IdxGameObject" as posted by Vonni). You don't have any other list referencing your gameobjects. you would only work with this list. if that is not the case you would have to restructure your code to apply this concept.
Ok so this list IS the items? I kind of wanted some kind of representation in the inspector though about it. How would I do that. like i pick up a gun or something then pass it through this list how would i see it in the list in the inspector? Right now it just shows 0 array length so I don't know whats up. I cant tell you right now but i think i tried myInventory.Add(theitem) yesterday and it said 0 in the inspector and didnt show the item.
like jerobinson said: use
public GameObject[] myInventory = new GameObject[16];
and you will see an array in the inspector with length 16 (and each GameObject
in it set to NONE
). If you pick up an item and put it in the list with myInventory[5] = thePickedUpGameObject
in your code it should show up in the inspector. Is it that what you want?
Answer by Vonni · May 30, 2013 at 05:34 PM
// Custom Class Somewhere in your assets
public class IdxGameObject {
public GameObject go;
public int idx;
public IdxGameObject Constructor(GameObject go, int idx){
this.go = go;
this.idx = idx;
}
}
// Some Script
using System.Collections.Generic;
public List<IdxGameObject> myList = new List<IdxGameObject>();
void Start(){
myList.Add(someGameObject, 1);
}
Not sure if this is what you had in mind
Your answer
Follow this Question
Related Questions
How to change the textures of assigned gameobjects? 1 Answer
How to run function in another script with prefabs? C# 2 Answers
How can I pass an instance of a gameobject to another before runtime? 1 Answer
How do I call a function in another gameObject's script? 5 Answers
How to link this 2 variables from 2 different scripts / gameobjects? 1 Answer