Sniper's Paradise!


Functions in UnrealScript

This page will attempt to explain the basic facts about functions in order to get started. For a more in depth explanation when you are more confident with UnrealScript you can simply search the Unreal Wiki or UDN for any topics about Functions.

Functions always start with the word “function”, this is always followed by the optional return variable type, which in turn is always followed by the function name, then the functions parameters which are always enclosed within parenthesis (brackets). For example.

	function <optional variable type> MyFunction(parameters)

	//Examples...
	function int MyFunction(Actor Other)

	function MyFunction(Actor Other)

	function MyFunction()

Declaring a Function

Function declarations are very simple. For example.

//This function has no return value, an no extra paramters.
//This function has been declared, we have informed the compiler that we
//intend to use it, for example maybe in a sub-class .It has not been defined yet.
function MyFirstFunction();

Defining a Function

To define a function, we just add some code to the body. The body of the function is always enclosed within square brackets. For example.

function MyFirstFunction()
{
	//Code goes here.
}

Functions that have the optional return variable type should always return something. Here is a good and bad example.

//Bad Example.
function int MyFirstFunction()
{
	//This function expects an integer to be returned.
	//But there is no return command!
}

//Good Example.
function float MyFirstFunction()
{
	//This function expects a float value to be returned.
	//We return 1.000000.
	return 1.000000;
}

The parameters can be used to pass information between functions. For example.

//A global integer variable called "FinalValue".
var int FinalValue;

function GetValue()
{
	//This function is calling the MultiplyByTen function.
	//We pass the integer "2" into the function.
	MultiplyByTen(2);
}

//This MultiplyByTen function expects an integer to be passed into it.
//x is set to the value of the integer passed into it.
function MultiplyByTen(int x)
{
	//"x" now has the value of 2. So we can set FinalValue to "x" * 10 (2 * 10).
	FinalValue = x * 10;
	//So FinalValue now has a value of 20.
}

//This is a simplified example of how you could give health to a player by passing variables.
function GiveHealth(int HealAmount)
{
	Health = Health + HealAmount;
}

Probe Functions

Probe functions are special functions that already exist within the game. These functions are called whenever something happens that triggers them. For example if two actors touch each other then the “Touch” function is called. Probe functions can be disabled/enabled, or ignored while in a state. By default they are all enabled. These always start with the keyword “event” instead of the keyword”function”, just for ease of reference more than anything else as event functions that begin with the keyword”function” will work fine. Here are some of the basic and most commonly used probe functions. These all originate in the Actor class. For a more complete list check out this page Probe Function and the Actor class.

Function Description
AnimEnd Called when the actors current animation has ended.
Attach Called by SetBase() when another actor (“Other”) has been attached to the actor.
BaseChange Called when the actor’s Base changes
Bump Called when the actor touches is touched by another actor. The actor must have bCollideActors=true and bBlockActors=true.
Destroyed Called when this actor is destroyed.
Detach Called by SetBase() to notify the actor that a previously attached actor (“Other”) has been detached from it.
EncroachedBy Called when the collision on this actor and another is encroaching on eachother. Both bCollideActors and bBlockActors must be truth.
GainedChild Called when another actor sets this actor to its Owner through the SetOwner() or the Spawn() functions.
HitWall Called when the actor hits a wall. The actor must have bCollideWorld=true.
Landed Called when the actor stops falling. If you want to receive the HitWall() event instead of Landed() then bBounce must be True.
LostChild The opposite of GainedChild(), called when another actor is no longer owned by this actor. Through SetOwner() or the other actor was destroyed.
Tick Called when time passes. The actor mus thave bStatic=false. DeltaTime is the time in seconds that has passed since the last tick. Child actors tick after their owners have been ticked. Tick speeds are FPS dependant.
Timer First called with SetTimer(), this function is executed when the amount of time that was specified in SetTimer has passed. The actor must have bStatic=false.
Touch Similar to Bump, is called when the actor touches another actor (when the collision cylinders collide). The actor must also have bCollideActors=true
Trigger Called when this actor’s “Event” property is triggered.
ZoneChange Called when the actor changes zone.

Overriding Functions

Functions, (unless declared as being native) can be overridden by any child classes. This allows you to create new classes based on other ones without having to re-write a whole lot of code. For example.

//The PlayVictory function is first declared in the Monster class.
//The Pupae class defines it and makes it play the "Stab" animation.
function PlayVictory()
{
	//Simplified function from Pupae class.
	PlayAnim('Stab', 1.0, 0.1);
}

//What if we want to extend the Pupae class but make it play a different animation when PlayVictory is called?.
//Or what if our new monster has no animation called "Stab", we can simply override the function.
function PlayVictory()
{
	//Execute our new code.
	PlayAnim('Bite');
	//Our function overrides the ones in Pupae and Monster.
}

Extending Functions

What if we need to extend an already existing function in a parent class but don’t want to override it. We do that by using the special “Super” command. For example, when something is destroyed we need to make sure any effects we made are destroyed. The parent classes may also have things that need destroying. Heres how to do that.

function Destroyed()
{
	//Check if the effect still exists.
	if(MyEmitter != None)
	{
		//It still exists so destroy it.
		MyEmitter.Destroy();
	}
	//Call the Destroyed function in the parent class.
	Super.Destroyed();
}

What if we want to override a function so that the immediate parent version is not called, but the parent of that parent has important code that we need. We can do that by specifying which class the Super command should point to. If you try to use Super to call a function in a class that is not in a parent class you will most likely crash the game. For example.

//In this example our class extends the Monster class.
class MyMonster extends Monster;

function Destroyed()
{
	if(MyEmitter != None)
	{
		MyEmitter.Destroy();
	}
	//We want to skip the destroyed function in the class Monster, but we want
	//to call it in the class xPawn (which is the parent of Monster).
	Super(Pawn).Destroyed();
}

//Note that the parent function called with the Super command has an opportunity
//to also use the Super command. So in this example the destroyed function in Pawn
//will execute its code then call the destroyed function in Pawn (UnrealPawn does not
//override it) which in turn executes its code and calls the destroyed function in Actor.

Note – Disable probe functions if you won’t be needing them anymore, especially Tick and Timer.



Spam Killer

Back To Top
2005 Sniper's Paradise
All logos and trademarks are properties of their respective owners.
Unreal™ is a registered trademark of Epic Games Inc.
Privacy Policy
Website by Softly
Powered by RUSH