Sunday, February 27, 2011

UDK Programming Tutorial - Programming a Gametype


Setting up the Code


Requirements:

UDK 2-2011 is installed

WOTgreal is installed and has been set up correctly.

Know a little about 3D maths (enough to keep up).


Stage 3: Finally Programming something!


Note: It is VERY bad practice to overwrite existing UDK script files. The previous setup has been made so that the UDK will compile your files. To use and modify existing UDK files you extend them.

Note # 2: Some of the following code for the tutorial has been split up to multiple lines due to word wrap. Take into account that some lines that appear to be two lines, should actually be one line.

Create four text files in the folder C:\UDK\UDK-2011-02\Development\Src\Game\Classes that you made last tutorial. Name these text files

Gamesearch.uc

Gamesettings.uc

Gametype.uc

GametypePawn.uc

You must have the option “Hide extensions for known file types” unchecked to be able to do this step properly. This can be found in the following locations:

Windows 7 - Start->Control Panel->Appearance and Personalisation-> Folder Options-> View

Windows XP - Start->Control Panel-> Classic View-> Folder Options->View


Now open WOTgreal, you'll notice it is completely empty. You'll need to press UDK-> Create package/class tree.


The package and class tree should then appear on the left hand side of the screen. Ex

pand the Game section, and open the four files that we just made. In each file, write the code that follows.


Gamesearch.uc


class Gamesearch extends UTGameSearchDM;


Gamesettings.uc


class Gamesettings extends UTgameSettingsDM;


And then, because we want to actually show something, we'll add more than just extends to these files.


Gametype.uc


class Gametype extends UTDeathmatch;


defaultproperties

{

DefaultPawnClass=class'GametypePawn'

}


defaultproperties is a section of code that you will find located in most files. It is the place where you initialise global variables. Global variables are defined at the start of your code, just under your class declaration and before any function declarations (more on that later).

DefaultPawnClass is a Pawn variable declared in the GameInfo.uc class (C:\UDK\UDK-2011-02\Development\Src\Engine\Classes\GameInfo.uc)


Now we need to fill in the file called GametypePawn (I had to name it like this because Pawn and GamePawn are both existing files in the UDK, and they could get mixed up with ugly results), for the Gametype to reference. We're going to create a basic 3rd person camera.


GametypePawn.uc


class GametypePawn extends UTPawn;


simulated function bool CalcCamera(float fDeltaTime, out vector out_CamLoc, out rotator out_CamRot, out float out_FOV)

{

local vector cam_loc;

Mesh.SetOwnerNoSee(false);

cam_loc = Location - Vect(1, 0, 0) * 196.f;

out_camLoc = cam_loc;

out_CamRot = Rotator(Location - out_CamLoc);

return true;

}


simulated function bool IsFirstPerson()

{

return false;

}


DefaultProperties

{


}


CalcCamera is an inherited function in which we can override the camera's position and angle from the player. This is a very glitchy way of making a 3rd person camera, but it's also very easy, and doesn't require rewriting the Camera class.

The function accepts fDeltaTime as a parameter, and outputs out_CamLoc, out_CamRot and out_FOV as return values. Returning true will tell the camera that it has been overridden, and to use the new values supplied from the player class.


local vector cam_loc;” declares a local parameter. Any function-specific parameters that you wish to declare in Unreal Script must be declared at the beginning of the function, and must be keyworded “local”. I can't stress this enough, I have had soo many silly hard-to-figure-out errors because of this. The variable will hold the modified camera location (thus the name cam_loc) before it is assigned to out_camLoc to be returned.


Mesh.SetOwnerNoSee(false)” is VITAL if you actually want to see your character and not a disembodied gun ;). I put it here rather than in default properties because it is reset every time a character faints. It can (however) be set in default properties by using the following lines of code (which I will elaborate on later):


Begin Object Name=WPawnSkeletalMeshComponent

bOwnerNoSee=false

End Object


cam_loc = Location - Vect(1, 0, 0) * 196.f;” sets the camera to be 196 unreal units away from the player. The camera's location is (169, 0, 0) units away from the player, and is always at that angle.


out_camLoc = cam_loc;” assigns the value to the return variable (normally we'd tweak the value a bit more before this point).

out_CamRot = Rotator(Location - out_CamLoc);” Make the camera point at the player (kinda handy).


Now the code is finished! Compiling it and playing it in a level is in the next step.


Next Tutorial: Compilation!

No comments:

Post a Comment