Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Momcat · Sep 02, 2018 at 05:28 AM · scriptableobjectinventory systemitems

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;
             }
         }
     }

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eses · Sep 02, 2018 at 07:38 AM 0
Share

@$$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.

avatar image Momcat · Sep 02, 2018 at 12:06 PM 0
Share

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.

avatar image eses Momcat · Sep 02, 2018 at 02:33 PM 0
Share

@$$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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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. :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges