This page has been translated automatically.
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Landscape Tool
Using Editor Tools for Specific Tasks
FAQ
Programming
Fundamentals
Setting Up Development Environment
Usage Examples
UnigineScript
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
CIGI Client Plugin
Rendering-Related 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.

Variable Export

To use variables from your C++ code in UnigineScript, you need to export them. After that, they will be available on the script side.

  • External variables are read-only.
  • If a value of a registered variable is changed in the C++ code, it instantly changes in the script as well (unlike in case of constants).

See also

An example can be found in <UnigineSDK>/source/samples/Api/Scripts/Variable/ directory.

Variable Export Example

Let's say, you declared a number of variables on C++ side. To export them, you will need to do the following:

  1. Create a pointer to an external variable via MakeExternVariable().
  2. Register the variable via Unigine::Interpreter::addExternVariable().
  3. All variables are exported into a global namespace. To limit the scope of variable, use library namespace.
Source code (C++)
#include <UnigineInterpreter.h>

using namespace Unigine;

int main(int argc,char **argv) {

	int i = 0;
	float f = 0.0f;
	vec3 v3 = vec3(1.0f,2.0f,3.0f);
	vec4 v4 = vec4(0.1f,0.2f,0.3f,0.4f);
	float m[16] = { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f,\
					8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f };
	mat4 mat = mat4(m);
	quat q = quat(0.0f,0.0f,1.0f,0.0f);

	// export a variable and specify a name to access it from Unigine scripts
	Interpreter::addExternVariable("integer",MakeExternVariable(&i));
	Interpreter::addExternVariable("float_point",MakeExternVariable(&f));
	Interpreter::addExternVariable("vector3",MakeExternVariable(&v3));
	Interpreter::addExternVariable("vector4",MakeExternVariable(&v4));
	Interpreter::addExternVariable("matrix4",MakeExternVariable(&mat));
	Interpreter::addExternVariable("quaternion",MakeExternVariable(&q));

	Engine *engine = Engine::init(UNIGINE_VERSION,argc,argv);

	// enter the main loop
	while(engine->isDone() == 0) {
		engine->update();
		engine->render();
		engine->swap();
		// if a variable value is changed after it was registered, in scripts the value will be changed as well		
		i = 42;
	}

	// engine shutdown
	Engine::shutdown();

}

Access from Scripts

After the registration, you can access variables from a script by their registered names:

Source code (UnigineScript)
// my_world.usc
int init() {

	log.message("Integer is %d\nFloat is %f\n",integer,float_point);
	log.message("Vector3 x is %d\nVector3 y is %f\n",vector3.x,vector3.y);
	log.message("Vector4 w is %d\n",vector4.w);
	log.message("Matrix4 m10 is %d\nMatrix4 m11 is %f\n",matrix4.m10,matrix4.m11);
	
	return 1;
}

Output

The following results will be printed into the console after launching the application:

Output
Integer is 0
Float is 0.000000
Vector3 x is 1
Vector3 y is 2.000000
Vector4 w is 0
Matrix4 m10 is 1
Matrix4 m11 is 5.000000

If you reload the world, the integer value that has been changed on the C++ side will appear in the console:

Output
Integer is 42
Float is 0.000000
Vector3 x is 1
Vector3 y is 2.000000
Vector4 w is 0
Matrix4 m10 is 1
Matrix4 m11 is 5.000000
Last update: 2018-08-10
Build: ()