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 MostNimbus · Feb 21, 2017 at 10:33 AM · scriptableobjectinventoryinheritanceclassesitem

Item database with override functions

Hi. What is the best way of creatin item database, which supports "Individual items"?

For example, I was using scriptable object for quite time. You can create an item struct and change values of them. But what if item has not only different stats, but also different function?

For example:

 public class ItemConsumable{
     public string name;
     public virtual void consume()
     {
         Debug.Log("You have consumed:" + name);
     }
 }
 
 
 //Alchogol
 public class alchogol : ItemConsumable
 {
     public alchogol()
     {
         name = "Alchogol";
     }
 
     public override void consume()
     {
         base.consume();
         Debug.Log("You go drunk");
     }
 }
 

As you can see, the alchogol class overrides ItemConsumable's "consume" function. What is the best way to "Store" items of that type? Can theese classes somehow be added to an array of classes? Thanks.

Comment
Add comment · Show 8
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 RobAnthem · Feb 21, 2017 at 11:47 AM 1
Share

Derive all your item types from your base class, but remember you are misusing OOP if you create a separate class for each item, ins$$anonymous$$d create re-usable item types that inherit from your master type. If you need to define small amounts of extra data, it may be better to create an enum, like so

 public enum ConsumableType
 {
     Potions,
     Alcohol,
     StrongAlcohol,
     Water
 }

Afterwards you can store any number of items in arrays, lists, etc, based solely on the base class. For example:

 public ConsumableType consumableType;
 public List<ItemConsumable> items;
 public int effectLevel;
 public void AddItem(ItemConsumable item)
 {
     items.Add(item);
 }
 
 // THIS IS AN EXA$$anonymous$$PLE, NOT A SUGGESTED USAGE
 public void AddAlcohol()
 {
     AddItem(new Alcohol());
 }

And for the enum example

      public override void consume()
      {
          base.consume();
          if (consumableType == ConsumableType.Alcohol)
          {
                Debug.Log("Drank alcohol");
                someClass.DrunkEffect(effectLevel);
          }
          else if (consumableType == ConsumableType.Potion)
          {
                Debug.Log("Drank Potion");
                someClass.HealEffect(effectLevel);
          }
      }


avatar image MostNimbus RobAnthem · Feb 21, 2017 at 07:05 PM 0
Share

But what if there are a lot of individual objects, and each of them has an override for base function? Creating that much enums would be such a mess.

I assume that it can be much easier with "itemStorage[n].use" or so, but is it possible to somehow add all of the classes declared into some sort of an array - the itemDatabase?

avatar image Bunny83 · Feb 21, 2017 at 08:12 PM 0
Share

Define what you mean by "store". If you want to store and use them at runtime in a generic manner the answer is "Yes". If you mean by "store" to store them on disk / inside a savegame file it gets way more complicated. The point of inheritance is to provide a general interface for all derived objects and each object might implement it's own logic.

If you have a List or array of type "ItemConsumable" you can store any derived object in that array and you can use all the methods that the base class provides. So in case of your "consume" method it would work just fine.

 ItemConsumable[] itemArray;
 
 itemArray[1].consume();

Here we use the general interface that the base class provides. We don't have to know what actual class is stored at index 1. So based on what object is stored there you might execute different code automatically. That's why we call it polymorphism

avatar image MostNimbus Bunny83 · Feb 21, 2017 at 08:44 PM 0
Share

Yes. I mean storing them as classes for further usage. For example

 public class $$anonymous$$agical Chest : $$anonymous$$onoBehaviour
 
 {
     GameObject preciousLoot = Instantiate(**ItemGameobject, take a look below**)
     itemObject = preciousLoot.addComponent<ItemObject>();
     itemObject.itemReference = **This one! That "Individual" item class**;
 }


Now, the item object:

 public class itemObject : $$anonymous$$onoBehaviour
 {
     public itemClass item;
 
     public void onUse(Gameobject Consumer)
     {
         item.consume(Consumer);
     }
 }

Now you can see what am I trying to reach. The "base" is nothing but a empty gameobject, which contains only blank functions that are redirected to functions of a referenced item. If you call "Use" on this - it will warn the "itemClass" variable (Which can be a special item that I mentioned in my previous posts) and then, in the class of that object magic happens.

$$anonymous$$AIN PROBLE$$anonymous$$. THE REASON WHY I HAVE CREATED THE TOPIC:

The main problem is - I don't know how to GET the classes from item classes script and put them into itemClass variable.

avatar image RobAnthem MostNimbus · Feb 21, 2017 at 08:54 PM 1
Share

Short answer? You have to write editor extensions for creating and editing data. Really writing editor extensions should be common practice for all Unity devs. The editor is by nature, limited to specific things, it is up to us to extend its capabilities to fit our needs.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

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

Related Questions

Problem accessing enum from other class 1 Answer

Inventory Management Issue 2 Answers

A question about creating items using scriptable objects 1 Answer

Managing Item Types 1 Answer

About Classes in js 2 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