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 /
  • Help Room /
avatar image
0
Question by Kageyashx · Sep 28, 2017 at 06:32 PM · c#unity 5subclass

Check if Class is specific enum and change object to child?

Hello, im working on an inventory system since a few weeks but ive got a problem. Im working with a static Array as my "inventory", adding,deleting etc works but now i want to make my items useable.

Item mainclass

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ItemMAIN : ScriptableObject {
     public int ItemID;
     public string itemName;
     public string itemDesc;
     private int itemAmount;
     public Sprite itemSprite;
     public enum ItemType{Consumable,Equipment};
     public ItemType itemtype;
 
 
 
     public ItemType getItemType(){
         return itemtype;
     }
 }
 

Item subclasses

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ConsumableItem : ItemMAIN {
     public enum PotionType{HP,MANA};
     public PotionType potionType;
     public float regAmount;
 
 
 
     public void heal(){
         if (potionType == PotionType.HP) {
             Playerstats.hp = Playerstats.hp * regAmount;
 
 
         } else if (potionType == PotionType.MANA) {
             Playerstats.mp = Playerstats.mp * regAmount;
         } else {
             return;
         }
     }

 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EQItem : ItemMAIN {
 
     public enum EQType{WEAPON,RING,NECKLACE,BLESSING};
     public EQType eqType;
     public float attackBonus;
 
 }
 



Class to create item based on a int(itemID)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ItemDatabase {
 
     static private List<ItemMAIN> _items;
     static private bool _isDatabaseLoaded = false;
 
     static private void ValidateDatabase(){
         if (_items == null) {
             _items = new List<ItemMAIN> ();
         }
         if (!_isDatabaseLoaded) {
             LoadDatabase ();
         }
     }
     static public void LoadDatabase(){
         if (_isDatabaseLoaded) {
             return;
         }
         _isDatabaseLoaded = true;
         LoadDatabaseForce ();
     }
     static public void LoadDatabaseForce(){
         ValidateDatabase ();
         ItemMAIN[] resources = Resources.LoadAll<ItemMAIN> (@"Items");
         foreach (ItemMAIN item in resources) {
             if (!_items.Contains (item)) {
                 _items.Add (item);
             }
         }
     }
     static public void ClearDatabase(){
         _isDatabaseLoaded = false;
         _items.Clear ();
     }
     static public ItemMAIN GetItem(int id){
         ValidateDatabase ();
         foreach (ItemMAIN item in _items) {
             if (item.ItemID == id) {
                 return ScriptableObject.Instantiate(item) as ItemMAIN;
             }
         }
         return null;
     }
 }
 

Inventory array: public static ItemMAIN[] playerInventory;

the lenght of the array is fixed later in the code. Adding/deleting works fine,a Object of the type EQItem or ConsumableItem will be added to the inventoryArray anyway i want to make it useable.

 public void UseItem(int itemID){
 
         ItemMAIN item = ItemDatabase.GetItem (itemID);
 
 
     }

Atm thats my method to use the item, i gonna create a new item based on the itemID. Lets say i wanna use a HealthPotion, i wanna do something like that :

  • check if the item is Consumable or EQ

  • check if the enum PotionType is HP or Mana

  • use void health Does anyone can tell me how to "check" those things?

Comment
Add comment · Show 1
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 MaxGuernseyIII · Sep 29, 2017 at 01:33 AM 0
Share

Thank you for posting all your code so we have some of the necessary context. The last little bit could use an edit. What you want to know is not clear to me (although I have some guesses). If you could spend a little more time beefing up the question part of your question, you might get more answers.

Giving a few examples of what you want (from the player's perspective) can help. An useful example includes the following:

  • The situation before the player does something.

  • What the player does.

  • What the situation should be after the player does something.

Also, if what you are really doing is trying to start a design discussion, you might want to consider the forums, ins$$anonymous$$d.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MaxGuernseyIII · Sep 29, 2017 at 01:51 AM

I think what you are asking about is how do you make an item work the way you want it to work. Like, how do you make a potion give someone some health and then go away (for instance).

I think you've taken some good steps and I think there are some more steps you can take.

In short, what you want is called "polymorphism" that is, you have a heterogeneous population of items which each have the potential to do different things.

You've implemented polymorphism using enum type codes. At least you've done this in the case of potions and, presumably, you plan to do this with the more durable items as well.

There are issues with this kind of polymorphism and an alternative exists in the form of inheritance.

This leads us into a deep conversation that, frankly, probably won't even fit in unity answers. As I said in my comment, you could create a forum thread. You can also look up some stuff. I'll give you some search phrases in a second but, first, I want to direct your attention to a super-simple, super-valuable technique for design around problems like this.

It's called "Commonality-Variability Analysis" (CVA) and, like the other topics I'm about to suggest, you can look it up in detail. Unlike those topics, I can also describe it in a kind of cursory sense, right here.

CVA works like this:

  1. Start with your domain (your game).

  2. Brainstorm as many of your requirements as you can (e.g., "when I use an hp potion, I get healed by its amount") - don't worry about organizing them, yet...just get them written down somewhere.

  3. Go through all your requirements and look for requirements that represent things of a like kind (e.g., hp potions go away when used, mp potions go away when used, and daggers go away when thrown) and organize the requirements into those groups.

  4. For each group, analyze what it is, exactly, that they all have in common (e.g., everything that can be pulled out of a chest or dropped has "item-ness").

  5. Use those descriptions as the headings for each group.

When you are done, you have an organized list where each heading is the semantic name of a commonality and each item underneath a heading is a variation of that commonality. This can (but doesn't have to) translate directly into a class hierarchy:

  • Each commonality can correlate with a base class that has all the stuff that is the same for every variant, especially the interface elements which are required (e.g., all items have the ability to use, and some need to be able to remove themselves from inventory, so there needs to be an interface that supports that).

  • Each variability can correspond to an inheritor of the commonality's base class, defining only what makes that variant special. (e.g., consumables do something, then destroy themselves).

Once you have that, I believe it will be apparent to you how to make the things you mentioned in your bulleted-list at the end of the first version of your question.

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

464 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 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 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 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 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 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

My game script is fine but I'm still getting unexpected class error 0 Answers

Survival Shooter Error CS0120 1 Answer

How to make a 2D array of buttons? 2 Answers

ios app crashes on start 0 Answers

The referenced script on the behaviour is missing error help plz 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