This page has been translated automatically.
Getting Started
Migrating to UNIGINE 2.0
C++ API Migration
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
C++ Plugins
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.

Data Structure Export

Data structures (together with their constructors and accessors, if necessary) can be exported into UnigineScript.

See Also

An example located in the <UnigineSDK>/source/samples/Api/Scripts/Structures/ folder.

Data Structure Export Example

Data structures are exported in the same way as C++ classes:

  1. Create an external class based on your C++ data structure via MakeExternClass().
  2. Add constructors to the external class.
  3. Add methods to the external class.
  4. Register the external class via Unigine::Interpreter::addExternClass().

Here is how different data structures can be exported from C++ into UnigineScript:

Source code (C++)
#include <string>
#include <map>

#include <UnigineEngine.h>
#include <UnigineInterpreter.h>

/*
*/
using namespace Unigine;

/******************************************************************************\
*
* Data structure
*
\******************************************************************************/

/*
 */
typedef struct mydata{
	std::string name;
	ivec3 vec;
	const char *getName() const { return name.c_str(); }
} mydata;

std::map<std::string, mydata> mymap;

/*
 */
void get_data(const Variable &id) {

	void *interpreter = Interpreter::get();
	ArrayMap map = ArrayMap::get(interpreter,id);
	for(std::map<std::string,mydata>::iterator it = mymap.begin(); it != mymap.end(); ++it) {
		map.append(Variable(it->first.c_str()),Variable(interpreter,TypeInfo(TypeID<mydata*>()),&it->second));
	}
}

/*
 */
struct MyVector {
	
	MyVector() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {
		
	}
	
	float x;
	float y;
	float z;
	float w;
};

/******************************************************************************\
*
* Main
*
\******************************************************************************/

/*
*/
int main(int argc,char **argv) {
	
	// export a data structure as an extern class
	ExternClass<mydata> *mydata_export = MakeExternClass<mydata>();
	// add a default constructor
	mydata_export->addConstructor();
	// export structure accessor
	mydata_export->addFunction("getName",&mydata::getName);	
	// register the exported class
	Interpreter::addExternClass("mydata",mydata_export);
	// register the external function
	Interpreter::addExternFunction("get_data2",MakeExternFunction(&get_data,"[]"));
	
	// Fill a map
	mydata data;

	data.name = std::string("map_data_0");
	mymap[std::string("key_0")] = data;
	
	data.name = std::string("map_data_1");
	mymap[std::string("key_1")] = data;
	
	// export a data structure as an extern class
	ExternClass<MyVector> *my_vector = MakeExternClass<MyVector>();
	// add the constructor
	my_vector->addConstructor();
	// register structure accessors
	my_vector->addSetFunction("setX",&MyVector::x);
	my_vector->addGetFunction("getX",&MyVector::x);
	my_vector->addSetFunction("setY",&MyVector::y);
	my_vector->addGetFunction("getY",&MyVector::y);
	my_vector->addSetFunction("setZ",&MyVector::z);
	my_vector->addGetFunction("getZ",&MyVector::z);
	my_vector->addSetFunction("setW",&MyVector::w);
	my_vector->addGetFunction("getW",&MyVector::w);
	// register the external class
	Interpreter::addExternClass("MyVector",my_vector);

	// Initialize the engine.
	Engine *engine = Engine::init(UNIGINE_VERSION,0,argc,argv);
	
	// Enter the main loop and call update() function in the loaded world script.
	engine->main();
	
	// Shut down the engine and call shutdown() function in the loaded world script.
	Engine::shutdown();
	
	return 0;
}

Access from Scripts

After the registration, the exported data structure can be used in UnigineScript just like a usual class.

Source code (UnigineScript)
// my_world.cpp

int init() {
	
	// Get data from a map.
	mydata data2[];
	get_data2(data2);
	foreachkey(string key; data2) {
		log.message("%s: %s\n",key,data2[key].getName());
	}
	
	log.message("\n");
	
	// make extern struct
	MyVector vector = new MyVector();
	
	// set members
	vector.setX(1.0f);
	vector.setY(2.0f);
	vector.setZ(4.0f);
	vector.setW(8.0f);
	
	// get members
	log.warning("MyVector: (%g,%g,%g,%g)\n",vector.getX(),vector.getY(),vector.getZ(),vector.getW());
	
	// delete extern struct
	delete vector;
	
	// show console
	engine.console.setActivity(1);
	
	return 1;
}

Output

The following result will be printed into the console:

Output
key_0: map_data_0
key_1: map_data_1

MyVector: (1,2,3,4,8)
Last update: 2017-07-03
Build: ()