Sunday, November 18, 2012

Nokia's Snake - Part I: Preparation - Making a Quick Game

Here our journey starts! And why not start it with something legendary! Something so simple yet addictive that a whole generation of traveling people would play it in subways all around the world!? Snake!

Even though this game was created in 1970s and Nokia had nothing to do with this, it became a world-wide hit after it became standard game on Nokia phones! So please allow me to call it Nokia's Snake!


About importance of preparation!

Most probably at this stage you think that you only have a problem of HOW to develop a game and there is no issue of WHAT game to develop! You might think it's the easy part! I know I did! 

But once you start developing you come to realize that there are some things you didn't think were to be done. In the best case scenario you will simply add the functionality/content and it will slightly affect your deadline. But in the worst case (like I had) you're gonna have to rebuild the whole code and redo some graphical assets.You need to have a good map before you start your trip, because you need to know what to put in the backpack and which shortcuts are actual shortcuts!) So...

Let's break down the game before we try to build it!

In this game we control this creature which is kind of (surprise, surprise!) a snake! But we do control only it's head with a d-pad, the rest of the body is just following the same route.
The snake lives in this weird rectangular world and it can't leave it! The only thing it can do is eat! There is always just one "food"-thing on the screen and once the snake eats it, another one appears! But this unhealthy nutrition makes snake grow! With every meal our snake is longer!
Speed
Oh and did I tell you the most awful thing about the snake's life!? It has this SandraBullock-KeanuReevs medical condition where she just can't stop! If it stops it dies! Hit a wall - DEATH! Hit your own body - DEATH!

The point of this game is simply high score! No bosses, no story lines :) Just play as long as you can!

So let's write our understanding of what functions this game needs to have in my favorite bullet list style.
  • As the game loads we see menu with two buttons: StartGame and Score.
  • Score will show us other menu with list of previous game records if there are any.
  • StartGame will start the game, duh.
  • As the game starts we see the world space, pause button, current score and current top record.
  • The world space consists of floor and walls. We'll call walls TheWalls :)
  • Player is represented by an object we'll call SnakeHead.
  • The SnakeHead constantly moves in the direction it is looking.
  • Player can change direction of the Snake movement, by pressing keys: up,down,left,right.
  • Object that we'll call BodyParts follow the route of the head!
  • There is a concrete number of BodyParts.
  • An object we'll call SnakeFood should be randomly created within the world space.
  • Every time SnakeHead touches SnakeFood, it is disappears. Number of BodyParts is increased by one and a new SnakeFood is randomly created within the world space. 
  • If SnakeHead touches a BodyPart or one of TheWalls the game is over!
  • Once the game is over we create a record of number of BodyParts the Snake head at the moment of death into a list. We compare the record to the existent ones (if any) and put it after the record that is heigher, but before the one that is lower, thus creating an aligned chart of records.
 PS: Before we start with the game itself let's make sure we are on the same page and you have all of the tools tuned and ready! Meaning you should have your UDK and IDE installed, folder(s) for your code created, .ini files edited and so on. If you haven't coded a game in UDK yet, you need to do that! This post will help you out:
Set-up UDK for Programming with UnrealScript


Monday, November 12, 2012

Set-up UDK for Programming with UnrealScript

 So there are couple of necessary steps you have to make before you can start developing the game of your dreams with UDK. Captain Obvious was assisting me on writing this article.
  1. Download UDK
  2. Install UDK
  3. Download an IDE
  4. Install the IDE
  5. Create folder for your classes.
  6. Add entry about the folder to UDK .ini file
  7. Create BuildGame shortcut.
  8. Create batch file

Now when you know what we need to do, let's do it!

Get UDK

Latest version of UDK can be downloaded from here:
At the moment of writing November 2012, was the latest. It is also the first one to introduce Clean Starter Project Tool. So do not be surprised or confused once you see this message:
Difference between those is that in the first case UnrealTournament will be installed with all the graphical assets and code, both of which can be helpful both to analyze and reuse. My advice is go with the SampleGame install. Both cases work for following the tutorials on this blog, because even though I do read UT code from time to time, I never use (read "extend") them. The rest of the installation should be ordinary, unless they add some other stuff in later versions.
By the end of the installation though, you will be asked if you would like to install version control tools. We don't need them for now.

