- Home /
Stuck when using inheritance(C#)
I am not sure why but I always struggle with figuring this basic scripting piece out. I am trying to use a Parent and Child class to make my code cleaner and easier to read and work with. So I set a base class with Getters and Setters:
using UnityEngine;
using System.Collections;
public class BaseSkill : MonoBehaviour
{
//These are the different aspects of a skill
private string _name;
private int _maxPoints;
private int _curPoints;
private string _description;
#region Getters and Setters
public string Name
{
get{return _name;}
set{_name = value;}
}
public string MaxPoints
{
get{return _maxPoints;}
set{_maxPoints = value;}
}
public string CurPoints
{
get{return _curPoints;}
set{_curPoints = value;}
}
public string Description
{
get{return _description;}
set{_description = value;}
}
#endregion
}
And then I inherit from it:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// This script is attached to the multiplayerManager and keeps track of what skills the player has available in a list.
/// Skills are kept track of by a point system. Each player has a certain amount of skill points to spend. If they spend a point
/// in a certain skill they gain the function of that skill.
/// </summary>
///
public class PlayerSkills : BaseSkill
{
//Skill points player can spend
public int skillPoints;
//List of Player Skills here
public List<BaseSkill> MySkills = new List<BaseSkill>();
//Create the first skill
BaseSkill SwiftFeet = new BaseSkill();
SwiftFeet.Name = "SwiftFeet";
}
But.. On the line SwiftFeet.Name = "SwiftFeet"; I get this error:
error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
What am I doing wrong and how can I set those base variables through my getters and setters for my newly created skill? As you can probably see I am trying to create skills that all have similar attibutes, a name, description, maxPoints, etc. I need to set these variables for each skill and then I want to add these into a list for the player. Thanks in advance.
G'day madmike, I hope this worked out for you (and I know it was a week or two ago you asked), but reading your code re$$anonymous$$ded me of a tutorial I saw briefly a while ago. He was approaching the problem the same way, this guy treated each "BaseSkill" as a single skill. These base skills were then added to another class called "PC", as a list there.
This might be what you are after if you are looking for information on this topic: http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial at the very least it should give you a heap of pointers for other components of your game too.
Thanks. Yeah those tutorials are great I check them out frequently to get ideas. Thanks for the reply!
Personal point of view but this:
public string $$anonymous$$axPoints
{
get{return _maxPoints;}
set{_maxPoints = value;}
}
seems pretty useless to me as it does not do anything better than a simple public variable.
This on the other hand does something:
public string $$anonymous$$axPoints
{
get{return _maxPoints;}
set{
if(value < int.$$anonymous$$axValue && value > -1)
_maxPoints = value;}
}
As it checks if the value will not do anything wrong and hence legitimate the setter.
Now, if there is any underlying reason as why to use properties when nothing else than get;set; is done please enlighten me as I really do not see the point of it.
Like you mentioned fafase, there isn't really a need to do it if you are not modifying the getter and setter, but I went through a similar process earlier this month (should I, shouldn't I?) and these were my reasons:
Reasons for:
Compliance to the .NET coding standards.
Compliance to your own format standards.
You intend on applying controls around how get/set works in the future.
The reasons against it:
properties do not show automatically in the inspector, you need to create a custom inspector to expose the information.
extra code to maintain.
Compliance to your own format standards.
Reasons that shouldn't factor into the decision:
performance (a few other articles have shown there is very little performance difference due to the JIT compiler) References below.
Because someone else thinks it's wrong, this is along the lines of using underscores in front of private variables.
In the end, I believe it's down to preference.
Tip. If you think it takes up too much room, you could also use this format to cull it down a bit:
public String AutoProperty { get; private set; }
my 2c :)
Edit References: http://stackoverflow.com/questions/646779/does-c-sharp-inline-properties/646780#646780
Edit 2: I went with "don't use them unless constraining or trying to control the setting of fields". Although I did extend the inspector to allow me to set/view properties, I really didn't like how much maintenance the code was taking just to maintain the use of properties.
Rob.
Answer by kru · Jan 23, 2013 at 04:01 AM
You can't assign an instance property outside of a method. Put the line where you set SwiftFeet.Name into the Start() or Awake() method of your script.
Answer by temptest123 · Jan 23, 2013 at 04:10 AM
Are you sure you want to use Inheritance here?
You are aware that your PlayerSkills now IS a BaseSkill (through inheritance) and also HAS BaseSkills (in the MySkills list)?
Hmm that's true. $$anonymous$$aybe I need to rethink this set up. Still learning scripting. Thought this would be cleaner but then you have a point there.
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Inheritance best solution to handle *many* instances of a class with unique values? 1 Answer
[C#] Calling a method of a child class on a parent object 1 Answer
How to serialize fields for both base and derived classes? 0 Answers
Inheritance in C# and Unity3d 1 Answer