This page has been translated automatically.
UnigineScript
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
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

Functions

A function is a group of statements that is executed when it is called from some point of the program. The following is its format:

Source code (UnigineScript)
type name(parameter1, parameter2, ...) {
	// statements 
}

The example of function usage:

Source code (UnigineScript)
int foo(int a,int b) {
	return a + b;
}

int sum;
sum = foo(3,4); // 'sum' is 7

You can define only a function prototype before using it:

Source code (UnigineScript)
int foo(int a,int b);
foo(5,2);

int foo(int a,int b) {
	return a + b;
}

All functions are in the global scope.

Returning a Value

void

void function represents absence of a variable and is used as a return value in functions that don't return anything.

Source code (UnigineScript)
// this function doesn't return anything
	void func() {
	log.message("func is called\n");
}						

Return Values

Values are returned with the help of an optional return statement. Any data type may be returned, except for vector, map ,and enum .

Functions with the void return type always return 0.

Parameter Passing Mechanism

Passing Arguments by Reference

By default, function arguments are passed by value, so, if you change the value of the argument within the function, it does not affect anything outside of the function. If you wish to allow a function to modify its arguments, you must pass them by reference.

If you want an argument to a function to be always passed by reference, mark it with an ampersand (&) in the function definition.

Source code (UnigineScript)
void foo(int &a,int b) {
	a = 13; // it will affect the variable outside since 'a' is passed as a reference
	b = 14;
}

int a = -1;
int b = 20;
foo(a,b);
log.message("%d %d\n",a,b);

// the result is: "13 20"

Containers and class instances are always passed by reference. For example:

Source code (UnigineScript)
class Foo {
	int v;
};

void foo(int &v) {
	if(v) log.message("%d ",v);
	v = 13;
}

Foo f = new Foo();
foo(f.v);
foo(f.v);

int v[] = (0);
foo(v[0]);
foo(v[0]);

Foo f1[] = (new Foo());
foo(f1[0].v);
foo(f1[0].v);

//the output is: 13 13 13 
					

Passing Containers as Arguments

To pass a vector or a map as an argument, use a proper function declaration, as shown below:

Source code (UnigineScript)
void foo(int a[]) {
	a[13] = 321;
}

int arr[] = ( 1, 2, 3, 4 );
foo(arr);			

Accessing Elements of the Function

The function return value elements can be accessed as follows:

Source code (UnigineScript)
vec3 foo() { return vec3(1.0,2.0,3.0); }
log.message("%f %f\n",foo().x,foo()[0]); // the result is: 1 1
log.message("%f %f\n",translate(1.0,2.0,3.0).m23,translate(1.0,2.0,3.0)[14]); // the result is: 3 3

Note that if the returned value is a class, you need to cast it to its own type.

Source code (UnigineScript)
class Bar {
	string toString() {
		return "bar";
    }
};

Bar bar() { return new Bar() };
log.message("%s\n",Bar(bar()).toString());

Default Argument Values

You can provide default values for function arguments. After that, if you leave an argument empty, its default value will be used. The omitted argument can be indicated with a comma (no whitespace required).

Source code (UnigineScript)
void foo(int a = 1,int b = 2) {
	log.message("%d %d\n",a,b);
}

foo(); // the result is: 1 2
foo(3,4); // the result is: 3 4
foo(3); // the result is: 3 2
foo(,4); // the result is: 1 4

Technique of Using Functions

Function Overloading

You can read about function overloading here.

Using Inline Functions

Functions of the following signature and body will be automaticallly inlined, which gives a noticeable speed boost for user-defined classes:

Source code (UnigineScript)
void get() { return a; }
void set(int b) { a = b; }
void get(int i) { return a[i]; }
void set(int i,int b) { a[i] = b; }

__FUNC__

__FUNC__ preprocessor macro always reports the right function. It never effect performance in any way.

Source code (UnigineScript)
// __FUNC__ == function signature

class MyClass {
	int doSomethingWithNode(Node node) {
		if(node == NULL) {
			log.error("%s: node is NULL\n",__FUNC__);
		}
  
    }

};

// Output: MyClass::doSomethingWithNode(): node is NULL
		
Last update: 2017-07-03
Build: ()