Get an IDE

IDE stands for Integrated Development Environment! I know that because I Googled it! Shortly it's the program that will help us write code! One might have thought (I did) that UDK has some built in coding tool (like say Flash), but it doesn't. You need something that will edit class files.

Classes are simple text files. You can use Windows Notepad for the job! But IDEs help you read and write the code better and faster. 
My study process went much faster once I switched from UnCodeX to VisualStudio+nFringe. But then I found this Unreal Script IDE which almost the same as VisualStudio+nFringe, but it comes in one package and is free. (nFringe is only free for non-commercial use) 

You can get it here: 

Now listen to me very carefully! Do NOT "Next, Next, Next"!!! Depending on what was installed on your machine before the installation might take long time or it might take couple of seconds.
Once it comes to "Select UDK Binary Win32 Folder" select path to Binaries\Win32 folder within your UDK installation. 
God I hope you remember where installed it! By default it's right in C:\.

Once the installation is over run the Unreal Script IDE, there should be an icon created on your desktop. Now, does your Solution Explorer in the right side of the screen look somewhat like this? If you did SampleGame installation of UDK, then you should have more folders and files here, but what matters is that it shouldn't be empty!

If it is empty, try reinstalling and make sure you've done "listen to me very carefully" part right. 
This page might also help you if you're having problems:  


Create folder for our work!

Some of the following steps can be done from UnrealScriptIDE, but I won't use it in case you choose some other IDE for your work. 
Go to your UDK folder! If you don't see 4 folders: Binaries, Development, Engine, UDKGame, then go into the folder with date in its name.(or the folder might be called Custom or something you called it if you did the EmptyGame installation) Hopefully now you see the folder!
Go to Development\Src. Here you will create folder for your project! Call it whatever you want, make it meaningful, don't make it long! You might gonna have to type this name a dozen of times later.
So let's say we created a folder called UnrealGame
Create folder called Classes inside of it.

