- Home /
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
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; } }
}
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?
also that would only be the case with some specific weapons not all weapons
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.
[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.
}
}
[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();
}
Your answer
Follow this Question
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