Lesson 16.1 – Writing the function to move the player

Lesson Objectives

At the end of this lesson, you will know…

  • How to plan a function
  • How to write the code/logic for a function
  • Some of the most common C# commands

 

Ok, we’ve spent all this time building up the pieces we need for our game. Now it’s time to actually get the game to do something. The first thing we’re going to add the code to handle when the player moves to a new location.

By the way, this is a long lesson, and covers a lot of new things. So make sure you have some time to complete it.

Outline of the function

The first thing we need to do is figure out what’s going to happen during a move.

When I write a function that needs to do a lot of things, like this one, I like to do some planning first – so I don’t miss anything.

When the player moves to a new location, we’ll completely heal them, we’ll check to see if the location has any quests (and if the player can complete them), and if there are any monsters to fight there.

Here is an outline of the logic for a player move function. Something like this is often called “pseudo-code”. It isn’t C# code, but it represents what the code will do.

Each indentation level is where we handle a different condition – for example, the steps to follow if the location has a monster, and the steps to follow if the location doesn’t have a monster.

  • If the location has an item required to enter it
    • If the player does not have the item
      • Display message
      • Don’t let the player move here (stop processing the move)
  • Update the player’s current location
    • Display location name and description
    • Show/hide the available movement buttons
  • Completely heal the player (we assume they rested/healed while moving)
    • Update hit points display in UI
  • Does the location have a quest?
    • If so, does the player already have the quest?
      • If so, is the quest already completed?
        • If not, does the player have the items to complete the quest?
          • If so, complete the quest
            • Display messages
            • Remove quest completion items from inventory
            • Give quest rewards
            • Mark player’s quest as completed
      • If not, give the player the quest
        • Display message
        • Add quest to player quest list
  • Is there a monster at the location?
    • If so,
      • Display message
      • Spawn new monster to fight
      • Display combat comboboxes and buttons
        • Repopulate comboboxes, in case inventory changed
    • If not
      • Hide combat comboboxes and buttons
  • Refresh the player’s inventory in the UI – in case it changed
  • Refresh the player’s quest list in the UI – in case it changed
  • Refresh the cboWeapons ComboBox in the UI
  • Refresh the cboPotions ComboBox in the UI

 

Creating a shared function

We have four different functions for movement, one for each direction. And we need to do the steps shown above for moving in each direction.

We could do that by writing the same code in each function. But then, if we ever want to change that logic, we’d need to make the change in four places – and that often leads to mistakes.

So, we’re going to create a new shared “MoveTo” function to handle movement to any location. Each of the four movement functions will call that one new function.

Storing the player’s location

We also need a place to save the player’s current location. Since this value will change, we need to store it in either a variable or a property.

In this case, we’ll make a property in the Player class. It makes sense to do this because a player’s location is a “property” (in the general sense) of the player.

Storing the current monster

We also need a place to store the current monster that the player is fighting, in case they move to a location that has a monster there.

In the World class, we have a list of all the monsters in the game. However, we can’t use the monsters from there to fight against. We only have one “instance” of each monster. So, if we fought against the rat in the World.Monsters list, and killed it, the next time we fight against it, it would already be dead.

When we move to a new location, if it has a monster there, we’ll create a new instance of that type of monster and save it to a variable. Then, the player will fight against that instance of the monster.

 

Now that we know what we want to accomplish, we’re ready to add the code.

 

Creating the functions to move the player

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

Step 2: First, let’s create the new property to store the player’s current location.

Double-click on the Player class, in the Engine project. Add a new property named CurrentLocation, with a datatype of Location.

Note: You don’t have to put the properties in any specific order. I just like to like to keep my List properties at the end of the properties. I find it a little easier to read when they’re grouped in the same place in every class.

Step 3: Right-click on the SuperAdventure.cs form, in the SuperAdventure project, then select “View Code”.

