This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
基础
专业(SIM)
UnigineEditor
界面概述
资源工作流程
版本控制
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
使用范例
C#
UnigineScript
统一的Unigine着色器语言 UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Animations-Related Classes
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
VR-Related Classes
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

Data Structure Export

Warning
The scope of applications for UnigineScript is limited to implementing materials-related logic (material expressions, scriptable materials, brush materials). Do not use UnigineScript as a language for application logic, please consider C#/C++ instead, as these APIs are the preferred ones. Availability of new Engine features in UnigineScript (beyond its scope of applications) is not guaranteed, as the current level of support assumes only fixing critical issues.

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>
#include <UnigineInterface.h>

/*
*/
using namespace Unigine;

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

/*
 */
typedef struct mydata{
	std::string name;
	Unigine::Math::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(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 registration, the exported data structure can be used in UnigineScript just like a usual class.

Source code (UnigineScript)
// my_world.usc

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: 2024-04-19
Build: ()