0

I'm new to C# started recently and had an idea to make a small text based RPG sort of a game, I am using Visual Studio 2017. I want to have a few different files, including one that marks down all the different types of enemies in the game, how much damage one of their attacks do, their defense and how much health they have. I have this so far:

        string rat;
        int enemyHealth;
        int enemyAtkDmg;
        int enemyDef;

But then I started wondering how I could group "string rat;" and the rest together, so for example:

Rat - 10 Health, 2 Attack Damage, 0 Defense.

Any suggestions on how I could do that? Thank you.

Eric Burdo
  • 812
  • 1
  • 10
  • 24
Anthony P
  • 31
  • 2
  • 9
  • 8
    I recommend look into [classes](https://msdn.microsoft.com/en-us/library/x9afc042.aspx) :) – Gilad Green Apr 03 '17 at 11:12
  • 2
    Please have a look on some basic C# tutorial. It is nice you are starting with side project (it is the quickest way to learn) but gaining some basic knowledge first is a must. – SteppingRazor Apr 03 '17 at 11:13
  • 1
    I would also recommend classes... and take it a bit further than what Sajeetharan recommends (although that is a simple approach). Create a bass class called "mob" or similar. Then sub-class that with your different Types of mobs (rats, spiders, bandits). Use good names for your properties of the classes, and it will take you a LONG way towards a good organization of your code, making it easier to write and understand. – Eric Burdo Apr 03 '17 at 11:25
  • 1
    What Eric describes is called [Inheritance](https://msdn.microsoft.com/en-us/library/ms173149.aspx). – PJvG Apr 03 '17 at 11:27
  • @anthony did you find the answer – Sajeetharan Apr 04 '17 at 12:13
  • @Sajeetharan Sorry for the late response, I still have not found out how to do the code you suggested below. Do you think you could explain it a little? sorry again. – Anthony P Apr 05 '17 at 02:43
  • @AnthonyP have you looked into classes like [Gilad suggested](http://stackoverflow.com/questions/43183125/grouping-variables-in-c#comment73438863_43183125)? It should provide you with more than enough information on how to get [Sajeetharan's solution](http://stackoverflow.com/a/43183158/2541501) working... – PJvG Apr 05 '17 at 14:41

2 Answers2

2

You can create a class with a Custom name and add these as properties inside

 public class MyClass
    {
        public string rat { get; set; }
        public int enemyHealth { get; set; }
        public int enemyAtkDmg { get; set; }
        public int enemyDef { get; set; }           

    }

then ,

        MyClass myObj = new MyClass();
        myObj.rat = "sample";
        myObj.enemyAtkDmg = 10;
        ....
        etc
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 5
    While this does answer the question, I don't think `string rat` is really what I would expect in what this class is meant to be. I think you're taking this question too literal (yes all stated fields are grouped, but is that really the best solution to the problem?). This class is meant to be some kind of *enemy*, *mob* or *monster*. Sure you can call it `MyClass` and include a `string rat`, but that's not very descriptive and/or useful. Better call the class "Enemy" and the `string` property "name" or remove the `string` property if it's not used. – PJvG Apr 03 '17 at 11:34
1

Additionally you can use structs

A struct type is a value type that is typically used to encapsulate small groups of related variables, such as the coordinates of a rectangle or the characteristics of an item in an inventory

public struct Rat
{
    public int enemyHealth;
    public int enemyAtkDmg;
    public int enemyDef
}

Here is a "discussion" about structs vs classes

Community
  • 1
  • 1
Ello
  • 907
  • 1
  • 15
  • 33
  • Structs are a PITA if you need to modify the values and I somehow feel that an enemy/rat will change it's position/health/damage/etc. A struct is _probably_ not what the OP needs. – default Apr 03 '17 at 11:39
  • 1
    structs would be problematic if working with events due to its inmutable nature. Not a good suggestion in this case. – Cleptus Apr 03 '17 at 11:40
  • 1
    well, according to the question he assigned the values initially once, like a config so immutable is no problem here but yes, classes would be better, that's why there's an "additionally" since he is still learning and want to to know maybe about both – Ello Apr 03 '17 at 11:52