Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Kalbytron · Sep 10, 2014 at 08:37 PM · effectsitemsattribute

Item attributes organization

I was wonder how to implement modular attributes on items, players, and anything else. As some of you guys know Minecraft, there are potions that store different attributes and affect the player when applied.

I want to know how those attributes are written. Are they made by writing a base script and writing multiple derived scripts for every unique attribute? Or is all of the attributes written in one script and, in the inspector, tell what attribute to apply?

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 Kiwasi · Sep 10, 2014 at 10:13 PM 0
Share

This is a design issue and should probably go to the forums. UA is better for specific technical questions.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by VesuvianPrime · Sep 10, 2014 at 10:34 PM

It's a very good question, and probably worth asking somewhere like StackOverflow rather than Unity Answers. Not to say Unity Answers is a bad place, just you're more likely to get programming experts weighing in on the problem: http://stackoverflow.com/questions/1338391/when-to-subclass-instead-of-differentiating-the-behaviour

This is how I separate my concerns:

  1. Write interfaces to figure out what data, inputs, outputs and functionalities I want. I don't need to design my application to be interface driven, they're just nice as a template to work around.

  2. Write an abstract base class that implements my base interface. This class is going to contain most of the functionality.

  3. Write subclasses for the different interfaces. These are going to handle data for the most part.

My approach to Minecraft enchantments would be something like this (Haven't played Minecraft in a while, so I forget exactly what has what kinds of enchantment):

 IEnchantment;
 IEnchantmentWearable : IEnchantment;
 IEnchantmentWeapon : IEnchantment;
 IEnchantmentTool : IEnchantment;
 IEnchantmentHelmet : IEnchantmentWearable; // I think helmet has it's own enchantments like water breathing, but also has armor and unbreaking bonuses
 IEnchantmentBoots : IEnchantmentWearable; // Similarly, boots let you walk in lava?
 // etc

Each of these interfaces would have a bunch of integer values representing the levels of each enchantment:

 public int damageLevel;
 public int unbreakingLevel;
 public int waterBreathingLevel;
 // etc

Finally I'd make my class heirarchy that implements the interfaces. Make a fancy base class that interacts with the Player class somehow. Make sure everthing is serializable and looks nice in Unity inspector.

Sorry for the crazy rambly post. It gets to a point where a lot of programming is gut instinct. Only you can decide when to suclass on a case by case basis.

Comment
Add comment · Show 3 · 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 Kalbytron · Sep 15, 2014 at 05:13 PM 0
Share

Subclasses, like writing on new derived scripts for different attributes?

avatar image VesuvianPrime · Sep 16, 2014 at 11:34 AM 0
Share

Correct. So you could do:

 public abstract class Enchantment : IEnchantment
 {
     public string name;

     // Contains logic for handling enchantment stats
 }

 public class EnchantmentWeapon : Enchantment, IEnchantmentWeapon
 {
     public int damageLevel;
 }

 // etc
avatar image Kalbytron · Sep 23, 2014 at 02:10 AM 0
Share

And IEnchantment is the potions logic like throwing, splash range, and appliance to entities, right? Sorry for the slow response.

avatar image
0

Answer by Cherno · Sep 11, 2014 at 12:27 AM

Just today I started using a XML database for my items. It's very handy once you get your head around it and from mmy limited experience, I can recommend it. You basically have all your items and their values inside a XML file and you read from it whenever you need to fill your Item class instance with values. You can also, like I did, just create a dictionary at the start of the game that has all items stored by name.

Here is a tutorial that will get you started:

Get to grips with XML interactions using the .NET XmlSerializer that can turn your XML data into a class hierarchy with just a few lines of code...

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

26 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

Related Questions

Creating a custom List or Collection 1 Answer

Attaching stats to Gameobjects vs an Item database? 0 Answers

Inventory System: How to detect equal items on a list for increasing amount? 0 Answers

Can I refer to a custom abstract class as a MonoBehaviour property? 1 Answer

Self-assigning field in class 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