UDK doesn't really like totally empty folders so... Open notepad and let's quickly create our first class! 
Copy/paste the following lines into your Notepad. Save the file as UnrealGameInfo.uc in the Classes folder we have just created! (don't forget to switch Save As Type to All Files in Notepad Save As dialogue)

class UnrealGameInfo extends SimpleGame;
defaultproperties
{
}

You can switch UnrealGameInfo with whatever name you want, but keep in mind that the name of file and name in the code should be identical.

Let UDK know we exist!

If you think that UDK is gonna pass by, notice the newbie folder and make you happy just like that, I gotta bad news for you! We've got to add an entry in config files about the folder for UDK to take it into consideration when compiling code.
Go to \UDKGame\Config. Find DefaultEngine.ini file there and open it with Notepad. Within this file find this line:
[UnrealEd.EditorEngine]
 Depending on type of your installation there might be one or couple of lines after this one before some blank space starts. So add the following line to the end of the block:
+EditPackages=UnrealGame
Where UnrealGame is name of the folder we created for our classes. Save the file. Now UDK will compile all the code inside this folder, will keep an eye on it and notice changes!

Building!

Depending on IDE, you might not need to go through this step, but I prefer to do this no matter what. Create a shortcut on your desctop to either to \Binaries\Win32\UDK.exe or to \Binaries\Win64\UDK.exe depending on your system. There is no actual difference as far as I'm concerned. (If you don't know which system you're running, just go with 32)
Now go to Properties of the shortcut and add word make to the end of Target line. Like this:
Click OK and double-click the icon! Let's see what happens! If what you see is this (look for  line UnrealGame - Release):
 
Then congratulations! You made it! 
If not, you're probably screwed and gonna have to find what you did wrong! :( Comment here and I'll try to help you out.


Let's make our lives easier!

This step will make the development-testing process much easier, thus faster and more productive! I learned the "trick" from Unreal Development Kit Game Programming with UnrealScript: Beginner's Guide book which I strongly recommend!
Fist we need a scene! If you know what scene you want and are not feeling lazy to make it now, create at least simple version of it in the editor. Otherwise just open editor and save the default scene as UnrealScene in UDKGame\Content\Maps. DO NOT save maps outside this folder.

Open our beloved Windows Notepad! Put the following line in it:

C:\UDK\Binaries\Win32\UDK.exe UnrealScene?GoalScore=0?TimeLimit=0?Game=UnrealGame.UnrealGameInfo -simmobileinput -log
Where  C:\UDK\Binaries\Win32\UDK.exe is path to UDK.exe on your computer. Feel free to change Win32 to Win64 if you're running 64 bit system.

UnrealScene is name of the scene.
UnrealGame name of the folder we created in Development/Src.
UnrealGameInfo is name of the class we created within the folder.
-simmobileinput stands for touch input simmulation. If your target platform is not iOS, remove this. 



Save this as .bat file and call it something like Run_UnrealGame.bat. (don't forget to switch Save As Type to All Files in Notepad Save As dialogue)

Double-click the Run_UnrealGame.bat file! It should start UDK game within the level we choose, using the GameInfo class we wrote!
QUICK TIP: If your computer is as slow as mine, the default resolution UDK runs the game with might be to big for you. Also you simply might want to change the resolution to fit the target platform. To do so open console either by pressing Tab button or "`" button which is the one before number "1" key on the keyboard. Write setres widhtXheight and press Enter. For example for iPhone 3GS screen resolution you would do "setres 480x360" command. Once you close the window, it will always start in the resolution unless you change it.
You're good to go now! 
Edit the class we created, add new classes, build them using the shortcut and run the game using the bat file! 

Wednesday, November 7, 2012

Starting out!


Hi, I’m Fred and this is the part that everyone skips!

I’m going to be honest with you! I’m not a professional programmer. Actually before I tried to write my first game with UnrealScript my total number of lines written in any language combined was below 500.

Also I get bored pretty quick! If I was reading this article I would close it by now! I’ve tried studying about 5 engines and SDKs before I got to UDK and gave up on all of them. Not because they were not powerful enough, just not that exciting!

But on the other hand I am self educated 3D Animator and CG artist and I know that even though studying process might be hard, it’s easier with every step you make! Also it’s very rewarding and fun! I mean what can be more fun than doing something you didn’t think you could, not so long ago! Doing something your friends think only Zuckerberg-like guys can do, makes you feel like you just discovered a super-power! (at least if you’re as geeky as I am)

While I’ve been studying UDK+UnrealScript the lack of information shocked me! It was almost driving me crazy how much info there is about how to setup lighting and animate camera and how little there was about actually programming a game within it. And even the small amount of information that you would find about UnrealScript itself was about modifying code that was already written for Unreal Tournament (which is included into UDK code). That is more like learning editing properties within a text editor then actually developing the unique game you have on your mind. 

So what are we doing here!?


In schools they don’t actually (read: hopefully) teach you just knowledge, they teach you how to work with information. What I’ll be trying to achieve is not to explain you how I write a particular game, but how to bring any game idea of yours to life!

We’re gonna be building fully functional games from scratch here! We’ll be studying how UDK and UnrealScript work and are organized. We’ll cover some basic math and algorithms where necessary. We’ll learn how to quickly build simple games as well as how to develope a feature heavy ones.

How we are gonna roll!


We’re going to have at two major categories for the start! Quick Game and Big Project!

Quick Game - will be all about a game that can be written within less than 16 work hours. And I stress the word “written”! Creation of graphics is not included. We’ll do a separate category for this later. Once there are people who read these stuff, we will decide together what game we’ll be writing! These posts will be out on Mondays.

Big Project - will be about creation of a much bigger game! The one that takes couple of guys working for half a year at least. Actually it is going to be similar to the one my team is developing for a year now. These will be out on Fridays!

We will be working together! I mostly create graphics myself, but I understand that a lot of people who would like to learn how to program games, might not have the skill. So I’ll give you guys all the graphics I’m gonna be using, unless it’s the graphics created for some other project. But I’ll mostly try to have stuff exclusively created for the blog.

I’ll be making both blog posts and videos! Even though I might be covering similar topics in text and video, but they are gonna be far from identical! SO you can read! You can watch! But you better do both!

So well yeah... I guess that’s enough for an intro! I’ll see you guys on Monday!

Good hunting!