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
Usage Examples
C++ API
API Reference
Integration Samples
Usage Examples
C++ Plugins
Migration
Migrating to UNIGINE 2.0
C++ API Migration
Migrating from UNIGINE 2.0 to UNIGINE 2.1
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.

WorldExpression Class

This class is used to create a world expression, that executes an arbitrary expression (a script). An expression can be executed for the other nodes if they are assigned as child nodes of WorldExpression. The child nodes will inherit transformations (if any) of the world expression and will be transformed relative to the pivot point of WorldExpression.

Implementing World Expression Script

A WorldExpression script can be implemented as follows:

  • As a simple sequence of functions' calls. For example:
    Source code (UnigineScript)
    // get the WorldExpression node
    Node node = getNode();
    // apply transformation to the WorldExpression node
    node.setTransform(rotateX(cos(time * 3.0f) * 2.0f) * rotateY(sin(time * 2.0f) * 4.0f) * rotateZ(sin(time * 1.0f) * 8.0f) * translate(0.0f,0.0f,1.1f));
  • As a set of functions and classes. For example:
    Source code (UnigineScript)
    // create resources. This function is called on world load, when not all of the nodes can be loaded					
    void __constructor__() { ... }
    // delete the created resources
    void __destructor__() { ... }
    
    /* 
     */
    
    // implement any required functions
    void print_message() {
    	log.message("Hello from WorlExpression!\n");
    }
     
    // implement a function that the WorldExpression will execute each frame
    void my_update() {
    	...
    	print_message();
    	...
    }
    	       
    // execute the update function
    my_update();
    Notice
    Do not forget to call the function that should be executed each frame in the script.

Inter-Script Communication

Each WorldExpression has its own scope. You cannot directly set or get variables to or from the WorldExpression (as memory corruption occurs).

  • If a WorldExpression calls a function of a world script:
    Source code (UnigineScript)
    // World script
    namespace WorldScriptScope {
    
    	void worldScriptFunc(string arg) {
    		log.message("World script code: %s\n",arg);
    	}
    }
    Source code (UnigineScript)
    // WorldExpression
    engine.world.call("WorldScriptScope::worldScriptFunc","hello from WorldExpression");
  • If a world script calls a function of a WorldExpression:
    Source code (UnigineScript)
    // World script
    namespace WorldScriptScope {
    
    	void worldScriptFunc(WorldExpression expression) {
    		expression.call("worldExpressionFunc","hello from the world script");
    	}
    }
    Source code (UnigineScript)
    // WorldExpression
    void worldExpressionFunc(string arg) {
    	log.message("WorldExpression code: %s\n",arg);
    }

Script Functions

The following internal functions are available within the WorldExpression script:

  • Node getNode() - returns the WorldExpression node.
  • Node getChild(int num) - returns the WorldExpression child node by its number. If the child is not found, returns NULL.
  • int getNumChilds() - returns the number of WorldExpression children.
  • Node getParent() - returns the WorldExpression parent node. If there is no parent, returns NULL.

To get the WorldExpression node and its parent, you can write the following in the WorldExpression script:

Source code (UnigineScript)
Node node = getNode();
Node parent = node.getParent();
Now you can, for example, operate with all of the WorldExpression children as follows:
Source code (UnigineScript)
forloop(int i = 0; getNumChilds()) {
	Node child = getChild(i);
	log.message("The type of the node is: %s\n",child.getTypeName());
}
// this example shows the type of each WorldExpression child

WorldExpression Class

This class inherits from Node

Members


WorldExpression (vec3 size = 1)

Constructor. Creates an arbitrary expression to be executed.
Notice
An expression is executed only when its bounding box is in the viewing frustum or when the camera is positioned within this bounding box, so the size is important.

Arguments

  • vec3 size - Size of the bounding box. This is an optional argument.

variable call (string name)

Calls a world expression function from any other script.

Arguments

  • string name - World expression node name.

Return value

Variable returned by the called expression function.

variable call (string name, variable arg0)

Calls a one-argument world expression function from any other script.

Arguments

  • string name - World expression node name.
  • variable arg0 - Argument for the called function.

Return value

Variable returned by the called expression function.

variable call (string name, variable arg0, variable arg1)

Calls a two-argument world expression function from any other script.

Arguments

  • string name - World expression node name.
  • variable arg0 - The first argument for the called function.
  • variable arg1 - The second argument for the called function.

Return value

Variable returned by the called expression function.

variable call (string name, variable arg0, variable arg1, variable arg2)

Calls a three-argument world expression function from any other script.

Arguments

  • string name - World expression node name.
  • variable arg0 - The first argument for the called function.
  • variable arg1 - The second argument for the called function.
  • variable arg2 - The third argument for the called function.

Return value

Variable returned by the called expression function.

variable call (string name, variable arg0, variable arg1, variable arg2, variable arg3)

Calls a four-argument world expression function from any other script.

Arguments

  • string name - World expression node name.
  • variable arg0 - The first argument for the called function.
  • variable arg1 - The second argument for the called function.
  • variable arg2 - The third argument for the called function.
  • variable arg3 - The fourth argument for the called function.

Return value

Variable returned by the called expression function.

float getDistance ()

Returns the distance at which the expression is not executed.

Return value

The distance value.

string getExpression ()

Returns the executable expression.

Return value

The executable expression.

float getIFps ()

Returns the current constant frame duration used to execute the expression. 0 means that the expression is executed at the same frame rate as the main application window.

Return value

Frame duration (inverse FPS) in seconds (1/FPS).

vec3 getOffset ()

Returns the offset of the center of the world expression's bounding box along the X, Y and Z axes.

Return value

The offset of the expression box center.

vec3 getSize ()

Returns the current size of the WorldExpression bounding box. The size of the bounding box is important as if the bounding box gets outside the viewing frustum, the expression is not executed.
Notice
If the child object transformed according to the expression gets outside the view frustum, but the bounding box is still in the viewing frustum, the playback of the transformation sequence will not stop.

Return value

Size of the bounding box in units.

int isCompiled ()

Returns a value indicating if the given expression has been compiled. It is automatically called on world load or after setExpression() is used.

Return value

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

void setDistance (float distance)

Sets the distance at which the expression is not executed.

Arguments

  • float distance - The distance value. If a negative value is specified, 0 will be used instead.

int setExpression (string src)

Sets the arbitrary expression to be executed.
Notice
The expression passed as an argument must be wrapped with curly braces {} as they define the world expression scope.
If the expression is stored in a file, this file should be included as follows:
Source code (UnigineScript)
expression.setExpression("{\n#include <my_project/scripts/my_expression.h>\n}");
The path should be specified relative to the data directory.

Arguments

  • string src - An executable expression.

Return value

The expression number.

void setIFps (float ifps)

Sets a constant frame duration used to execute the expression. It can be used to decrease the frame rate to get higher performance. 0 means that the expression is executed at the same frame rate as the main application window.

Arguments

  • float ifps - Frame duration (inverse FPS) in seconds (1/FPS). If a too small value is provided, 1E-6 will be used instead.

void setOffset (vec3 offset)

Sets an offset of the center of the world expression's bounding box along the X, Y and Z axes.

Arguments

  • vec3 offset - An offset of the expression box center.

void setSize (vec3 size)

Sets the size of the WorldExpression bounding box. The size of the bounding box is important as if the bounding box gets outside the viewing frustum, the expression is not executed.
Notice
If the child object transformed according to the expression gets outside the view frustum, but the bounding box is still in the viewing frustum, the playback of the transformation sequence will not stop.

Arguments

  • vec3 size - Size of the bounding box in units.
Last update: 2017-07-03
Build: ()