Tuesday, October 11, 2011

Using Variables in UnrealScript

UnrealScript supports the following types of variables:
  • byte: One byte can stores values ​​from 0 to 255.
  • int: Stores integer values.
  • bool: Boolean variable, stores true or false.
  • float: Stores fractional numbers.
  • string: Used for storing text.
  • const: Define a constant, its value can not be modified.

Variables can be defined in two places:
  • In Classes: They are known as instance variables and must be defined with the keyword "var" immediately after the class declaration.
  • In Functions: They are known as local variables and must be defined with the keyword "local" at the beginning of the function. Local variables are destroyed when the function is exited.

To make an instance variables editable in the Level Editor, it must be defined as follows: var ().

Variables can not receive values ​​at the time of its definition. To assign default values ​​for instance variables it's necessary a block "defaultproperties".

The value of Constants must be assign at the moment of its definition.

The class below was created only to show the use of variables, it has no real functionality.
class Warrior extends Actor;

var string nameCharacter; 
var int baseAttackValue; 
var bool NPC;
var () string msgEditable;

const warriorBonus = 2.5;

function float calculatePowerAttack() 
{
   local float powerAttack;
   powerAttack = baseAttackValue * warriorBonus;
   return powerAttack;
}

defaultproperties 
{
   nameCharacter = "Ulukai"
   baseAttackValue = 50
   NPC = false
}