Step 4: Because we have a lot of code to write, and it’s easy to mistype something, copy the new code for the SuperAdventure form from here (https://gist.github.com/ScottLilly/208630cfcdded1cbfdc0) and overwrite all the existing code in SuperAdventure.cs with it.

 

What’s in the code you just added?

On line 18, we added a new variable to hold the monster that the player is fighting at the current location.

In the form’s constructor, we do a couple things to start the game.

On line 25, we “move” the player to their home. Since the MoveTo function expects a location as the parameter, we need to use the World.GetLocationByID() function to get the correct location. This is where we use the constant World.LOCATION_ID_HOME, instead of using the value 1. It’s much easier to look at the code with the constant and know what it’s supposed to be doing when we use this clearly-named constant.

On line 26, we add an item to the player’s inventory – a rusty sword. They’ll need something to fight with when they encounter their first monster.

We added the new MoveTo function to handle all player movement.

We’ve also gone into each of the four functions that handle moving in a different direction and had them call the MoveTo function.

The MoveTo function

The first thing you may notice in this function are the lines that have a “//” in them. These are comments. Comments are ignored by the computer. They only exist for programmers to read, to know what the program is supposed to be doing. Everything after the double-slashes, until the end of the line, is ignored by the computer.

I used a lot more comments in this function than I normally would. That’s to make it easier for you to follow along with what is happening, and see how the code ties back to the pseudo-code we have above.

The second thing you may have noticed is that this function is long – over 300 lines long.

That’s really too long for a function.

When a function is that long, it’s difficult to keep track of what exactly it’s supposed to be doing. But we’re going to clean this up in the next lesson. Personally, I like to keep my functions around 10 to 40 lines long.

We’ll break this function into smaller functions in the next lesson.

What’s happening in the MoveTo() function?

On line 57, we have our first “if” statement.

In this case, we check if the new location has any items required to enter it.

The “!=” is how C# says “not equal to”. The exclamation point, when doing any sort of comparison in C#, means “not”. And “null” means nothing/empty.

So, if the ItemRequiredToEnter property of the location is not empty, we need to check if the player has the required item in their inventory. If it is empty, we don’t need to do anything – there is no required item, so the player can always move to the new location.

On line 72 we see if we found the required item in the player’s inventory. If we didn’t find an item with a matching ID, the “playerHasRequiredItem” variable will still be “false”.

Notice the exclamation point in front of playerHasRequiredItem.

Let’s assume the player does not have the required item in their inventory. The “playerHasRequiredItem” variable will have a value of “false”. Doing a “not” on a Boolean variable, reverses its value: “!true” equals “false”, and “!false” equals “true”.

Thinking “if not false” is not as clear as thinking “if true”. But they both mean the same thing.

On line 75, we display the message that the player is missing the item required to enter this location. This line has a new “+=” symbol.

“+=” means, take the value from the variable/property on the left, add the value on the right to it, and assign the results back into the variable/property on the left.

When you use “+=” with a string, it means, “add the string value on the right to the end of the existing string”. When you use it with a number, it means, “add the value on the right to the value on the left”.

Here, we take the text in the rtbMessages RichTextBox, and add our new message to the end of it. That way, the player can still see the old messages. If we used the “=” sign instead, it would replace the existing “Text” value with our new message.

We also have “Environment.NewLine”. This adds an “Enter” to the text, so the next thing we add to it will be displayed on the next line, instead of the end of the current line.

Line 76 has “return”. This means “exit out of this function”.

Since this function is a “void” function (see line 54), it doesn’t return a value. We can “return” here and not do the rest of the function. We want to do that in this case, because the player does not have the item required to enter the location. So, we don’t want to do the rest of the function, which would actually move them to the location.

On lines 84 through 87, we make the movement buttons visible, or not, based on whether or not the new location has a place to move to in each direction. We do this by checking if the property for the location is empty or not.

The “Visible” property of the buttons expects a Boolean value: true or false.

So, on line 84, if the LocationToNorth property is not empty, the value to the right of the equal sign will evaluate to “true”, and the button will be visible. If the LocationToNorth property is empty, this will evaluate to “false”, and the button will not be visible.

On line 100, we check if there is a quest at this location. If so, we need to do some more work.

Lines 106 through 117 are where we look through the player’s quest list, to see if they already have the quest at this location and if they already completed it.

Lines 128 through 163 looks at each item required to complete the quest, then checks each item in the player’s inventory, to see if they have it, and have enough of it, to complete the quest.

There are some “break” statements in this “foreach”. Those are used to stop looping through the items and exit the “foreach” loop. If we discover that the player doesn’t have one item, or enough of it, to complete the quest, we can stop checking for any other items.

Line 180 has a “-=”. The “+=” means, “add the value on the right to the variable/property on the left”. So, a “-=” means, “subtract the value on the right from the variable/property on the left”. You can only use this with numbers, and not strings, unlike the “+=”.

In this case, we are using it to remove items from the player’s inventory that they turn in to complete the quest.

On line 204, there is a “++”. When you have this after a variable or property, it means “add 1 to this variable or property”. There is also a “–“, for when you want to subtract 1 from a variable or property.

At lines 264 through 273, we create the new monster to fight, by making a new monster object, using the values from the standard monster in our World class.

Updating the DataGridViews in the UI

From lines 290 through 321 we update the DataGridView controls in the UI.

The player’s inventory will have changed if they completed a quest. The items needed to turn in were removed from their inventory. The reward item was added to their inventory. So, we need to update the UI with their current inventory.

Also, if they received a new quest, or completed an existing one, their quest list would change.

Updating the ComboBoxes in the UI

For the ComboBoxes in the UI, we create new lists to hold the specific datatype of items we want to show in the list (lines 324 and 353). Next, we go through all the items in the player’s inventory and add them to these lists, if they are the correct datatype (lines 326-335 and 355-364).

On lines 328 and 357, there is a new comparison: “is”. This is used to see if an object is a specific datatype.

Remember how we created the Weapon and HealingPotion sub-classes of the Item class? When you create a Weapon object, its datatype is both Weapon and Item. When you create a HealingPotion object, its datatype is both HealingPotion and Item.

If the lists are empty (weapons.Count or healingPotions.Count are equal to 0), we hide the ComboBox and “use” buttons, since there is no weapon or potion for the player to use.

If the lists have items in them, we “bind” the list to the comboboxes (lines 345-349 and 374-378). The “DisplayMember” determines what property will be displayed in the comboboxes. In this case, we want to display the value of the Name property. The “ValueMember” is the behind-the-scenes value we’ll use a little later, to know which item was selected.

NOTE: There are better ways to connect your list properties to DataGridViews and ComboBoxes. But we’re just concentrating on the basics in these tutorials.

NOTE: Hopefully you noticed something about the variable names. They are generally long and descriptive. That makes it easy to understand what values they are supposed to hold. By making your variable names descriptive, it will be easier to work with your program – especially when fixing bugs or making changes in the future.

There are a couple cases in the “foreach”s where I use short variables like “qci” and “ii”.

Since the foreach loop is only a few lines long, I sometimes use a shorter variable name. The variable has a very short life – it only exists within those few lines of the loop. You can display the whole loop on your screen, without scrolling. So, it’s easy to keep track of where the variable is populated and where it is used. If the loop was longer, I’d use a longer, more descriptive name.

 

Summary

Now you’ve seen how to plan out your logic in pseudo-code, and then create a function in the program to perform that logic.

You’ve seen how “if”s, “else”s, and “foreach”s are used in a function.

You’ve also seen what a huge function looks like, and you probably have an idea how difficult it would be to work with it. The next lesson will cover how to clean up that function and make it easier to understand.

We covered a lot in this lesson. If there was anything that wasn’t clear, please leave a comment below and I can update this lesson with some more details.

 

Source code for this lesson

Get it from GitHub: https://gist.github.com/ScottLilly/208630cfcdded1cbfdc0

or DropBox: Lesson 16.1 – https://www.dropbox.com/sh/mklgy3fjvixg9xg/AADp47PdxOYk_V7f7IvfLdS3a?dl=0

 

Next lesson: Lesson 16.2 – Refactoring the player movement function

Previous lesson: Lesson 15.1 – Getting random numbers for the game

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

149 thoughts on “Lesson 16.1 – Writing the function to move the player

  1. Hi Scott,

    I’m having a little trouble with this lesson. I pasted the required code into the Designer portion of the UI, but I’m getting the following error on a snippet of code. The code is as follows:

    “this.dgvInventory.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick):”

    MVS is telling me that I don’t have a definition for dataGridView1_CellContentClick or an extension method for dataGridView1_CellContentClick . How do I fix this?

    Thank you!

    1. That could have happened if you accidentally double-clicked on the datagrid control. Visual Studio would have created an “eventhandler” in SuperAdventure.Designer.cs, and an empty function in SuperAdventure.cs. The code you pasted in wouldn’t have the function, which is what the error is saying.

      To fix it, you can edit SuperAdventure.Designer.cs. Find the line in the error message – the one with “this.dgvInventory.CellContentClick +=”, and delete it. That should fix the error.

      You can read more about eventhandlers in Lesson 21.3, if you want a better understanding of them.

      Please tell me if that does not fix the problem.

  2. Hi Scott, terrific job you did right here!

    Something I need to point out. I copied the whole content in your SuperAdventure.cs to mine (had to adjust some class names and some minor tweaks) but had an error on line 255:

    “Error CS7036 There is no argument given that corresponds to the required formal parameter ‘isCompleted’ of ‘PlayerQuests.PlayerQuests(Quest, bool)’…”

    I added a simple “, false” as a 2nd parameter, as it was asking for a bool value and the quest gotten should not be completed.

    I didn’t see anyone else complaining about this so maybe I missed something on the PlayerQuests.cs file…

    Any thoughts?

    1. Thank you!

      For the error you saw, check your PlayerQuest class against the one shown in Lesson 10.1. The constructor should only have one parameter – the “details” parameter, which is a Quest datatype.

      If that isn’t the source of the problem, can you upload your SuperAdventure.cs, Player.cs, and PlayerQuest.cs files to https://gist.github.com, so I can look at them?

  3. I really don’t know where to start with the errors i’m having…

    First off: in SuperAdventure.cs

    CS0103 The name ‘dgvInventory’ does not exist in the current context SuperAdventure

    Secondly: in SuperAdventure.Designer.cs

    CS1061 ‘SuperAdventure’ does not contain a definition for ‘label2_Click’ and no extension method ‘label2_Click’ accepting a first argument of type ‘SuperAdventure’ could be found (are you missing a using directive or an assembly reference?)

    And the same error ^^^ for label5_Click , richTextBox_TextChanged and SuperAdventure_Load.

    I’ve been following along the PDF version or the lessons.

    What have i done wrong? ;W;

    1. Hi Jacob,

      For the error “‘dgvInventory’ does not exist”, edit SuperAdventure.cs in Design mode (the graphics screen). Left-click on the top-left datagrid one time and look at its properties in the lower-right section of Visual Studio. Make sure the “(Name)” value is “dgvInventory” – a capital “I”, as in “India”.

      For the other errors, if you accidentally double-clicked on some of the controls (the labels, form, etc.), when you were in the Design screen for SuperAdventure, Visual Studio would have created functions and “eventhandlers”. Eventhandlers are what connects the UI controls to their functions in SuperAdventure.cs. If you pasted code into SuperAdventure.cs (in one of the lessons afterwards), it would not include the functions that Visual Studio created for those extra eventhandlers.

      You can read more about how eventhandlers and the Designer file work in Lesson 21.3.

      To fix the errors for the label2_Click, label5_Click, richTextBox_BoxChanged, and SuperAdventure_Load, you can modify SuperAdventure.Designer.cs

      Find the lines with the four eventhandlers and delete them.

      That should fix the errors. If it doesn’t please tell me, and we can look at it some more.

      1. That fixed the problem.

        I had created random event handlers which i deleted, allowing me to open the designer.

        i had named dgvInventory “dbvInventory”, which happens way too often when i type.

        1. A new error has popped up.

          Application.Run(new SuperAdventure());
          CS0118 ‘SuperAdventure’ is a namespace but is used like a type

          1. Did you create the project with a different name? That might be one source for that problem. There is some information about how to fix renamed projects in Lesson 02.2B

            If that is not the source of the error, can you upload your solution (including the folders under it, and the files in those folders) to GitHub or Dropbox, so I can look at it?

  4. I can’t get _currentMonster in Super adventure to work for the life of me. It keeps saying “_currentMonster does not exist in the current context”.

  5. Hi Scott. Thanks for the tutorial, its been really fun and useful so far. I’ve now encountered a few errors after pasting the code into SuperAdventure.cs.
    Most of it is fine but there is an error under the words “CurrentLocation” and “Weapon”.
    The errors say
    “‘Player’ does not contain a definition for ‘CurrentLocation’ and no extension method ‘CurrentLocation’ accepting a first argument of type ‘Player’ could be found (are you missing a using directive or an assembly reference?)”
    and
    “‘Weapon’ could not be found (are you missing a using directive or an assembly reference?)”

    I’m guessing I’ve missed some lines maybe in some of the classes?

    Do you know what I’d need to change to fix it?

    Thanks for the help

    1. Hi Stephen,

      For the CurrentLocation, make sure you have the changes for the Player class that are in this code: https://gist.github.com/ScottLilly/208630cfcdded1cbfdc0#file-player-cs You especially want to check the CurrentLocation property on line 14. If that is there, make sure it is spelled exactly the same, including the “C” and “L” being upper-case.

      For the Weapon error, do you have a “Weapon” class in your Engine project? That should have been created in some earlier lessons. The last lesson it was in was Lesson 08.2. If you have the class, make sure it is “public” on line 9 (see the full class code here).

      If those do not fix the errors, can you upload your solution (including the folders underneath it, and all the files in those folders) to GitHub or Dropbox, so I can look at it?

      1. Hi, thanks for the help. I must have accidentally missed that line “Public Location CurrentLocation { get; set; }” The weapon error was simply a matter of me having called it “weapons” instead of “weapon”.

        The only problem I’m encountering now is an error message saying “‘SuperAdventure’ does not contain a definition for ‘Form1_Load’ and no extension method ‘Form1_Load’ accepting a first argument of type ‘SuperAdventure’ could be found (are you missing a using directive or an assembly reference?)”
        I changed all instances I could find of the form from “form1 to “super adventure” earlier in development, however maybe I changed one that I shouldn’t have. I was hoping I could go onto the form properties and change the name back but it now won’t show and has a message reading:
        “The designer cannot process unknown name ‘Form1_Load’ at line 295. The code within the method ‘InitializeComponent’ is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again.”
        This means I can’t change the names of anything within the designer.
        I have attempted to change form 1 to different versions of “superadventure” that I might have called it but the same error remains.

        This form1_Load part is line 295 in the SuperAdventure.Designer.cs code. The full line is
        this.Load += new System.EventHandler(this.Form1_Load);
        which means the problem might not be directly related to the new code added in this lesson, however it was working fine last lesson.

        Do you know what I need to change to fix it or should I go back to an older save and change the name back to form1?

        1. Hi Stephen,

          It sounds like you might have accidentally double-clicked on the SuperAdventure form, while in design mode. If you do that, Visual Studio will try to make a function and connect to it with an “eventhandler”. The error is saying that there is an eventhandler, but the class is missing the function it wants to connect to – which would have happened if you pasted in the code from a lesson (which would not have the extra function). For this program, we do not need to use the form’s “Load” event.

          To fix the error, you can edit SuperAdventure.Designer.cs. If you delete line 295, or comment it out, that should fix the problem.

          You can read more about eventhandlers in Lesson 21.3.

          Please tell me if that does not fix the error, or if you have any other questions.

  6. Hey Scott.
    Could you please explain line 165 step by step:

    Monster standardMonster = World.MonsterByID(newLocation.MonsterLivingHere.ID);

    I understand what the code is doing. I just don’t understand how 🙂
    Thank you very much ! 🙂

    1. Hi Tom,

      When we created the Location object, we also created a Monster object, and set the Location’s MonsterLivingHere property to that Monster object. That Monster object has its own properties, like “ID”.

      So, “newLocation.MonsterLivingHere.ID” looks at the location the player is moving to (the “newLocation” object), reads its MonsterLivingHere property (which is a Monster object, with its own properties) and reads the “ID” property from that monster object. That gives us the ID of the Monster that lives at that Location.

      We call World.MonsterByID() and pass in the Monster’s ID value, to get the “standard” instance of that Monster object, which we store in the “standardMonster” variable – whose datatype is the “Monster” at the start of the line.

      Does that make it clear?

      1. Thank you very much ! Its more clear now.
        I just try to understand how the program knows which Monster ID to compare before we pass it into the World.MonsterByID(). Where is it that we tell the program what to use as ID for the monster’s ID before we pass it into World.MonsterByID() ? I understand that Monster has an ID and that it exists in “newLocation”. But how does it know what ID it has when we haven’t passed anything into it yet? I hope my question makes sense 🙂

  7. Hi again Scott,

    I moved on even though I haven’t worked through the previous errors. In the SuperAdventure.cs code in all instances where the World class was mentioned, it says that it doesn’t exist in that context. Also, the other major error is regarding when Monster was mentioned, it says it’s not available due to its protection level even though it is a public class. I’m a bit confused. If you could help it would be greatly appreciated.

    Thanks,
    -Taryn

    1. Actually for this one never mind, I was able to fix this myself but there are some errors left, one regarding the RewardExperiencePoints, and two regarding SuperAdventure_Load.

      The first one is:

      -Severity ‘Monster’ does not contain a definition for ‘RewardExperiencePoints’ and no extension method ‘RewardExperiencePoints’ accepting a first argument of type ‘Monster’ could be found (are you missing a using directive or an assembly reference?) SuperAdventure

      And the other two are:

      -The designer cannot process unknown name ‘SuperAdventure_Load’ at line 291. The code within the method ‘InitializeComponent’ is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again.

      -‘SuperAdventure’ does not contain a definition for ‘SuperAdventure_Load’ and no extension method ‘SuperAdventure_Load’ accepting a first argument of type ‘SuperAdventure’ could be found (are you missing a using directive or an assembly reference?)

      Thanks for any help,
      -Taryn

      1. I have several comments from you, and it looks like the latest one is that the errors are fixed now. But, it looks like that is for your questions on lesson 11.1

        If you still have the problems in this comment, you can do this:

        The RewardExperiencePoints property was added to the Monster class in Lesson 06.1. Make sure you have it set to “public”.

        SuperAdventure_Load was probably created if you accidentally double-clicked on the SuperAdventure form, when it was in “Design” mode. If you do that, Visual Studio creates an “eventhandler”, and a SuperAdventure_Load function in SuperAdventure.cs. Then, if you paste in the code from the lesson later, it does not have the SuperAdventure_Load function – which causes the error.

        You can fix this by editing SuperAdventure.Designer.cs. Delete (or comment out) line 291 – the line with the SuperAdventure_Load.

        You can learn more about eventhandlers in Lesson 21.3

        Please tell me if that does not fix the errors in your program.

        1. Hi Scott,

          Thank you so much and I’m sorry my comments were so scattered yesterday, I just really wanted the program to work.

          Regarding the Monster class adding public in front of it did fix the errors and the error with the RewardExperiencePoints was just a typo on my part, something, now I realise, should have been one of the first things I checked.

          And the last error was fixed, I didn’t realise that piece of code was unnecessary. Now all the errors are fixed, again, thank you very much.

          -Taryn

  8. hey, scott I have a problem in line 289 in the SuperAdventure.Designer.cs

    “his.Load += new System.EventHandler(this.SuperAdventure_Load)”

    that’s said

    “SuperAdventure’ does not contain a definition for ‘SuperAdventure_Load’ and no extension method ‘SuperAdventure_Load’ accepting a first argument of type ‘SuperAdventure’ could be found (are you missing a using directive or an assembly reference?)”

    i don’t know what the problem is i have fowler you’re guides to everything

    1. Hi Frederik,

      SuperAdventure_Load was probably created if you accidentally double-clicked on the SuperAdventure form, when it was in “Design” mode. If you do that, Visual Studio creates an “eventhandler”, and a SuperAdventure_Load function in SuperAdventure.cs. Then, if you paste in the code from the lesson later, it does not have the SuperAdventure_Load function – which causes the error.

      You can fix this by editing SuperAdventure.Designer.cs. Delete (or comment out) line 289 – the line with the SuperAdventure_Load.

      You can learn more about eventhandlers in Lesson 21.3

      Please tell me if that does not fix the errors in your program.

Leave a Reply

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