Lesson Objectives
At the end of this lesson, you will know…
- Nothing new. However, you do need to create the rest of the classes, so you can eventually play the game.
Time to add more classes
Now we can create the rest of the classes you’ll use in the game.
By the way, some programmers would call these classes the “business classes”, or “business objects”. Even though this is a game, and not a business application, that’s a term you might hear.
Step 1: Start Visual Studio Express 2013 for Desktop, and open the solution.
Step 2: Right-click on the “Engine” project, in Solution Explorer, then select “Add”, and then “Class”.
Enter the name of the new class to create, and add the properties in the class – just like you did with the Player class.
Copy the code below, to ensure the spelling and casing if the classes and properties is correct. That will be important when we start using the classes – because C# is case-sensitive.
It may not be obvious what all the properties will be used for, but I’ll discuss them when we use them in the future lessons.
HealingPotion.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Engine { public class HealingPotion { public int ID { get; set; } public string Name { get; set; } public string NamePlural { get; set; } public int AmountToHeal { get; set; } } }
Item.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Engine { public class Item { public int ID { get; set; } public string Name { get; set; } public string NamePlural { get; set; } } }
Location.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Engine { public class Location { public int ID { get; set; } public string Name { get; set; } public string Description { get; set; } } }
Monster.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Engine { public class Monster { public int ID { get; set; } public string Name { get; set; } public int MaximumHitPoints { get; set; } public int CurrentHitPoints { get; set; } public int MaximumDamage { get; set; } public int RewardExperiencePoints { get; set; } public int RewardGold { get; set; } } }
Quest.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Engine { public class Quest { public int ID { get; set; } public string Name { get; set; } public string Description { get; set; } public int RewardExperiencePoints { get; set; } public int RewardGold { get; set; } } }
Weapon.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Engine { public class Weapon { public int ID { get; set; } public string Name { get; set; } public string NamePlural { get; set; } public int MinimumDamage { get; set; } public int MaximumDamage { get; set; } } }
Summary
There wasn’t really anything new in this lesson. We just need to add these classes to make the game actually do something.
Source code for this lesson
Next Lesson: Lesson 07.1 – Inheritance and base classes
Previous lesson: Lesson 05.1 – Creating objects from classes
All lessons: Learn C# by Building a Simple RPG Index
Is it possible to provide a list of the properties of the 7 new classes as part of the tutorial? Some of us accessing this great tutorial in totalitarian states do not have the luxury of downloading files from github 🙁 Thanks a bunch!
Hello Ken,
Here’s a DropBox location with the files: DropBox – Lesson 06.1
I’m going to see if I can either install a Git repository on this site, or find second place to put the files on GitHub. Some of the files get much bigger, so adding them all inside the lesson might not work well. I might not be able to copy everything over to a new location until this weekend, but I will let you know when thethey are ready.
Please let me know if you cannot access DropBox, or if you know a better file-sharing location you can access.
All the lessons that have code on GitHub should now have links to the same files on DropBox. Please let me know if there is any problem with those files, or with accessing them. Thanks!
Hi! Sorry i was just going to ask in case i’ve missed something but where we supposed to come up with these ourselves or copy your code? Thanks! The tutorial has been great!
Hi Will. You can get the source code for these classes from the GitHub or Dropbox links at the bottom of the lesson (or, just use them from here: classes on GitHub or classes on Dropbox). Create each class by right-clicking on the project name, then “Add”, then “Class”. Then copy/paste the code for the class from GitHub. I just did things that way to keep this page a little cleaner, and not have it full of code.
Thanks!
Hi. i was just whant to understand why does every property in the classes have (public int ID)
what do we use it for
In a future lesson, we are going to create a “World” class, that will hold collections of all the items, monsters, quests, and locations. We need a way to find the exact item/monster/quest/location in those collections, and an ID is the best way to do that. We wouldn’t want to use the name, because you might change the name (for example, if you were writing the game for a different language – like Spanish).
Thanks a ton!!
You’re welcome!
Hey Scott, I noticed in this lesson the Quest class has a public List.
This may need to be added to the list above. It maybe in a later lesson, but I figured that it may throw someone off if they cant get to GitHub.
The QuestCompletionItem.cs from GetHub has this inside:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
public class QuestCompletionItem
{
public Item Details { get; set; }
public int Quantity { get; set; }
public QuestCompletionItem(Item details, int quantity)
{
Details = details;
Quantity = quantity;
}
}
}
Hope this helps!
Hi Simeon,
The QuestCompletionItem class is added in Lesson 10.1 (where we also modify the Quest class, to use the new QuestCompletionItem class).
Hi Scott!
Not sure if you look at this anymore, however I hope you do! 🙂 I am creating the new classes but it seems to be missing
using System.Linq;
using System.Threading.Tasks;
Has this been something I missed or just what the new version has? I’m adding them in for now but not sure!
Thank you!
Hi Daniel,
It sounds like Visual Studio changed the default “using” directives they include when you add a new class. I don’t think any of the classes will need “System.Threading.Tasks”, but some of the classes will need “System.Linq” in the future. It is safest to include them. If they are not needed, it doesn’t hurt to have them.
Hi I need help lol, my program.cs tab has the super adventure show up as a error.. it goes on to say that it’s being used at a namespace?
Can you upload your solution (including the directories under it, and all the files in those directories) to GitHub or Dropbox, so I can look at it?
Hi Scott, I just wanna know why we put {get; set;} after each variable, why can’t we just to do that because we can still change its value. I’m sorry if you answered this before but I have never used get; set; and it’s new.
Why cant we just NOT do that*
You could just do something like:
public string Name;
This is called a “field” in the class. You can use it to store and retrieve values in the object. But, that’s all you can do with the field.
With a property, you add the “get” and “set”. The nice thing with the property is that you can add extra code to the “get” and “set”. For example, in the “set”, you can raise a property changed notification, so the UI knows the value was updated and needs to be refreshed on the screen. Fields can’t do that because they don’t have any setter to add the notification code to.
You can add more code/logic in the setter. For example, in this game, when the player moves to a new location, the CurrentLocation property’s setter will be called. We can add code to the setter to see if there is a monster at the location. If there is, we’ll create a new monster, add the Monster object to the CurrentMonster property, and give the player a monster to fight.
Let me know if that doesn’t explain why we use the properties, or if you have any other questions.
Sorry, I took more than 2 weeks to reply, but what I’m understanding is that the get; set property just widens the usability of a variable by adding more stuff or doing additional stuff to it or in addition to it like a method every time we use that variable. Is that part of what the setter does or am I still on the wrong track?
Thank you,
Hashim
Hi Hasim,
You are correct. Having “get; set;” on the property gives us flexibility to do additional things when storing or retrieving values.