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 TheNinjassin · Jul 25, 2014 at 11:00 PM · settersgetters

Abstract Inheritance and abstract variables sloppy code

Ok, please bare with me. //long winded explanation incoming.

Ok so I'm making an Item system for my role-playing game. I want to use a system of inheritance with abstract classes so that I will be able to easily add the items to lists and only allow specific types of item to fill certain variables (such as only putting armour in the players armour slots and weapons in the players weapon slots and not vice versa). at the moment the inheritance works like this:

 public abstract class Item
 {
     public string Name;
 }
 
 public abstract class Weapon : Item
 {
     public int Strength{get; set;}
     public Weapon(int strength, string name)
     {
         Strength = strength;
         Name = name;
     }
 }
 
 public class GodSlayer : Weapon
 {
     public GodSlayer() : base(10, "God Slayer"){}
 }
 

this works fine for me in terms of functionality but it looks sloppy like i'm mixing up abstract variables and putting them in places I might not need to. I was initially using a getter and setter for the Name variable but couldn't work out the syntax for basing it in the GodSlayer class. However that's working at the moment without needing to be getted and setted so I presume I also don't need it for the Strength variable. basically I also want to know what the purpose of getting and setting is in this context in addition to my other question. I was also earlier using

 public abstract string Name();
 
 and
 
 public override string Name()
 {
     return "God Slayer";
 }

this seemed inefficient since I had to use an override function each time I wanted to assign a value to the variable. What's the difference between these two methods?

sorry for so many questions, just any help clarifying this whole situation and helping me understand the best way to go about this would be very much appreciated.

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 SilentSin · Jul 26, 2014 at 11:09 AM

I wouldn't recommend using separate classes for every item. Just make a class for each item type (weapon, armour, shield, potion etc) and give them constructors to initialise their stats.

 public class Weapon : Item
 {
     public string name;
     public int strength;

     public Weapon(string name, int strength)
     {
         this.name = name;
         this.strength = strength;
     }

     public static readonly Weapon[] sAllThaWeponz = new Weapon[]
     {
         new Weapon("God Slayer", 10),
         new Weapon("Firebrand", 6),
         new Weapon("Wooden Sword", 1),
     };
 }

Now all you have to do to make a new weapon it add another line to the array and change it a bit instead of making a new class.

If you did want to keep making a new class for each new weapon, you could do something like this:

 public abstract class Item
 {
     public abstract string Name { get; }
 }

 public abstract class Weapon : Item
 {
     public abstract int Strength { get; }
 }

 public class GodSlayer : Weapon
 {
     public override string Name { get { return "God Slayer"; } }
     public override int Strength { get { return 10; } }
 }
Comment
Add comment · Show 8 · 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 TheNinjassin · Jul 26, 2014 at 11:33 AM 0
Share

That's cool I'll definitely use the top example, If I wanted to add a function to the weapon though (for example if godslayer has a special smite ability which it imbues to the user) how would I add that using this method?

avatar image TheNinjassin · Jul 26, 2014 at 12:42 PM 0
Share

also that would only be the case with some specific weapons not all weapons

avatar image SilentSin · Jul 26, 2014 at 01:10 PM 0
Share

I'm trying to post a description of how you'd do that, but it isn't letting me. It says I'm still under the character limit, but I click comment and it does nothing.

avatar image SilentSin · Jul 26, 2014 at 01:11 PM 0
Share

[Part 1]

That sort of thing starts making things much more complicated. I wasn't able to find much guidance for that sort of system when I was working on my RPG prototype a while ago, but I'll try to explain what I ended up with.

 public static class Hit
 {
     public static GameObject sAttacker, sDefender;
     public static int sDamage;
     // and whatever other details you want, such as attack direction or damage type

     public static void Set(GameObject attacker, GameObject defender, int damage)
     {
         sAttacker = attacker;
         // etc.
     }
 }
avatar image SilentSin · Jul 26, 2014 at 01:12 PM 0
Share

[Part 2]

Whenever something hits something else, it calls Hit.Set() to set the details of the current attack, then it tells the defender to Defend(), where it takes he damage off its health, flinches, gets knocked back and whatever else happens on a hit.

Then you can give the attacker a delegate to apply additional effects, so in the creature class, ins$$anonymous$$d of just Hit.Set() then Defender.Defend(), you would have something like:

 Defender defender = whateverYouHit.GetComponent<Defender>();
 if(defender != null)
 {
     Hit.Set(gameObject, defender.gameObject, myWeapon.Damage);
     if (myWeapon.hitEffect != null) myWeapon.hitEffect();
     defender.Defend();
 }
Show more comments

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

24 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

Related Questions

Getter and Setter in C# 2 Answers

How can I display an object with a script based off an int in a different script? 1 Answer

Get and Set difference 2 Answers

problems with setters and getters. 1 Answer

getter and setter not working 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