Time for action – Compiling and testing AwesomeActor
- Open ConText and press F9, which we set up earlier to compile our code. If we typed everything in correctly, it should give us a Success message at the end!
If there are any warnings or errors, look over the code again to make sure everything is spelled correctly and the punctuation is correct. The error message itself should provide a clue as to where to look. It will also give you a line number where the error happened.
- Now that our code is compiled, let's add our AwesomeActor to a level. If you don't have an editor shortcut or can't find it in your Start menu, it's easy to make one. Go into
UDK-AwesomeGame\Binaries\Win32
and right-click onUDK.exe
. Click on Send To and then Desktop (create shortcut). Right-click on the shortcut it created and click on Properties. In the Target field, add editor to the end without quotes: - Now let's open the editor!
- Close the Welcome Screen and Content Browser, and let's take a look at the editor real quick. To try out our code we're going to need a test map, so go to File, and click on Open, and select
ExampleMap.udk
to open it.One thing we'll notice immediately is that there seems to be a lot of strange objects floating around the level. These are Actor classes that are normally invisible in game, but have sprites that can be seen in the editor. Remember the sprite we added to the default properties of our AwesomeActor? This is where it gets used.
Now let's add our AwesomeActor.
- Click on the Content Browser button to open it up again.
- The Content Browser will show the tab with game assets like textures and meshes at first, but we need to take a look in the Actor Classes, so select that tab in the top.
- This looks a lot different than the class tree in UnCodeX though. Classes can be put into Categories so they're more organized in the editor, but right now we just need to see a normal class tree, so uncheck Show Categories.
There's our AwesomeActor class!
- Select AwesomeActor and close the Content Browser. In the 3D viewport, right-click on the floor and near the bottom click on Add AwesomeActor Here.
There's the AwesomeActor, showing the sprite that we put in the default properties of our class! Normally these sprites won't show up in the game, but we didn't put any restrictions on the one in our default properties so we'll be able to see it for now.
- Click on the Play button to run the game in a new window.
- After you're done checking out the AwesomeActor in the level, close the game window.
- Now let's save the map so we can keep using it to test. We don't want to save over
ExampleMap
so let's save it in our own folder. Create a new folder in theContent\Maps
directory calledAwesomeGame
, and in the editor save the map asAwesomeMap.udk
inside that folder. - Close the editor.
So we have our class set up, but is there anything more we can do with it? Usually the first task when learning a new programming language is to make a Hello World program, so let's do that now. Open up our
AwesomeActor.uc
file in ConTEXT. Let's add some more code.The first thing we'll do while we're here is make it so our actor doesn't show up in the game but still shows in the editor. We can do this with a simple one line addition to our default properties.
- Add a new line in the default properties and write the following:
Begin Object Class=SpriteComponent Name=Sprite Sprite=Texture2D'EditorResources.S_NavP' HiddenGame=True End Object Components.Add(Sprite)
- Now let's add our Hello World. This will go before the default properties section.
function PostBeginPlay() { `log("Hello World! =========="); }
PostBeginPlay
is a function that is run when an Actor is first created, so it's a good place for our Hello World. The log line we put inside that function will output to a text file so we can see that our class is running correctly. So now, our class should look like this:class AwesomeActor extends Actor placeable; function PostBeginPlay() { `log("Hello World! =========="); } defaultproperties { Begin Object Class=SpriteComponent Name=Sprite Sprite=Texture2D'EditorResources.S_NavP' HiddenGame=True End Object Components.Add(Sprite) }
Before we compile, make sure the editor is closed. The compiler can't delete the old
.u
file if it's still in use by the editor and we'll get an error. - With the editor closed, compile the code by hitting F9 in ConTEXT.
- Now open the editor, and open
AwesomeMap.udk
.We don't need to do anything to our AwesomeActor, changes we make to our compiled classes automatically affect any of the actors we've placed in our levels.
- Run the game by clicking on the Play button in the top as before. We'll see that our AwesomeActor is invisible now, so the line we added to the default properties is working. But where's our Hello World?
- Close the game window and exit the editor. Go into the
UDKGame\Logs
folder and take a look at the files in there.There should be
Launch.log
,Launch2.log
, and any number of backups depending on how many times the game, editor or compiler has run. When they run, they create a backup of the existingLaunch.log
file and start a new one. Whenever more than one is run at the same time, as in the case of us running a game window from the editor, it creates a second file calledLaunch2.log
and so on. So, since we were testing our code from a game window in the editor, let's take a look atLaunch2.log
. - Open
Launch2.log
in theUDKGame\Logs
folder.[0008.05] Log: Game class is 'UTGame' [0008.24] Log: Primary PhysX scene will be in software. [0008.24] Log: Creating Primary PhysX Scene. [0008.26] Log: Bringing World UEDPCAwesomeMap.TheWorld up for play (0) at 2011.05.19-15.15.52 [0008.28] ScriptLog: Hello World! ========= [0008.28] Log: Bringing up level for play took: 0.193269
Towards the end of the file we can see our Hello World shows up!
Now you can see why we added a bunch of equal signs in our code. It's pretty easy for our logs to get buried with everything else that's going on, so using some kind of unique marker like we did makes them easier to find.
A quick note about comments
One of a programmer's essential tools are comments. They serve two purposes. First, since they're ignored by the compiler, they can be used to write notes to yourself in your code. Doing this lets you remember what your code does, which can be helpful when you come back to it months later. If you're working with other programmers, writing comments is good programming practice so others can see what your code does.
Second, comments are a quick way to remove sections of your code without permanently deleting it or relying on undo, since you may have to make changes over several days or weeks and repeatedly close and open the files.
There are two ways to write comments. The first way is to write to slash marks, which comments out a line or part of a line:
// This entire line is a comment. SomeCode(); // This is a comment at the end of a line. 4 + 5; // + 6; We've commented out "+ 6;" here to test something.
The second way to write comments is to use a slash and asterisk. This comments out entire sections of code.
/* This line is commented out. This line is commented out as well. The slash and asterisk at the end of this line end the comment. */
Note that these cannot be nested as it will break the code. For example, this works:
/* Commenting out some code.
// Having a double slash comment inside here is fine.
Ending the comment. */
While this would not work:
/* A comment.
/* A comment inside a comment like this would not work. */
Ending the comment. */
As you're working on your own projects, don't forget to comment your code! It makes it easier to read and understand.