Skip to content

Variables

1. Data types

Throughout the execution of a script, a lot of data is being manipulated all the time. This data can be of different types, each serving a different purpose. The following table lists the data types available in XS and their corresponding keywords.

Data type Keyword Possible values Example
Boolean bool true or false true
Integer int Any whole number between -999999999 and 999999999 42
Float float Any real number between -999999999.999999 and 999999999.999999 3.141592
String string Any sequence of characters enclosed in double quotes "...". See Strings. "Hey there, partner."
Vector vector See Vectors. xsVectorSet(0, 0, 0)

2. Variables

Variables are used to store data to be retrieved and manipulated later.

2.1. Definition

Defining a variable means creating a new variable by giving it a name, a type and a value. Variables must be defined before they can be used.

Syntax of variable definition

type name = value;

2.1.1. Type

This can be any of the data types listed above. This determines what kind of data the variable can store, e.g. a variable of type int cannot store a string.

2.1.2. Name

There are a few rules to follow when naming variables:

  • Names are case-sensitive, e.g. foo and Foo are two different names.
  • Names can only contain letters, numbers and underscores. Diacritics are not allowed.
  • Names must start with a letter or an underscore.
  • Names cannot be the same as a keyword.
  • Variables cannot have the same name as a function, rule or rule group.
  • Variables cannot have the same name as another variable, constant or label in the same scope.

2.1.3. Value

This is the data that the variable will store. The value must be of the same type as the variable.

2.1.4. Examples

Some examples of variable definitions

// Used to store the number of infantry units.
int numInfantry = 0;

// Default gatherer distribution.
float foodGathererPercentage = 0.5;
float woodGathererPercentage = 0.3;
float goldGathererPercentage = 0.2;

// Used to store the position of the main base.
vector mainBaseLocation = xsVectorSet(0, 0, 0);

// Used to determine whether an attack should be launched.
bool shouldAttack = false;

2.2. Assignment

Assigning a variable means changing the value of an existing variable. The variable must have been defined before it can be assigned.

Syntax of variable assignment

name = value;

2.2.1. Examples

Some examples of variable assignments

// ========================================================================
// Definitions (see the assignments further below).
// ========================================================================
int numInfantry = 0;

float foodGathererPercentage = 0.5;
float woodGathererPercentage = 0.3;
float goldGathererPercentage = 0.2;

vector mainBaseLocation = xsVectorSet(0, 0, 0);

bool shouldAttack = false;

// ========================================================================
// Assignments, i.e. we will change the values of the variables we just
// defined.
// ========================================================================

// We found 10 infantry units.
numInfantry = 10;

// We decided to change the gatherer distribution.
foodGathererPercentage = 0.4;
woodGathererPercentage = 0.3;
goldGathererPercentage = 0.3;

// We found the main base at x=30, y=0, z=30.
mainBaseLocation = xsVectorSet(30, 0, 30);

// We decided to attack.
shouldAttack = true;

2.3. Constants

Constants are variables whose value cannot be changed after they have been defined. They are defined in the same way as variables, except that the const keyword is written before the type.

Syntax of constant definition

const type name = value;

2.3.1. Examples

Some examples of constant definitions

1
2
3
4
5
6
7
8
// The number pi is a constant.
const float pi = 3.141592;

// The number e is also a constant.
const float e = 2.718281;

// The speed of light is a constant.
const int c = 299792458;