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 coolguyjoey · Apr 26, 2020 at 05:53 AM · scripting problemscripting beginnerdesigndesign-patternspolymorphism

Creating one class or using polymorphism?

Hi all,

So I'm trying to make a 2D game where I have 50 unique collectables(items). These collectables are obtained by the player under certain circumstances, ex: defeating monsters, in-game events...etc.

Upon getting these collectables, the icon of it will appear on the "show room" panel, and the status of it becomes "unlocked".

These collectable has no real interactions with the game, they only contains some data like an example below:

  public class Collectable
  {
        public int id {get;set;}
        public string name {get;set;}
        public ItemType type {get;set;}
        public int rarity {get;set;}
        public float bonus {get;set;}
  }

Notice that "bonus" is the bonus given to the player in some in-game events, ex: 0.2 additional chance to obtain Collectable XXX upon defeating monsters.

Now I have two questions:

-Should I create only one class like above then assign the values to it at runtime upon creation, or, create a Collectable base class and then create other 50 classes that inherit from the base class? Or if there's any better way you suggest?

  //Option 1:
  public void CreateCollectables()
  {
           collectablelist.Add(new Collectable(){1,"item1", ItemType.Battle,1,0.1});
           collectablelist.Add(new Collectable(){1,"item2", ItemType.Type1,1,0.1});
           collectablelist.Add(new Collectable(){1,"item3", ItemType.Type2,1,0.1});
           ...
           ...
           collectablelist.Add(new Collectable(){1,"item50", ItemType.Event,1,0.1});
  }
  
  //Option 2:
  public class Collectable1 : Collectable{
          public Collectable1()
         {
                 id = 1; 
                 name= "item1" 
                 ...
          }
  }
  //then
  public void CreateCollectables()
  {
           collectablelist.Add(new Collectable1());
           collectablelist.Add(new Collectable2());
           collectablelist.Add(new Collectable3());
           ...
           ...
           collectablelist.Add(new Collectable50());
  }

-If I were to map a collectable to a slot in the panel, what would be the ideal structure?

  public class Collectable
  {
  ...
  CollectableSlot slot;
  }
  //or?
  public class CollectableSlot
  {
  ...
  Collectable collectable;
  }

Any comments are welcomed, thank you!

Comment
Add comment
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

1 Reply

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

Answer by Hellium · Apr 26, 2020 at 09:13 AM

For your first question, if only the data change (and not the behaviour), then there is absolutely no reason to have multiple classes of Collectables. Collectable is a pure data container, nothing more.

I would advise you to have a constructor for this class and only readonly public variables. (or pure getters)


For your second question, I would clearly go for the 2nd example. Again, your Collectable is a pure data container and should not be aware of how it will be used. You might want to have a 3D model representing the collectable on the floor, have this collectable displayed in an inventory, have a collectable displayed on a reward screen, etc...

Comment
Add comment · Show 2 · 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
avatar image coolguyjoey · Apr 26, 2020 at 03:10 PM 0
Share

Hi Hellium,

Thank you for your answer, great explanation.

Yet I have an additional question. By changing the variables to readonly I can then only initialize them through constructor. But what if I have another variable public bool status, which tells if it's locked or unlocked, should I just keep it as a public property that has both a getter and setter, as I will need to change it as game progresses?

avatar image Hellium coolguyjoey · Apr 26, 2020 at 03:46 PM 0
Share

As I said Collectable is a pure data container. I don't believe it should contain anything related to "how" the data must be used. In your case, you can have another entity responsible for giving the status of the collectible in the appropriate situation.


Here are several examples of what I mean


 public interface ICollectableStatusProvider
 {
     bool IsLocked(Collectable collectable);
 }


 public class CharacterCollectableStatusProvider : ICollectableStatusProvider
 {
     private Character character;
     public CharacterCollectableStatusProvider(Character character)
     {
         this.character = character;
     }
     public bool IsLocked(Collectable collectable)
     {
         if(character.class == "$$anonymous$$age" && collectable.Type == ItemType.Sword)
             return true;
         if(character.class == "Warrior" && collectable.Type == ItemType.Wand)
             return true;
 
          return false;
     }
 }


 public class InventoryCollectableStatusProvider : ICollectableStatusProvider
 {
     private Inventory inventory;
     public InventoryCollectableStatusProvider(Inventory inventory)
     {
          this.inventory = inventory;
     }
     public bool IsLocked(Collectable collectable)
     {
        foreach(Item item in inventory.Items)
        {
            if(item.id == collectable.id)
                return false; 
        }
         return true;
     }
 }


 public class OnGroundCollectableStatusProvider : ICollectableStatusProvider
 {
     public bool IsLocked(Collectable collectable)
     {
        return false;
     }
 }





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

235 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

How would i trigger sound on collision? 1 Answer

Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer

Hide selected object with button 0 Answers

Counting prefabs in screen?,Problem with counting prefabs in screen 1 Answer


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