This page has been translated automatically.
UnigineScript
The Language
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版本。

Expression Class

This class allows to execute a given code fragment at run-time. The arguments of run function initialize the code. There can be up to four arguments of any type passed to the Expression. For example:

Source code (UnigineScript)
Expression e0 = new Expression("133.0 * 133.0");
if(e0.isCompiled()) {
	log.message("%s\n",typeinfo(e0.run()));
}

Expression e1 = new Expression("
	{
		int a,b,c,d;
		return a + b + c + d;
	}
","a,b,c,d");
if(e1.isCompiled()) {
	log.message("%s\n",typeinfo(e1.run(1,2,3,4)));
}

Expression e2 = new Expression("
	{
		string name;
		File file = new File(name,\"rb\");
		int size = file.getSize();
		file.close();
		delete file;
		return size;
	}
","name");
if(e2.isCompiled()) {
		log.message("%s\n",typeinfo(e2.run("test.cpp")));
}
Output
double: 17689
int: 10
int: 1302

Expression Class

Members


Expression (string src, int scope = 0)

Runs the expression pointed to.

Arguments

  • string src - Source buffer pointer.
  • int scope = 0 - 1 to treat the expression namespace as the global; otherwise, 0 (by default).

int getFunction (string name, int num_args)

Returns the ID of the function from the expression namespace. It can be used to call a function by its ID instead of the name (speeds up the function call; it is almost as fast the direct call).

Arguments

  • string name - Name of the function.
  • int num_args - The number of function arguments.

Return value

Function ID.

string getName ()

Get a name used as a namespace name when calling expression methods.

Return value

Namespace name.

variable getVariable (string name)

Returns ID of the variable from the expression namespace. It can be used to pass a variable by its ID instead of the name. It speeds up passing of the varible and can be used, for example, on mobile devices when the performance is crucial.

Arguments

  • string name - Variable name.

Return value

Variable, if it exists; otherwise, 0.

int isCompiled ()

Returns a value indicating if the given expression has been compiled.

Return value

1 if the expression has been compiled; otherwise, 0.

int isFunction (string name, int num_args)

Checks if a given user-defined function exists in the expression namespace.

Arguments

  • string name - Full name of the target function.
  • int num_args - Number of arguments of the target function.

Return value

1 if the function exists; otherwise, 0.

int isVariable (string name)

Checks if a given user-defined variable exists in the expression namespace.

Arguments

  • string name - Name of the target variable.

Return value

1 if the variable exists; otherwise, 0.

variable run ()

Runs the given expression.

Return value

Argument value.

void setName (string name)

Sets a namespace name for the expression that can be used when calling expression methods.

Arguments

  • string name - Namespace name.

void setVariable (string name, variable value)

Set the value of the variable from the expression namespace by its name.

Arguments

  • string name - Variable name.
  • variable value - Variable value to set.
Last update: 2017-07-03
Build: ()