Lesson 10.1 – Creating collections of objects

Lesson Objectives

At the end of this lesson, you will know…

  • How to store groups of objects in one property or variable, and read them later.

 

How to handle groups of objects

You may have noticed that there are properties missing from some of our classes. When outlining the program, we mentioned that the player will be able to have items in their inventory. The player will have quests. Quests have items that need to be turned in, to complete them. Monsters have items that can be looted from them.

We have a Quest class, and an Item class, but we don’t have any place to store them in the Player or Monster classes. We also need a way to store more than one item, since a Player will probably have more than one item in their inventory and could have more than one quest.

We do this with lists, or collections.

But before we create these list properties, we’re going to need a place to store some additional information for the items and quests.

For example, with the items in the Player’s inventory, we need to store the quantity of each items they have. It’s similar to what we’ll need to do with the Quest class – store the quantity of each item that is needed to complete the quest.

For the player’s quests, we also need a place to record whether or not the player has completed the quest.

In the Monster class, we want a list of the items that can be “looted” from the monster. This is called the “loot table”. We need to store the item, the percentage of times it is “dropped”, and if the item is a default loot item – in case the random number generator says that none of the loot items were dropped.

There are different ways to do this, but I’m going to show you a way that uses some additional classes.

So, let’s create these new classes, then add them as “list” properties to the existing Player, Monster, and Quest classes.

 

NOTE: In the video, the Quest class is missing the RewardItem property from the last lesson (shown at 6:52). You should keep that property in your class.

 


Link to video on YouTube

 

Step 1: Start Visual Studio Express 2013 for Desktop, and open the solution.

 

Step 2: Create these four new classes by right-clicking on the Engine project and selecting “Add”, then “Class…”. The four classes to create are named:

 

InventoryItem.cs

 

LootItem.cs

 

PlayerQuest.cs

 

QuestCompletionItem.cs

 

NOTE: We use a new datatype for two of these classes – “bool”. This stands for “Boolean” and is used to store a value of “true” or “false”. For example, in the PlayerQuest class, the “IsCompleted” property will store a value of “false”, until the player finishes the quest. Then we’ll store “true” in it.

 

Step 3: Now that we have the new classes, we can create properties for them in the Player, Quest, and Monster classes.

Edit the Player class, by double-clicking on it in the Engine project.

Look at line two of the Player class. In order to use lists, one variable or property to hold a collection of objects that are the same class, we need to have this line included. This is where your program will find everything it needs, in order to work with collections.

Add these two properties to the Player class:

Now you have two new properties that can hold lists containing InventoryItem and PlayerQuest objects.

Then, in the constructor code, add these new lines:

These two lines set the value of the new properties to empty lists. If we didn’t do this, the value for those properties would be “null” – nothing. By setting them to an empty list, we can add items to them later, because you can add objects to an empty list, but you can’t add object to nothing (null).

 

Player.cs (complete, with changes)

 

 

Step 4: Edit the Quest class. Add this new property:

In the constructor, add this new line, so the QuestCompletionItems list will be ready to have objects added to it:

 

Quest.cs (complete, with changes)

 

 

Step 5: Edit the Monster class, by adding this property:

And add this to the constructor, so the new property isn’t “null”:

NOTE 1: We didn’t need to set the string, integer, and Boolean properties to a default value because those datatypes have a built-in default value. However, Lists are null (non-existent), until you set them to an empty list (a new List object, with no values in it yet).

NOTE 2: We didn’t create new parameters in the constructor to pass in values for these new properties. We could have, but we are going to populate them a little differently – a way that I think is a little easier for someone new to programming.

 

Monster.cs (complete, with changes)

 

 

Summary

Almost every program I’ve written has included some type of list of collection. In the real world, we usually work with several items (e.g., display all the deposits into, and payments from, a bank account).

We’ll show how to add to, and work with, the objects in these lists very soon.

 

Source code for this lesson

Source code on GitHub (new classes)

Source code on GitHub (updated classes)

 

Source code on Dropbox (new classes)

Source code on Dropbox (updated classes)

 

 

Next lesson: Lesson 11.1 – Using a static class

Previous lesson: Lesson 09.1 – Using your classes as datatypes

All lessons: Learn C# by Building a Simple RPG Index

 

55 thoughts on “Lesson 10.1 – Creating collections of objects

  1. Hello

    I’ve started reading your tutorial for a while already and there is only 1 thing i’d have to say with all theses classes appearing, folders are a great thing to organize the project lol.

    Thanks for this awesome tutorial. It have been much easyier to understand how those things work with the details your are giving to us.

    Peace and nice work!

    1. Hello (and thank you),

      Yes, organizing classes is extremely important. Some of the large programs I’ve worked on have over 100 projects, thousands of classes, and over a million lines of source code.

      1. Honestly i’ve been saying that mainly because i didn’t see you talking about it even though there is quite a few classes already.

        Well, i guess it’s a bit of my own fault for not exploring Visual Studio a bit more too lol.

        1. Because this is a small program, that was originally only going to be the code from the first 16 lessons, I didn’t create folders for the classes. For the WPF version, I created the folders that I would normally use.

  2. Hey, just got upto this part of the tutorial but now starting to get errors that I can’t seem to see the problem to. Things Like

    Error CS0051 Inconsistent accessibility: parameter type ‘Item’ is less accessible than method ‘InventoryItem.InventoryItem(Item, int)’ Engine

    and that’s for most of the new classes just made.

    1. Hi Dylan,

      Check your classes and properties, to ensure they have “public”. From that message, I’m guessing that your Item class says “class Item”, instead of “public class Item”.

      Please let me know if that does not fix the errors.

Leave a Reply

Your email address will not be published. Required fields are marked *