This page has been translated automatically.
Programming
Fundamentials
Setting Up Development Environment
UnigineScript
High-Level Systems
C++
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
API
Bounds-Related Classes
Containers
Controls-Related Classes
Core Library
Engine-Related Classes
GUI-Related Classes
Node-Related Classes
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
Rendering-Related Classes
Utility Classes
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

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.

See also

An example can be found in <UnigineSDK>/source/csharp/samples/Api/Scripts/Variables/ 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. Register the variable via Unigine.Interpreter.addExternVariable() (the same as the C++ API Unigine::Interpreter::addExternVariable() function.
Source code(C#)
using System;
using Unigine;

class UnigineApp {
	
	/*
	 */
	[STAThread]
	static void Main(string[] args) {

		Wrapper.init();

		// export a variable and specify a name to access it from Unigine scripts
		Interpreter.addExternVariable("my_variable_int",13);
		Interpreter.addExternVariable("my_variable_float",13.17f);
		Interpreter.addExternVariable("my_variable_double",13.17);
		Interpreter.addExternVariable("my_variable_string","13.17s");
		Interpreter.addExternVariable("my_variable_vec3",new Variable(new vec3(13.0f,17.0f,137.0f)));
		Interpreter.addExternVariable("my_variable_vec4",new Variable(new vec4(13.0f,17.0f,137.0f,173.0f)));

		// init engine
		Engine engine = Engine.init(Engine.VERSION,args);
		
		// enter main loop
		engine.main();
		
		// shutdown engine
		Engine.shutdown();
	}
}

Access from Scripts

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

Source code(UnigineScript)
// variables.cpp

int init() {

	log.message("\n");
	
	log.warning("int:      %s\n",typeinfo(my_variable_int));
	log.warning("float:    %s\n",typeinfo(my_variable_float));
	log.warning("double:   %s\n",typeinfo(my_variable_double));
	log.warning("string:   %s\n",typeinfo(my_variable_string));
	log.warning("variable: %s\n",typeinfo(my_variable_vec3));
	log.warning("variable: %s\n",typeinfo(my_variable_vec4));
	
	// show console
	engine.console.setActivity(1);
	
	return 1;
}

Output

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

Output
int:		int: 13
float:		float: 13.17
double:		double: 13.17
string:		string: "13.17s"
variable: 	vec3: 13 17 137
variable: 	vec4: 13 17 137 173
Last update: 2017-07-03
Build: ()