Monday, December 18

Getting Started With Unreal Script

Google+ Pinterest LinkedIn Tumblr +

Getting Started With Unreal Script

By Bryan Thompson

Based on the Dec 2010 beta of UDK

This is unofficial documentation and is not endorsed by Epic Games.

This is a tutorial to get your own Unreal Script to run so you can experiment and toy around with it. First off we are going to learn how to compile and run some scripts which are my own version of the Hourences tutorial scripts. They make the view third person.

I also wrote a short article here about the basics of unreal script.

http://www.bukisa.com/articles/426771_introduction-to-unreal-script

And I wrote a tutorial here on how to get started creating high quality video games.

http://www.bukisa.com/articles/426778_from-gamemaker-to-udk

Italicized sections are not necessary but are added in for clarification

Part 1 Creating the necessary script files

In order to create and edit UnrealScript files (extension .uc) you need a text editor. You could just use notepad but I prefer to use ConText: http://www.contexteditor.org/

You can get the UnrealScript highlighter file for Context here:

http://wiki.beyondunreal.com/Legacy:ConTEXT

Create three new script files MyGameInfo, MyPawn, and MyPlayerController

Here is the breakdown of what to put in each of these files.

—-MyGameInfo:

class MyGameInfo extends UTDeathmatch;

defaultproperties

{

     PlayerControllerClass=class’MyPlayerController’

     DefaultPawnClass=class’MyPawn’

     Name=”Default__MyGameInfo”

}

—MyPawn:

class MyPawn extends UTPawn;

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

{

     local vector start, end, hl, hn;

     local actor a;

     start = Location;

     if (Controller != none)

     {

          end = Location – Vector(Controller.Rotation) * 192.f;

     }

     else

     {

          end = Location – Vector(Rotation) * 192.f;

     }

     a = Trace(hl, hn, end, start, false);

     if (a != none)

     {

          out_CamLoc = hl;

     }

     else

     {

          out_CamLoc = end;

     }

     out_CamRot = Rotator(Location – out_CamLoc);

     //glitch fix for after recovering from FeignDeath

        MyPlayerController(Controller).SetBehindView(true);

        return true;

}

//this is an overwrite function example

exec simulated function FeignDeath()

 {

  super.FeignDeath();

  `log(“feign death was called in mypawn”);

 }

defaultproperties

{

     Begin Object Name=WPawnSkeletalMeshComponent

          bOwnerNoSee=false

     End Object

     Name=”Default_MyPawn”

     //double the normal jumpz just for fun

     JumpZ=+00840.000000

}

—-MyPlayerController:

class MyPlayerController extends UTPlayerController;

event Possess(Pawn inPawn, bool bVehicleTransition)

{

     Super.Possess(inPawn, bVehicleTransition);

     SetBehindView(true);

}

//this is an example function for recieving custom input.

exec function yeahbaby()

 {

  `log(“yeahbaby called”);

   Player.Actor.clientMessage(“I Key PRESSED”);

   Pawn.DoJump(true);

 }

defaultproperties

{

     Name=”Default_MyPlayerController”

}

Part 2 Getting Your scripts to compile

To get your Unreal scripts to compile first create a new folder in C:\UDK\UDK-2010-11\Development\Src (at least, that’s where Src is on my computer)

Name it MyGame

Create another folder inside that one and name it Classes

Move the script files you created earlier into the Classes folder you just made.

Hooray Our Scripts are now in place! Now we just need to change an ini file to tell the compiler where to find them.

There are actually four different working ways to do this that I know of (this can lead to confusion because different tutorials tell you to use different methods and if you try to use more than one method at once you will get a compile error). But don’t worry about that, just follow my orders.

All your ini files are found in this folder C:\UDK\UDK-2010-11\UDKGame\Config

Open up the DefaultEngine.ini file. You may have to adjust its properties so that it isn’t read only. Find the section

[UnrealEd.EditorEngine] and under the last line in that section add +EditPackages=MyGame

Save the Ini File

Now delete the UDKEngine.ini file. This will force the compiler to make a new UDKEngine.ini file based on our edited DefaultEngine.ini file.

