Report article RSS Feed UnrealScripter #1

UDK is a powerful tool that allows making all types of games. The problem is that we need to be able to program it ! This tutorial has a simple aim : helping to understand the UnrealScript programming.

Posted by belzaran on Apr 28th, 2010
Intermediate Client Side Coding.

UDK is a powerful tool that allows making all types of games. The problem is that we need to be able to program it ! This tutorial has a simple aim : helping to understand the UnrealScript programming.

All I will explain here is for solo games only. But this will maybe and often work for multiplayer games. This first tutorial will allow you to create a SequenceAction useable in Kismet. This SequenceAction will display a message (subtitles for example) when we want to. You can use UIScenes for this, but using a SequenceAction will be simpler later. 

First thing to do : create a blank SequenceAction : 

uscript code:
class TT_SeqAct_Message extends SequenceAction;

defaultproperties
{
}

 

We save the file with the name : TT_SeqAct_Message.uc. As this name say it, a SequenceAction make something. We will use the event Activated() to put in what we want to do (here, displaying a message).

To display a message, we will use the function BroadcastLocalizedMessage() that send messages to all players. As we are in a solo game, we are sure to receive it! (you can use the ReceiveLocalizedMessage() to send a message to a single player or a particular group of players). The function BroadcastLocalizedMessage() is defined in the Actor.uc class. We have to use WorldInfo to use this function (look at the Actor.uc file to understand why). 

uscript code:
class TT_SeqAct_Message extends SequenceAction;

event Activated()
{
local WorldInfo WI;
WI = GetWorldInfo();
WI.BroadcastLocalizedMessage(class'TT_Message');
}

defaultproperties
{
}

 

We introduce here the variable WI, that corredpond to the WorldInfo. Then, we define it with the function GetWorldInfo(). And finally we can send our message ! All the actions here have to be known, because they are reused in a lot of cases :

1 – define a variable
2 – give a value to this variable
3 – exploite this variable 

The next step is to create the TT_Message class. We create a file named TT_Message.uc with the classic structure :

uscript code:
class TT_Message extends LocalMessage;

static function string GetString(
optional int Switch,
optional bool bPRI1HUD,
optional PlayerReplicationInfo RelatedPRI_1,
optional PlayerReplicationInfo RelatedPRI_2,
optional Object OptionalObject
)
{
Return "Trash Team !"
}

defaultproperties
{
Lifetime=5
DrawColor=(G=255,R=255,B=255)
PosY=0.9
FontSize=2
}

The GetString() function is the most imporant in the LocalMessage class. It defines what will be displayed. Some options can be used. Some of them use the PlayerReplicationInfo (informations
about the player as health, armor, speed…). We just want to display a simple sentence. In the class properties, some little things can be used to customize our message :
 

Lifetime : the time the message will be displayed (here : 5 secondes)
DrawColor : the color of the text (here : blank)
PosY : Vertical position of the message (here : on the bottom of the screen)
FontSize : Size of the font (here : middle size)

We will optimize our sequence. We will make a sequence that will be used to display many different messages. We consider a game with two races of characters. We can choose to be a troll or an human. Each race has a number. For example:

Human = 0
Troll = 1

We will display two different messages by races. We will use the switch option of the GetString() function of the LocalMessage() class. When I send a message to a player, I can define what message is sent. For example, BroadcastLocalizedMessage(class’TT_Message’,2) send the second message of the TT_Message class. To define the message :  

uscript code:
class TT_Message extends LocalMessage;

static function string GetString(
optional int Switch,
optional bool bPRI1HUD,
optional PlayerReplicationInfo RelatedPRI_1,
optional PlayerReplicationInfo RelatedPRI_2,
optional Object OptionalObject
)
{
if (switch == 0)
{
return "You are an human !"
}
else if (switch == 1)
{
return "You are a troll !"
}
else
{
}
}

defaultproperties
{
Lifetime=5
DrawColor=(G=255,R=255,B=255)
PosY=0.9
FontSize=2
}

 

Now we will define in our sequence what message to send. The aim is to be able to define it in
Kismet what race have to receive this message (race 0 or race 1). We add a node to our class TT_SeqAct_Message with the line : 

 

uscript code:
<span>VariableLinks.Add((ExpectedType=class'SeqVar_Int',LinkDesc="Race",PropertyName=Race,bWriteable=True))
</span>

 
VariableLinks.Add : add a node
ExpectedType : what type of variable is needed (here : integer)
LinkDesc : name of the variable (here : race)
PropertyName : name that will be display in Kismet for the node (here : race)

We have to define the variable race as an integer :

uscript code:
Var() int Race ;

The Race variable will be defined by Kismet by connecting an integer to the node Race of our sequence. To call the message, we will use the command : 

uscript code:
WI.BroadcastLocalizedMessage(class'TT_Message', Race) ;

 

So finally, the entire class : 

uscript code:
<span>class TT_SeqActMessage extends SequenceAction;

var() int Race;

event Activated()
{
local WorldInfo WI;
WI = GetWorldInfo();
WI.BroadcastLocalizedMessage(class'TT_Message', Race);
}

defaultproperties
{
bCallHandler=false
ObjName="Play Message"
ObjCategory="Trash Team"
VariableLinks.Add((ExpectedType=class'SeqVar_Int',LinkDesc="Message",PropertyName=Message,bWriteable=True))
}
</span>

ObjName : name of the sequence that will be shown in Kismet
ObjCategory : the folder of the sequence 

Here is the principle of our sequence:

 UnrealScripter #1

You can use the same principle to make SequenceCondition for example. You just need to open an existing SequenceCondition to see what is different.

Here is the result in Kismet : 

UnrealScripter #1

In the next tutorial, we will learn how to define the player race and use it in our SequenceAction !

 

Post comment Comments
Picklock
Picklock Jan 8 2011, 9:40am says:

cool, thanks!

+1 vote     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Groups
Indie Devs
Indie Devs Hobbies & Interests group with 1,098 members
UDK Developer's Group
UDK Developer's Group Hardware & Tech group with 688 members