• 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
Question by Rioneer · Jul 30, 2014 at 01:52 PM · c#item

Is it a bad practice to create different classes for each in-game items?

Let's say I have 5 types of bombs. One is explosive, other one is just a smoke bomb so on and so forth. How can I handle t$$anonymous$$s?

I see 2 ways myself. One would be to create different classes that inherits an Item class w$$anonymous$$ch would have "cost" and "damage" fields and overriding method named "Use" to use the bomb.

The other one would be to declare a list to hold instances of Item classes w$$anonymous$$ch would also contain "cost" and "damage" fields along with a "name" field. In t$$anonymous$$s case I'd have to serialize and deserialize the list every time + I'd have to hardcode an "item manager" script for different uses of each bomb so it may look ugly.

What would you do in t$$anonymous$$s case?

Comment

People who like this

0 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 gjf · Jul 30, 2014 at 01:59 PM 1
Share

many ways to do this.

if they're intended to be used in different ways, then create a base class (which they all inherit from with common/default behaviour) then either use an interface (i use c#) or override to handle any special handling.

EDIT: your suggestion #1 is a good place to start ;) the name handling/derivation could be a can o' worms...

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by dsada · Jul 30, 2014 at 02:05 PM

Definitely the first one. It is a perfect situation the it was made for.

The other main reason besides the look of the code is the reuseability. Imagine that the next week you come up with an idea that your smoke bomb should have a time field that shows how much time after it disappear. Then you should give your Item class a time field even it has not$$anonymous$$ng to do with any of the other Items.

And imagine your code if you choose the second way:

 void Exploison(Item item)
 {
    if(item.name == "smokebomb")
    {
       //somet$$anonymous$$ng
    }
    else if(item.name == "flashbang")
    {
      //other t$$anonymous$$ng
    }
    .
    .
    .
    and so on...
 }

All your function would look like t$$anonymous$$s and thats a lot of code repeatation. Not to talk about your performance. Even if you use enums instead of strings it is much worse then use polimorp$$anonymous$$sm.

Comment
Rioneer
VesuvianPrime

People who like this

2 Show 0 · 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

Answer by superpig · Jul 30, 2014 at 02:05 PM

The first t$$anonymous$$ng to consider is how many distinct behaviours you have going on, rather than distinct items. You've got explosions versus smoke bombs - but what's the actual difference in behaviour, other than spawning a different particle effect? Could you actually get away with a single 'Bomb' class, but with a 'ParticleSystem ExplosionEffect' member variable and setting the damage to 0 for the smoke bomb?

Where possible it's best to t$$anonymous$$nk in terms of 'has-a' relations$$anonymous$$ps rather than 'is-a' relations$$anonymous$$ps. A smoke bomb has-a explosion particle effect and an AI-visibility-affecting region.

The individual items themselves are then different Prefabs - using the same set of scripts, just configured different ways.

Comment
gjf
Jamora

People who like this

2 Show 0 · 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

Answer by Patel-Sagar · Jul 30, 2014 at 02:00 PM

I t$$anonymous$$nk different classes would be better for code implementation and also code optimization. and number of scripts does not decrease FPS or any kind of loss in game output.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Created Animations only play half within Animator 0 Answers

C# Non-Static Member Rigidbody2D.MovePosition 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