Now to compile your scripts! Open up Unreal Frontend. You can find it here. C:\UDK\UDK-2010-11\Binaries\UnrealFrontend or in the start menu but you might also want to make a desktop shortcut.

Unreal Frontend is your friend (Hey that rhymes :D).  Notice the tabs at the top. You can start up the UDK Game by clicking on the “Launch” Tab. You can start up the UDK Editor by clicking on the “Editor” Tab. You can also do a full recompile by clicking on the “Make” tab. You can also make sure that the log runs in the background of the editor by checking the Show Log box down at the bottom before clicking the Editor tab. This can be useful for testing.

For now click on the down arrow beside “Make” and select full recompile. This might take a few minutes so sit back and have a donut. Everything should compile properly assuming your scripts don’t have any stupid errors in them like missing semi-colons (Happens to me all the time :P). IF everything compiles properly you shouldn’t get any errors or warnings and somewhere near the bottom you should see a line like this —————— MyGame-Release—————-.

In the future you won’t have to do a full recompile all the time. Just Click on the Editor tab to open it up and if you have changed your scripts it will ask you to rebuild, click yes and this will recompile just the scripts that have been changed. This will usually go a lot faster.

Part 3 Seeing your scripts in action (testing them out)

At this point most tutorials would try to show you how to replace the default game mode for UDK Game (the “Launch” Tab in Unreal Frontend). This isn’t most tutorials.

Instead, we are going to test our game mode, and all associated scripts, by playing it in the Editor or by using Run to launch UDK Game with our custom parameters. The hardship of using UDK Game for testing purposes is that it is quite complicated to set up and even after the setup is complete it will by default  be missing a proper Exit function (I’ve had to exit out of testing sessions on UDK Game many times using Task Manager :P.)  If you do wish to set up the default UDK Game for testing purposes I would recommend going through the hourences tutorial (of course changing it slightly to account for its outdatedness).

http://www.hourences.com/an-entire-simple-udk-game/

  1. Testing your game the easy way

Open up the Editor by clicking on the “Editor” Tab in Unreal Frontend.

Right now I am just going to assume that you have a basic level set up that runs smoothly and that you think you can use for testing purposes.

If you don’t, set up one now, you may want to go easy on the geometry and static meshes to keep the framerate high. You can find a tutorial on 3DBuzz.com.

Open up your testing level and then go up to View drop-down menu and open up the World Properties window. Expand World Info and where it says “Game Type for PIE” select MyGameInfo from the drop down menu (PIE stands for Play in Editor).

  Now save your level.

  Now right click anywhere on the floor and click “Play from here”

You should now be playing the game mode defined by MyGameInfo and the other two script files.

2. Testing your game the professional way

This will give you a much more accurate test for performance since you won’t have the editor running in the background. First you will need an exit function so that you don’t have to exit the game using task manager. Put this function in your MyPlayerController class:

exec function MyQuitFunction()

 {

  Consolecommand(“Quit”);

 }

Create a reference to your new exec function in the ini files using part 4 below as a guide.

Now open up Run in the start menu and enter this line of text. And hit Ok

C:\UDK\UDK-2010-11\Binaries\UDK MyCustomMap?game=MyGame.MyGameInfo

You may have to edit this slightly so that the directory is correct, MyCustomMap is the name of your Map, and MyGame.MyGameInfo is the name of your game mode.

Otherwise your game should be running rather nicely.

Part 4 Setting up custom keyboard input.

If you were to open up the script file MyPlayerController you might notice the following function:

