Press "Enter" to skip to content

Lesson 02.2 – Building the solution for the game

Lesson objectives

At the end of this lesson, you will know…

  • How to create a program (solution) in Visual Studio
  • How, and why, to have separate projects in your solution

 

How to build your first solution in Visual Studio

People generally use “program”, “application”, and “project” interchangeably. However, Visual Studio calls a program a “solution”.

A “solution” is broken into smaller pieces that Visual Studio calls “projects”. The solution must have at least one project.

In these lessons, I’ll use “program” to talk about the game, “solution” for all the files used to make the game, and “project” for the different groups of files within the solution.

 

Deciding how many projects the solution needs

When I create a solution, there are always at least two projects.

One is for the user interface (UI), the part of the program that reads and writes to the screen. The second project is for the “logic” of the program.

I do this is for a couple of reasons.

First, it makes the program easier to test.

We won’t do it in these tutorials, but it’s possible to write automated tests to make sure the program does what it’s supposed to do. If you have “logic” in the user interface, this is difficult to automate.

Second, sometimes you’ll want to use one of your projects in another program.

One example might be the code to read from, and write to, a database. That could be useful for any program you write that needs to access a database.

If the database code is in the UI project, you won’t be able to use it in another project, unless it needs to use the same screens from the UI project.

So you put that all that database access code into a separate project, and you can copy it to your next project that uses a database.

When we write the game, you’ll see how I decide what to put in the Engine project.

NOTE: For a large business program, you might have dozens of projects in a solution – which sometimes leads to problems with keeping track of everything.

 

Creating a solution with two projects

Here’s a video of how to create the solution for your game.

 

 


Link to video on YouTube

 

Step 1: Start Visual Studio Express 2013 for Desktop

 

Step 2: Create a new solution with a “Windows Forms” project (File -> New Project -> Windows Form Application). Make sure you are using the using the project templates listed under “Visual C#”.

NOTE (for Visual Studio 2017 users): You can create this project under Visual C# -> Windows Classic Desktop -> Windows Forms App (.NET Framework). The “Engine” project will be under Visual C# -> Windows Classic Desktop -> Class Library (.NET Framework)

A Windows Form application is a user interface project, one that will display the program on the screen, wait for the user’s input, and update the information on the screen.

We’ll name the program “SuperAdventure”, and save it in the C:\Projects directory.

Important note!If you give your UI project a different name, click here to see how to fix the namespace (so you won’t have problems in the rest of the lessons).

 

 

Step 3: Now we’ll create the “logic” project.

Right-click on the name of the solution “SuperAdventure”, in the Solution Explorer and select Add -> New Project…, to bring up the project selection screen.

For the project type, select Visual C# -> Windows Classic Desktop -> Class Library (.NET Framework). Name the project “Engine”. (See the image above)

NOTE: If you are using Visual Studio Community Edition 2015, you want the project type that just says “Class Library”, NOT “Class Library (Portable)” or “Class Library (Package)”

The project didn’t need to be named “Engine”. That’s just what I like to name it. However, use “Engine” when you create yours, so it will match what’s in the other lessons and videos.

 

Step 4: When you create a Class Library project, Visual Studio automatically adds a “Class1” class. You don’t need it, so right-click on it and select delete, to get rid of it.

 

Step 5: When you create a Windows Form project, Visual Studio creates a “Form1” screen by default.

You don’t need to rename it, but I always do. Right-click on it, select “Rename”, and rename it to SuperAdventure (be sure to leave the “.cs” on at the end).

The form name does not need to match the project name. That’s just what I chose to name it for this program.

 

Step 6: For the UI project to use the code in the Engine project, we need to connect them.

This is called “setting a reference”.

In the SuperAdventure project, right-click on References -> Add References… -> Solution (which will show you the other projects in the solution) -> Engine -> OK.

Now you can see that “Engine” is listed as a reference in SuperAdventure.

This means that the SuperAdventure project will be able to use the code in the Engine project.

 

Step 7: Now you can run the program.

Click on the “Start” button to see what the game looks like.

It’s not much yet, but you have to start somewhere.

NOTE: When you create the solution, Visual Studio says “New Project…”, when it really should say “New Solution…”, to use the correct term.

 

Summary

Now, everything is ready to start writing the program.

The solution has two projects – one for the UI, the other for the code to do all the calculations and logic for the game.

If you had any problems, or have any questions, please leave a comment below.

 

Next Lesson: Lesson 03.1 – Building the first screen

Previous lesson: Lesson 02.1 – Installing Visual Studio Community 2017

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

