- Home /
Inventory and scriptable object items - please point me in the right direction
Quick question (at least, I hope it's quick):
This is my first time using scriptable objects, so I'm new to this stuff.
I have a BaseItem : ScriptableObject script.
Then I have a Weapons: BaseItem script, a QuestItems : BaseItem script, etc., all with BaseItem info and also some unique information.
Now, when populating the inventory, I've watched video upon video. I like the one presented in Unity's Adventure Game Tutorial, but it's set up for only populating the inventory list with the BaseItem. How do I get a master inventory list controller that'll add any kind of item? Do I need to go a whole different route? (If so, please point me in the right direction.) The simplified inventory from the Adventure Game tutorial that I was trying to adapt: (note: using C#)
public class Inventory: MonoBehaviour {
public const int numItemSlots = 4;
public Image[] itemImages = new Image[numItemSlots];
public BaseItem[] items = new BaseItem[numItemSlots];
public void AddItem(BaseItem itemToAdd)
{
for(int i = 0; i < items.Length; i++)
{
if(items[i] == null)
{
items[i] = itemToAdd;
itemImages[i].sprite = itemToAdd.sprite;
itemImages[i].enabled = true;
return;
}
}
}
@$$anonymous$$omcat if you already have BaseItem, Weapons and QuestItems, did you actually already try to at least drag for example QuestItem asset files to BaseItem List / Array ? :)
So you can do that, but you might / or might not like how they serialize when you write them to your save file. There are several answers by @Bunny83 on this topic.
So - I'm learning this stuff myself pretty much... but this is what I mean. Ins$$anonymous$$d of Apples and Oranges (data, their fields) you'll get this, if you push your inventory to Json, then to file:
{
"items": [
{
"instanceID": -54406
},
{
"instanceID": -59642
}
]
}
First one was Orange, the second one is Apple. So they get saved ask references to assets.
Yeah, so, when I add a QuestItem, Weapon, etc, to the array, that's fine, but I can't access the specific info for that type. In other words, I can access the baseitem variables, sure, but not the weapon-specific / quest-specific variables.
I looked up @bunny83 per your suggestion but he's made so many posts... Do you know which ones I should look at? I'm more than happy to read. And... Grumble grumble ... I was hoping to avoid learning json but maybe I should.... Your post makes it look pretty easy. :)
Thank you.
@$$anonymous$$omcat - You can do something like this - although this is probably another question and I'm not best to answer this, as I'm pretty noob myself:
void Start ()
{
foreach (var item in itemContainer.items)
{
Debug.Log("item type:" + item.GetType());
if (item is Apple)
{
var apple = item as Apple;
Debug.Log("item as apple:" + apple.ToString());
Debug.Log("some apple field:" + apple.roundness);
}
if (item is Orange)
{
var orange = item as Orange;
Debug.Log("item as orange:" + orange.ToString());
Debug.Log("some orange field:" + orange.weight);
}
}
}
Note that I just used different methods to print out stuff without much sense, but you can see that you can figure out the type of items, process them as their specific type object, even though they are stored in base class list.
Answer by Momcat · Nov 09, 2018 at 07:25 PM
I forgot to accept the answer, so I'll try to do that now.
Turns out the principle I needed to learn about was "casting," which is what eses was trying to point me towards when he did "var apple = item as Apple;" It took me a little while to figure out but you can save your item into an array of AllItems, then "cast" it back out as whatever it is, ie, item as weapon, item as armor, etc., and all the relevant data will be preserved! Pretty cool. I ended up learning about Interfaces too and used an Interface to implement it. Really really cool stuff to learn. Thank you eses for giving the right answer even though it took me longer to figure out what you were talking about! Cheers, I'm going to try to mark this as answered. :)
Your answer
Follow this Question
Related Questions
Inventory with Upgradable Items and Varying Stats 2 Answers
Inventory armor wielding proplem,How to convert from derived to base 1 Answer
Need some advice for my inventory system 0 Answers
Modifyable item system vs ScriptableObject 0 Answers
I need to find inventory and item system, and to save them 0 Answers