- Home /
Store multiple types of data in an Array?
Evening everyone! Here's the deal: I'm setting up an inventory system the stores multiple kinds of information on the items that occupy each slot. Each inventory slot needs to house the object's 1) Name, 2) Tag, 3) Prefab Object, 4) Amount, 5) MaxStack.
The way I've set this up for versatility, is that every object that can be 'collected' is a Prefab that includes a script that houses the above information in separate variables, and when the player interacts with the object, I need it to package that info into an Array or List, SendMessage("Inventory", Array/List) to the player's inventory script, and be unpacked by a function in that script to compare the item's info against what's already stored to find the next available space.
SO! In theory, this should work using Javascript Array or an ArrayList, but on unpacking, I repeatedly get Implicit Downcast warnings from type 'Object' to type 'String, int, etc.', even with type casting like: var itemName : String = array[0];
I'm working in UnityScript(variant of Javascript), but translating to/from C# isn't hard, so any help would be appreciated. ^_^
Answer by Clet_ · May 01, 2014 at 03:45 AM
Let's say your class looks like this :
public class MyClass {
public int myInt;
public string myString;
public bool myBool;
}
In your other scripts, you can have :
List<MyClass> list = new List<MyClass>();
When "unpacking", it should look like this :
MyClass unpackedItem = list[0];
Debug.Log(unpackedItem.myString);
Found a series of Unity Tutorials on Advance Inventory systems, and they're helping so far. Seems the issue I had was I didn't create the class correctly, as in I didn't make the new class it's own script. I though it could just be declared in one. ^_^;
I'll test this out (with the class made appropriately this time) and if it works out, this'll be the answer I needed.
The new class is definitely the solution I needed. Just need to be certain to mention that creating a new class in JS requires you to make a new script that 'is' that class, named the same and everything, and it's variables are defined in that script.
I don't know in JS, but in C#, you can declare non-$$anonymous$$onoBehaviour script anywhere you want.
In this particular case of an inventory, you definitely don't need to inherit from $$anonymous$$onoBehaviour
to those who need to use this at inspector:
use [System.Serializable]
for $$anonymous$$yClass
or some way similar to this way:
https://answers.unity.com/questions/324167/builtin-array-of-different-data-types-to-inspector.html
according to Loius answer and robertbuof comment:
[System.Serializable]
public class $$anonymous$$ultitype {
public int x;
public string y;
public GameObject z;
}
public $$anonymous$$ultitype[] things;
Answer by 0V3RR1D3 · May 01, 2014 at 03:14 AM
Hey, check this out, Hope it helps!
I've tried a lot of that already with the same general problem. I can put the values in, but getting them out becomes the problem. A Javascript Array or an ArrayList gives me implicit downcast warnings, and a Custom Class array (may have been a bit above my experience level) threw a whole bunch of errors about not supporting splitting when I tried to retrieve information from some of the fields. :\
At the moment I'm using the inventory function as a public one and calling it directly, but I would REALLY like to be able to store and retrieve this data for more compact scripting. Right now every inventory slot has 5 separate variables a piece. @_@;
Answer by HydraOrcUnity · Jun 12, 2021 at 04:20 PM
var data = new object[] { 1, "string", 1.2f, 2 };
This data format is useful for for sending JSON messages, for example using UltimateJson:
Debug.Log(JsonObject.Serialise(data));
Which will result in such array (this approarch generates an object instead of array when serialising classes):
[1,"string",1.2,2]
Also you can store this data in a database like MongoDB
Answer by PhyrePhrost · Jun 12, 2021 at 05:33 PM
you can use simply the type 'object' as an array and pass into it the variables... when you need one of them you can check is original type with GetType()