90 Comments

  1. Sam
    Sam July 31, 2014

    Loving the guide, really helping me learn programming. Just want to point out some phrasing issues in this page – particularly in the “Deciding how many projects the solution needs” section in the 4th and 5th paragraphs. Great guide though, really enjoying every minute of it.

    • Scott Lilly
      Scott Lilly July 31, 2014

      I’ll take a look this weekend and see if I can clean it up.

  2. justmin
    justmin November 18, 2014

    Very helpful guide. thank you!

  3. m
    m May 12, 2015

    The only thing that would have made this better, is pictures with each step. I could have flew through this thing with those, but I got slowed down by having to read things very carefully, or watching the slow-paced video (which is good, it needs to be slow-paced, which is why you need the pictures for speed!).

    But really though, great tutorial, I’m excited to get through it.

    • Scott Lilly
      Scott Lilly May 12, 2015

      Thanks, I’m glad you liked the course. It was tough trying to figure out a good balance for pictures, since they can be big and slow down the website. For future tutorials, I may see if there are any good tools to have small thumbnail pictures on the page (so it loads fast) that can easily (and optionally) be expanded for any users who want to see the full-sized versions.

  4. Joe
    Joe June 27, 2015

    Liking the course!  Just wanted to know if I should have been able to hear audio for the “C# RPG Tutorial – 02.2 – Building the solution for the game” video.  I had volume turned to the max but no audio, which I was able to pickup when I watched the Visual Studio 2013 installation video.

    • Scott Lilly
      Scott Lilly June 27, 2015

      Thanks. There’s only audio for the Visual Studio installation video. I was living in a noisy apartment when I made the course, so I didn’t do audio for the rest of them.

  5. Alex
    Alex August 18, 2015

    Hello! I’ve started this tutorial and already encountered a problem.

    When trying to create the reference, I get an error message saying “A reference to “Engine” could not be added.”

    Tried searching for a solution but couldn’t find any. Thanks.

    • Scott Lilly
      Scott Lilly August 18, 2015

      Hmmm. The only ways I can think of for this to happen don’t seem very likely to happen. Try going into the SuperAdventure references, delete any references to “Engine” that you see, save the solution, then try to re-add the reference.

      If that doesn’t work, can you take a screenshot of your Solution Explorer (with the references sections expanded for both projects), post the screenshot somewhere, and send me a link to it?

  6. Alex
    Alex August 19, 2015

    Hi, me again! Found a fix for the error. I don’t know why it worked, but reinstalling Visual Studio did the trick. Now I could add the reference without a problem!

    • Scott Lilly
      Scott Lilly August 19, 2015

      Cool, although it would be nice to know why it happened. But, at least you can continue. Let me know if you run into any other problems.

  7. Ards
    Ards August 28, 2015

    I’m having the same problem with not being able to add the engine. The only thing that is different is that I’m using VS 2015.  Could that be the problem?

    • Scott Lilly
      Scott Lilly August 29, 2015

      I expect it should still work about the same in Visual Studio 2015. I haven’t installed 2015 yet, but will be doing that this week on my new computer.

      If you can do a reinstall without much difficulty, you might want to try that. Or, if you can post a picture of the Solution Explorer (with the “references” sections expanded), I’ll see if there’s any obvious problem.

  8. Ange Claude
    Ange Claude September 5, 2015

    hello;

    i try to Add engine class to my reference and it doesn’t appear on my reference list as my previous happen to them how they sort out that error

  9. Ange Claude
    Ange Claude September 5, 2015

    hello ;

    At time i was straggling with the reference i got other option so that i can access my class Engine so if there are other who get the same error like me i can guide him

    • Scott Lilly
      Scott Lilly September 5, 2015

      What did you do to get the reference working?

      • John Welsh
        John Welsh March 7, 2017

        I had the same problem as others at first, but I went back and looked in the references section and tried to add Engine again when I noticed a small checkbox to the left of the Engine description. I checked it and all was good.

        • Scott Lilly
          Scott Lilly March 7, 2017

          Great! Let me know if you encounter any problems in the future lessons, that you need some help to solve.

  10. Maggot #9
    Maggot #9 September 13, 2015

    Hi. Just asking why we use winforms instead of WPF. If everywhere I have heard was that is better in most cases, its newer, more styling and less work in graphics part. Should I still stay on winforms?

    • Scott Lilly
      Scott Lilly September 13, 2015

      I used WinForms because that’s what I was working with at the time – although I was mostly working on the back-end parts of systems. If you’re going to create your own new program, you’d probably want to use WPF, since it is more popular. If you’re familiar with WPF, you could create this solution with a WPF project for the UI. I don’t think there’s anything in the source code that is WinForms specific. It should work pretty much the same in WPF (other than the lesson on adding in the controls on the UI form – the labels, buttons, datagrids, etc.)

  11. Joe D
    Joe D September 28, 2015

    Having the same problem with adding Engine Project. Included a screen shot of error, for some reason I cannot link a photo image to this page. Tried dropping it and that didn’t tried visual and text. well this was a short lesson

    • Scott Lilly
      Scott Lilly September 28, 2015

      I sent you an e-mail, so you can send me the link to the error message screenshot. Let me know if you don’t receive it.

  12. Geert
    Geert October 11, 2015

    Very cool guide so far, big thx mate

  13. David
    David November 4, 2015

    Hello,

    I am a student learning informatics and very interested in creating video games, but I didn’t know how and where to start since we are not learning much about creating video games at school (only simple form applications with 1 project in the solution or little about HTML, but this is different! The divided projects and adding the references to link the projects makes it whole for me and as you explain  everything is so clear and reasonable!

    I am at the beginning, but it is already so awesome, just what I was searching for! These descriptions are working perfectly with Visual Studio 2015 too! Thank you already for this tutorial with these brief explanations! I hope that i can keep up with you and create this game for start 🙂 (I am interested in simple graphics too, but thats for later) I appreciate your work!

    Many thanks: David

    • Scott Lilly
      Scott Lilly November 4, 2015

      You’re welcome, David! If you have any questions or problems while doing the lessons, please leave a comment and I will try to help.

  14. Don
    Don November 14, 2015

    Adding a reference to Engine in Visual Studio Community 2015 failed for a Windows Form application, but creating as a WPF project had no issues referencing the Engine class library.

    • Scott Lilly
      Scott Lilly November 14, 2015

      I haven’t heard of that happening before. Can you upload your solution (all the files/sub-directories/etc.) to some place like Dropbox, so I can look at it?

      • Don
        Don November 15, 2015

        Hi Scott, I think something is up with Visual Studio 2015 Community. Although I tried a couple times previously to close VS & start from scratch with no success, I’ve since rebooted and restarted VS & now the project adds the Engine reference without any issues.

        Great tutorial overall, thanks for putting the effort into this!

        • Scott Lilly
          Scott Lilly November 15, 2015

          You’re welcome! It’s good to hear that it’s working now. Let me know if you run into anything else while going through the lessons.

  15. Han Brouwer
    Han Brouwer January 13, 2016

    I’m using VS 2015 Community and made it a Windows 8 solution (using Windows 10).

    Step 6 say:In the SuperAdventure project, right-click on “References” -> “Add References…” -> Solution (which will show you the other projects in the solution) -> Engine -> OK.

    There is no Solution button! I tried to browse to the Engine file but I couldn’t add it to the reference that way.

    • Scott Lilly
      Scott Lilly January 13, 2016

      I believe you need to create the Engine project as a “Portable Class Library”, instead of a “Class Library”, when you have a Windows 8 solution. I will test this tonight, when I finish work, and see exactly what you need to do.

  16. Han Brouwer
    Han Brouwer January 13, 2016

    I`m stupid. I tried to add the referenc to the wrong project. I didn’t realise that there where two references. Sorry 🙂

     

    • Scott Lilly
      Scott Lilly January 13, 2016

      No problem. Let me know if you have any other questions.

  17. Lee
    Lee February 9, 2016

    Hi scott im just starting ur lessons 🙂

    First off ive installed ect followed ur videos. After we started a mew project ive noticed that its “form1.vs” not .cs.

    Does this matter before I continue ?

    Many thanks kind regards

    Lee

    • Scott Lilly
      Scott Lilly February 9, 2016

      Is it form1.vb? If so, that means you created the project as a Visual BASIC project, not a Visual C# project (the “cs” extension is used by C# projects, “vb” by Visual BASIC). You will need to delete that project. When you create the project again, make sure you are using the projects in the Visual C# section.

      Add new project under Visual C# section

  18. David
    David April 10, 2016

    As a network guy who has a couple of (humility here) awesome ideas for games I’ve been having trouble finding where to start… I haven’t had a programming course since 06 let alone coded anything except webpages. A great big thank you for helping me overcome the initial obstacles.

    • Scott Lilly
      Scott Lilly April 10, 2016

      You’re welcome. Good luck with the games you build!

  19. Oliver
    Oliver June 15, 2016

    Hey Scott,

    I run into a little problem with this tutorial.
    I can not run the application because there is no static main method.
    But on your side there is also none and it seems to compile just fine.
    I use Visual Studio Community 2015.
    Any idea how i could fix this?

    • Oliver
      Oliver June 15, 2016

      Ok, I solved the issue. I created the project SuperAdventure with a wrong name, so I renamed it. Because of that I had to rename the namespace inside SuperAdventure.cs.
      I hope this may help others that have similar problems.

      • Scott Lilly
        Scott Lilly June 15, 2016

        Cool. It’s good when you can find the source of a problem yourself. I believe that helps it stay in your memory.

  20. Rhiannon Hardy
    Rhiannon Hardy June 17, 2016

    Hiya i have a few questions (i am using MicrosoftVS 2015), for lesson 02.2, Step 5, do i rename the engine form 1 or do i rename the SuperAdventure form 1?. Also i don’t understand step 4 (lesson 03.1) very well as i can’t see SuperAdventures in the properties at the lower right corner and when i click on properties i still can’t move onto step 5 because i cannot find the ‘text’ property. as well step 5 says to change ‘Form1’ into ‘MyGame’ however i don’t know which form. Could you help?

    • Scott Lilly
      Scott Lilly June 17, 2016

      The Engine project should not have any forms. That project should be a “Class Library” project. It sounds like you might have created it as a Windows Form project. You can’t change a project type, so it might be easiest to delete the solution, and its files (since you haven’t added anything to the projects yet) and re-create the solution and projects.

Leave a Reply

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