This page has been translated automatically.
UnigineScript
The Language
Core Library
Engine Library
Node-Related Classes
GUI-Related Classes
Plugins Library
High-Level Systems
Samples
C++ API
API Reference
Integration Samples
Usage Examples
C++ Plugins
Content Creation
Materials
Unigine Material Library
Tutorials
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

Variables. Fundamental Data Types

Variables

A variable is the name for a place in the computer's memory where you store some data. To be distinguished from others each variable needs an identifier (a sequence of characters used to name the variable, type, class, function etc.) In UnigineScript variables are declared to be of a type (however, the language uses dynamic typing in fact).

Here is an example of a variable declaration.

Source code (UnigineScript)
int x; // x - identifier
// int - type
Notice
All the variables in UnigineScript are static by default.

For example:

Source code (UnigineScript)
int foo() {
	int i;
	return i++;
}

log.message("%d\n",foo());

// Output: 0,1,2,3...

Fundumental Data Types

A variable can hold the following Fundamental Data Types:

Name Describtion Size Range
int Integer 4 bytes signed: -2147483648 to 2147483647
long Long Integer 4 bytes signed: -2147483648 to 2147483647
float Floating Point Number 4 bytes +/- 3.4e +/- 38 (~7 digits)
double Double Precision Floating Point Number 8 bytes +/- 1.7e +/- 308 (~15 digits)
string A String of Characters

int

Represents an integer number. The initial value is 0.

You can use decimal (base-10), hexadecimal (base-16) or ASCII character notation, with an optional preceding sign (- or +).

Source code (UnigineScript)
int a = -123; // negative number, decimal
int b = 0x1E; // hexadecimal number (equivalent to 30 in decimal notation)
int c = 'N'; // character from the ASCII table (equivalent to 78 in decimal notation)

long

Represents a long integer. This this data type is used when you need a range of values wider than those provided by int. The initial value is 0L.

You can use decimal (base-10) or hexadecimal (base-16) notations with a mandatory l or L postfix and an optional preceding sign (- or +).

Source code (UnigineScript)
long a = -123456789123456L; // negative number, decimal
long b = 0x1EL; // hexadecimal number

float

A single-precision (32-bit) floating point value. The initial value is 0.0f.

You can use any of the following notations, with a mandatory f or F postfix and an optional preceding sign (- or +).

Source code (UnigineScript)
float a = 3.14f; // decimal notation
float b = 1.8e15F; // exponential notation
float c = 9.5E-7F; // exponential notation

double

Represents a double DC2-precision (64-bit) floating point value. The initial value is 0.0.

Source code (UnigineScript)
double a = 3.14; // decimal notation
double b = 1.8e15; // exponential notation
double c = 9.5E-7; // exponential notation

string

A string of characters. The initial value is an empty string.

Notice
Unlike C++, UnigineScript doesn't require null-termination of strings.

Source code (UnigineScript)
string a = "sequence of characters";
string b = "long " + a; // b = "long sequence of characters"

Other Data Types

Matrix

A matrix is a rectangular array of numbers, symbols or expressions, arranged in rows and columns.

Matrix Types

mat4

A matrix of sixteen (4×4) float components. The initial value is the identity matrix:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

Notice
Matrices are column-oriented (OpenGL style).

You can set a matrix the following ways:

Source code (UnigineScript)
/* addressing:
 * m00 m01 m02 m03
 * m10 m11 m12 m13
 * m20 m21 m22 m23
 * m30 m31 m32 m33
 */

mat4 a;

// mat4("m00 m10 m20 m30 m01 m11 m21 m31 m02 m12 m22 m32 m03 m13 m23 m33")
a = mat4("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15");

/*	
 * 0 4  8 12
 * 1 5  9 13
 * 2 6 10 14
 * 3 7 11 15
 */

// mat4(vec3 v)
a = mat4(vec3(1,2,3));

// mat4(float x,float y,float z)
a = mat4(1,2,3);

/* translation matrix:
 * 1 0 0 1
 * 0 1 0 2
 * 0 0 1 3
 * 0 0 0 1
 */

// mat4(quat q)
a = mat4(quat(1,0,0,45));

// mat4(vec3 axis,float angle)
a = mat4(vec3(1,0,0),45);

// mat4(float x,float y,float z,float angle)
a = mat4(1,0,0,45);

/* rotation matrix:
 * 1 0    0   1
 * 0 0.7 -0.7 0
 * 0 0.7  0.7 0
 * 0 0    0   1
 */

// you can use addressing by components
a.m31 = -59.5;
a.m22 += 2.0;

// arbitrary swizzles
a.m00m01m02m03 = vec4(1,2,3,4);
a.m00m10m20m30 = vec4(5,6,7,8);
a.m03m13m23 = vec3(1,2,3);
a.m03m23 = vec3(1,2,0);
a.m00m11m22m33 = a.m30m21m12m03;
a.m00m01m02m03 *= 2.0;
a.m00m01m02m03 += vec3(1,2,3);

