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

UnigineScript Containers

You can work with UnigineScript containers from C# side via Unigine API, that is, with:

See also

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

Container Export Example

Unigine vectors and maps can be accessed via:

  • ArrayVector class and the ArrayVector.get() function for vectors. The ArrayVector::get() C++ API function and the ArrayVector C++ API class have the same behavior.
  • ArrayMap class and the ArrayMap.get() function for maps. The ArrayMap C++ API class and the ArrayMap::get() C++ API function have the same behavior.

C# Side

Create setter and getter functions that receive Unigine containers and handle their elements. Then, export the created functions in order to use them in the script.

Notice
You should specify the array declaration as the last argument of the Function if your array functions receive an array as an argument.
Source code (C#)
Function(my_array_vector_set,"[]")
The postfix of Function shows the number of arguments (up to 8 arguments) and the type of return value:
  • no postfix - void.
  • i - int.
  • d - double.
  • f - float.
  • s - string.
  • v - Unigine.Variable (the same class as the C++ API Unigine::Variable class).
Source code (C#)
using System;
using Unigine;

/*
*/
class UnigineApp {

	/*
	*/
	private static void my_array_vector_set(Variable id,Variable index,Variable val) {
		ArrayVector vector = ArrayVector.get(Interpreter.get(),id);
		vector.set(index.getInt(),val);
	}

	private static Variable my_array_vector_get(Variable id,Variable index) {
		ArrayVector vector = ArrayVector.get(Interpreter.get(),id);
		return vector.get(index.getInt());
	}

	/*
	 */
	private static void my_array_map_set(Variable id,Variable key,Variable val) {
		ArrayMap map = ArrayMap.get(Interpreter.get(),id);
		map.set(key,val);
	}

	private static Variable my_array_map_get(Variable id,Variable key) {
		ArrayMap map = ArrayMap.get(Interpreter.get(),id);
		return map.get(key);
	}

	/*
	 */
	private static void my_array_vector_generate(Variable id) {
		ArrayVector vector = ArrayVector.get(Interpreter.get(),id);
		vector.clear();
		for(int i = 0; i < 4; i++) {
			vector.append(new Variable(i * i));
		}
		vector.remove(0);
		vector.append(new Variable("128"));
	}

	private static void my_array_map_generate(Variable id) {
		ArrayMap map = ArrayMap.get(Interpreter.get(),id);
		map.clear();
		for(int i = 0; i < 4; i++) {
			map.append(new Variable(i * i),new Variable(i * i));
		}
		map.remove(new Variable(0));
		map.append(new Variable(128),new Variable("128"));
	}

	/*
	 */

	private static void my_array_vector_enumerate(Variable id) {
		ArrayVector vector = ArrayVector.get(Interpreter.get(),id);
		for(int i = 0; i < vector.size(); i++) {
			Log.message("{0}: {1}\n",i,vector.get(i).getTypeInfo());
		}
	}

	/* entry point
	*/
	[STAThread]
	static void Main(string[] args) {

		Wrapper.init();
		
		// export functions
		Interpreter.addExternFunction("my_array_vector_set",new Interpreter.Function3(my_array_vector_set),"[]");
		Interpreter.addExternFunction("my_array_vector_get",new Interpreter.Function2v(my_array_vector_get),"[]");
		Interpreter.addExternFunction("my_array_map_set",new Interpreter.Function3(my_array_map_set),"[]");
		Interpreter.addExternFunction("my_array_map_get",new Interpreter.Function2v(my_array_map_get),"[]");
		Interpreter.addExternFunction("my_array_vector_generate",new Interpreter.Function1(my_array_vector_generate),"[]");
		Interpreter.addExternFunction("my_array_map_generate",new Interpreter.Function1(my_array_map_generate),"[]");
		Interpreter.addExternFunction("my_array_vector_enumerate",new Interpreter.Function1(my_array_vector_enumerate),"[]");
		
		// init engine
		Engine engine = Engine.init(Engine.VERSION,args);
		
		// enter main loop
		engine.main();
		
		// shutdown engine
		Engine.shutdown();
	}
}

Access from Script

And here is how the exported custom functions can be used back in UnigineScript:

Source code (UnigineScript)
// array.cpp

/*
*/
int init() {
	
	/////////////////////////////////
	
	log.warning("\naccess to vector\n\n");
	
	int vector[] = ( 0, 1 );
	
	for(int i = 0; i < 2; i++) {
		log.message("vector %d: %s\n",i,typeinfo(my_array_vector_get(vector,i)));
		my_array_vector_set(vector,i,string(i));
		log.message("vector %d: %s\n",i,typeinfo(my_array_vector_get(vector,i)));
	}
	
	/////////////////////////////////
	
	log.warning("\naccess to map\n\n");
	
	int map[] = ( 0 : 0, 1 : 1 );
	
	for(int i = 0; i < 2; i++) {
		log.message("map %d: %s\n",i,typeinfo(my_array_map_get(map,i)));
		my_array_map_set(map,i,string(i));
		log.message("map %d: %s\n",i,typeinfo(my_array_map_get(map,i)));
	}
	
	/////////////////////////////////
	
	log.warning("\nvector generation\n\n");
	
	my_array_vector_generate(vector);
	
	foreachkey(int i; vector) {
		log.message("vector %d: %s\n",i,typeinfo(vector[i]));
	}
	
	/////////////////////////////////
	
	log.warning("\nmap generation\n\n");
	
	my_array_map_generate(map);
	
	foreachkey(int i; map) {
		log.message("map %d: %s\n",i,typeinfo(map[i]));
	}
	
	/////////////////////////////////
	
	log.warning("\nvector enumeration\n\n");
	
	my_array_vector_enumerate(vector);
	
	/////////////////////////////////
	
	// show console
	engine.console.setActivity(1);
	
	return 1;
}

Output

The following result will be printed into the console:

Output
access to vector

vector 0: int: 0
vector 0: string: "0"
vector 1: int: 1
vector 1: string: "1"

access to map

map 0: int: 0
map 0: string: "0"
map 1: int: 1
map 1: string: "1"

vector generation

vector 0: int: 1
vector 1: int: 4
vector 2: int: 9
vector 3: string: "128"

map generation

map 1: int: 1
map 4: int: 4
map 9: int: 9
map 128: string: "128"

vector enumeration

0: int: 1
1: int: 4
2: int: 9
3: string: "128"
Last update: 2017-07-03
Build: ()