exec function yeahbaby()

 {

  `log(“yeahbaby called”);

   Player.Actor.clientMessage(“I Key PRESSED”);

   Pawn.DoJump(true);

 }

I declared this function myself and I’m rather proud of it :D.

I set up this function to be called by the I key on the keyboard. When the I key is pressed it writes a message to the log, then writes a message to the screen, then makes the player’s pawn jump. Thank you very much.

To set up the I key to call this function we need to go back to our ini files. Open up C:\UDK\UDK-2010-11\UDKGame\Config\DefaultInput.ini

Search for feigndeath until you find the line that says:

.Bindings=(Name=”F”,Command=”GBA_FeignDeath”)

Insert this line right below it:

.Bindings=(Name=”I”,Command=”yeahbaby”)

Save the ini file and delete UDKinput.ini so that the compiler is forced to make a new one from DefaultInput.ini Now the I key should call our function when pressed, its that simple.

Suppose you wanted to make the I key also do something when it is released.

To do this just replace the line we inserted earlier with:

.Bindings=(Name=”I”,Command=”yeahbaby | OnRelease nahbaby”)

Now just declare another function in MyPlayerController like the one below.

exec function nahbaby()

 {

  //put whatever code you want here

 }

That’s all there is to it.

Bonus: Part 5 Some fun facts about Unreal Script

I strongly recommend skimming over the important parts in the Unreal Script 3 reference

http://udn.epicgames.com/Three/UnrealScriptReference.html

But here are some of my personal notes anyway

Names

All names in Unreal Script are case in-sensitive. Yippee! That means you don’t need to worry about capital letters when referring to classes, functions, or variables. LoCation is exactly the same as location. Of course you still need to worry about capitals in strings but that should be obvious. Its worth noting that the names of bool variables usually have a lower class b at the beginning of them, for example bCanClimb.  However this is strictly a programmers preference.

All of Unreal Script is organized into classes which are like objects in Gamemaker.

Don’t get confused by this.

Class in UnrealScript or C++ is the same as object in Gamemaker

Object in UnrealScript of C++ is the same as Instance in Gamemaker

Classes

Every class in unreal script must have its own script file, each Unreal script file contains the information for just that class, this helps a ton with organization. Every Class in unreal script extends from its parent class. This gives it all of its parent’s functions, variables, and properties unless rewritten in the child class. Most classes ultimately extend from the Actor class which extends from Object, the base of all classes.

A class declaration goes at the top of its script file and might look like this.

Class Charizard extends FirePokemon;

Congratulations. You just made a class named Charizard, you don’t even half to type anything more in the script file. If you don’t, it will just behave exactly the same as class FirePokemon. Of course you may want to overwrite some of its functions or defaultproperties to make Charizard look and behave a tad bit differently than the default FirePokemon.

Functions

Functions are declared in classes, When you declare a function you fill it with a bunch of code and then later when you call the function it executes all of that code. This makes it so that you don’t have to type out commonly needed code over and over again and makes things very organized. You will find that unreal script programmers like to use functions extensively, in fact its very rare for them to use anything else :P. Here are some example function declarations and calls. Declaring a function looks like this.

Function givemefullhealth()

{

  Health=SuperHealthMax;

}

Calling the previous function looks like this.

Givemefullhealth();

Functions can also have a return type and/or parameters. This allows you to adapt a function’s internal code to specific situations. Here is a declaration of a function with a return type and parameters.

Function bool bLastValueIsGreater(float FirstValue, float SecondValue)

{

If SecondValue > FirstValue

{

 Return true;

}

Else

{

  Return false;

}

}

It’s worth noting that the return keyword immediatily ends the execution of a function

Here is an example of calling the above function for an if statement.

If bLastValueIsGreater(superhealthmax,health)

{

  Health=superhealthmax;

}

When you declare a function in a class that has already been declared in a parent of that class your new functionality overrides and replaces the original code of that function. However you can still use the parent’s code for that function with thesuper keyword.

Heres an example:

Class mypawn extends UTPawn

exec simulated function FeignDeath()

{

  super.FeignDeath();

  `log(“feigndeath was called in mypawn”);

}

In the above example we overwrite the original functionality of the FeignDeath() function. However we choose to still keep parent’s functionality for that function by using the super keyword. We just add our own little line of code after it to write a message to the log. Now you might be wondering why we put the keywords exec and simulated before the function declaration. Believe it or not there are special types of functions that need special keywords in front of them in order to work. All you need to know right off the bat is that if the function declaration in the parent class has special keywords then the overwritten function needs the same ones!

Share.

About Author

Leave A Reply