// swizzles per row/column
vec4 row = a.row0; // returns the full 1st row: 0 4 8 12
vec4 col = a.col3; // returns the full 4th column: 12 13 14 15

vec3 row_3 = a.row03; // returns the first 3 elements of the 1st row: 0 4 8
vec3 col_3 = a.col23; // returns the first 3 elements of the 3rd column: 8 9 10

vec4 row_4 = a.row13_1; // returns the 3 elements of the 2nd row + 1 in the end: 1 5 9 1
vec4 col_4 = a.col33_1; // returns the 3 elements of the 4th column + 1 in the end: 12 13 14 1
dmat4

A matrix of twelve double components. This is a 4×4 affine transformation matrix with the last row not stored. Instead, the last row is always of the form "0 0 0 1" and its values cannot be written, only read. The initial value is the identity matrix:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

Notice
Matrices are column-oriented (OpenGL style).

You can set a matrix the following ways:

Source code (UnigineScript)
/* addressing:
 * m00 m01 m02 m03
 * m10 m11 m12 m13
 * m20 m21 m22 m23
 * m30 m31 m32 m33
 */

dmat4 a;

/* dmat4("m00 m10 m20 m30 m01 m11 m21 m31 m02 m12 m22 m32 m03 m13 m23 m33")
 * it is not possible to write (m30, m31, m32, m33)
 */
 
 a = dmat4("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15");

/*	
 * 0 4  8 12
 * 1 5  9 13
 * 2 6 10 14
 * 0 0  0  1
 */

// dmat4(vec3 v)
a = dmat4(dvec3(1,2,3));

// dmat4(double x,double y,double z)
a = dmat4(1,2,3);

/* translation matrix:
 * 1 0 0 1
 * 0 1 0 2
 * 0 0 1 3
 * 0 0 0 1
 */

// dmat4(quat q)
a = dmat4(quat(1,0,0,45));

// dmat4(vec3 axis,float angle)
a = dmat4(vec3(1,0,0),45);

// dmat4(float x,float y,float z,float angle)
a = dmat4(1,0,0,45);

/* rotation matrix:
 * 1 0    0   1
 * 0 0.7 -0.7 0
 * 0 0.7  0.7 0
 * 0 0    0   1
 */

// you can use addressing by components
a.m03 = -59.5;
a.m22 += 2.0;
a.m30 = 78; // m30 will not be written


// arbitrary swizzles
a.m00m01m02m03 = dvec4(1,2,3,4);
a.m00m10m20m30 = dvec4(5,6,7,8); // m30 will not be written
a.m03m13m23 = vec3(1,2,3);
a.m03m23 = vec3(1,2,0);
a.m00m11m22m33 = a.m30m21m12m03; // m33 will not be written
a.m00m01m02m03 *= 2.0;
a.m00m01m02m03 += vec3(1,2,3);

// swizzles per row/column
dvec4 row = a.row0; // returns the full 1st row: 0 4 8 12
dvec4 col = a.col3; // returns the full 4th column: 12 13 14 15

dvec3 row_3 = a.row03; // returns the first 3 elements of the 1st row: 0 4 8
dvec3 col_3 = a.col23; // returns the first 3 elements of the 3rd column: 8 9 10

dvec4 row_4 = a.row13_1; // returns the 3 elements of the 2nd column + 1 in the end: 1 5 9 1
dvec4 col_4 = a.col33_1; // returns the 3 elements of the 4th column + 1 in the end: 12 13 14 1

Quaternions

The quaternions are a number system that extends the complex numbers.

Quaternions Type

quat

A quaternion, which components are float numbers. The quaternion is a mathematical construct that represents a rotation in three dimensions. The initial value is (0,0,0,1).

You can set a quaternion the following ways:

Source code (UnigineScript)
quat a;
a = quat(1,2,3,4);

// quat(mat4 m)
a = quat(mat4(1,2,3,4));

// quat(vec3 axis, float angle)
a = quat(vec3(1,2,3),0.2);

// quat(float x, float y, float z, float angle)
a = quat(1,2,3,0.2);

// you can use addressing by components
a.y = -59.5;
a.x += 1.0;

// two-element swizzles
a.xy = a.wz;

// arbitrary swizzles
a.xyz = a.wzx;
a.wzy = vec3(1,2,3);
a.xyzw = a.wzyx;
a.wyzw = vec4(1,2,3,4);
a.xyz += a.wyz;

Enumeration

The enumeration is a set of integer constants with names assigned to them.

Source code (UnigineScript)
// by default, values start from 0 and then increase from element to element
enum {
	KEY_UP, // 0
	KEY_DOWN, // 1
	KEY_LEFT, // 2
	KEY_RIGHT, // 3
};

// you can set a value explicitly
enum {
	KEY_F1 = 100, // 100
	KEY_F2, // 101
	KEY_F3 = 0x1E, // 30
	KEY_F4, // 31
};

// the result of the following will be "go right"
int key = 3;
if (key == KEY_RIGHT) {
	log.message("go right\n");
}
Last update: 2017-07-03
Build: ()