This page has been translated automatically.
Setting Up Development Environment
High-Level Systems
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
Double Precision Coordinates
Core Library
Engine Classes
Node-Related Classes
Rendering-Related Classes
Physics-Related Classes
Bounds-Related Classes
GUI-Related Classes
Controls-Related Classes
Pathfinding-Related Classes
Utility Classes
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.


Templates are used to generate different code chunks, functions and classes. Each template starts with the template keyword and sets an identifier and a code chunk, a function or a class, by which this identifier will be replaced when using the template.

The templates should be used instead of the #define preprocessor directive. The main differences from #define are the following:

  • The templates support syntax highlighting.
  • The templates work relative to a scope (while #define has the global scope).

See Also

  • Description of the #define and # preprocessor directives
  • Samples located in data/samples/systems/noise_02.cpp and data/samples/widgets/ui_01.cpp

Generating Code Chunks

A template that is used to generate a code chunk has the following syntax:

Source code(UnigineScript)
template template_name<ARG_NAME> { 
The ARG_NAME argument is used in the code chunk. It will be replaced by a value that is specified when using the template.
You can specify several arguments for the template, if it is required.
The following example demonstrates how to declare the template with 2 arguments and then generate a code from it:
Source code(UnigineScript)
template print<STRING,DIGIT> {
	log.message("%s, ",STRING);

// generate a code chunk

// The following code is generated:
// log.message("%s, ","int: 12");
// log.message("%d\n",17);
// log.message("%s, ","12");
// log.message("%d\n",17);

To turn a template argument into a string, use # before the argument:

Source code(UnigineScript)
template print<DIGIT> {


// Output:
// 17
// text_17

Generating Functions

Also it is possible to create a template that generates a function instead of the code chunk. This function will replace the identifier that is set by a template. The syntax of the function template is the following:

Source code(UnigineScript)
template template_name<ARG_NAME> void ARG_NAME { 
By using such template, you can generate functions with any names.

To declare several functions in one template, simply enclose the functions in curly braces:

Source code(UnigineScript)
template template_name<ARG_NAME> { 
	void func1_ ## ARG_NAME { function_code; }
	void func2_ ## ARG_NAME { function_code; }
Here the ## operator is used to concatenate 2 parts of a function name, so you cannot put it as the first or the last token in the function declaration.
The white spaces before and after the ## operator are required.

The following examples demonstrate how to:

  • Declare a template with 3 arguments and then generate a function from it:
    Source code(UnigineScript)
    template sum_template<NAME,A0,A1> void NAME() {
    	log.message("%s\n",typeinfo(A0 + A1));
    // generate the sum() function that adds 1 to 2
    // call the generated function
    // Output: int: 3
  • Declare several functions in one template and then use this template in the code in order to generate class member functions:
    Source code(UnigineScript)
    template setget<TYPE,NAME,VALUE> {
    	void set ## NAME(TYPE v) { VALUE = v; }
    	TYPE get ## NAME() { return VALUE; }
    class Foo {
    	int a,b,c;
    	// generate the following functions:
    	// void setA(int v) { a = v; }
    	// int getA() { return a; }
    	// void setB(int v) { b = v; }
    	// int getB() { return b; }
    	// void setC(string v) { c = v; }
    	// string getC() { return c; }
    	void info() { log.message("%d %d %s\n",a,b,c); }
    Foo f = new Foo();
    // call the generated member functions
    f.setC("Sample string");;
    // Output: 12 13 Sample string

Generating Classes

The syntax of the template that generates a class is the following:

Source code(UnigineScript)
template template_name<ARG_NAME> {
	class ARG_NAME {
By using such template, you can generate classes with any names. Also you can use the ## operator to construct a class name, for example:
Source code(UnigineScript)
template my_class<NAME> {
    class My ## NAME {
        My ## NAME() {
            log.message(__FUNC__ + ": called\n");
        ~My ## NAME() {
            log.message(__FUNC__ + ": called\n");

// generate the Foo class

MyFoo f = new MyFoo();
delete f;
Last update: 2017-07-03
Build: ()