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++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
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
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

Sharing Data between the Logic System Components

Sometimes it is necessary to share data between the elements of the Logic System: WorldLogic, SystemLogic, EditorLogic classes. Basically, there are three ways to do that:

  1. Using a global variable.
  2. Using a script variable.
  3. Using a member variable of AppSystemLogic, AppWorldLogic or AppEditorLogic classes.

This example illustrates all three ways of sharing data, showing:

  • how to declare and use a global variable;
  • how to declare a variable in the world script and use it in the methods of the AppSystemLogic class;
  • how to use member variables of AppSystemLogic class in the methods of AppWorldLogic and vice versa.

Using a Global Variable#

We can declare a global variable in a separate file, so we add a couple of files to our project:

  • global.h file.
    Source code (C++)
    // global.h
    
    #ifndef __GLOBAL_H__
    #define __GLOBAL_H__
    
    extern int global_var;
    
    #endif // __GLOBAL_H__
  • global.cpp file.
    Source code (C++)
    // global.cpp
    
    #include "global.h"
    
    int global_var = 33;

Now in order to use this variable in the methods of AppSystemLogic, AppWorldLogic or AppEditorLogic classes we just need to include the global.h in the corresponding header or implementation file.

Using a Script Variable#

We can declare a variable in our world script file (UnigineScript) named <YOUR_PROJECT_NAME>.usc.

Source code (UnigineScript)
// <YOUR_PROJECT_NAME>.usc
#include <core/unigine.h>
// This file is in UnigineScript language.
// World script, it takes effect only when the world is loaded.

// world script variable
int worldscript_var = 100;

/*  */

Now in order to use this world script variable in the methods of AppSystemLogic class as an example, we can use the Engine::getWorldVariable() method. So, we must include the UnigineInterface.h library and add the following code to the AppSystemLogic.cpp file:

Source code (C++)
// AppSystemLogic.cpp
#include <UnigineInterface.h>

/*  */

using namespace Unigine;

// engine pointer
Engine *engine;

/*  */

int AppSystemLogic::init() {
	
	// getting the engine pointer
	engine = Engine::get();

/*  */
	
	return 1;
}

int AppSystemLogic::update() {

/*  */

	// checking if there is a variable with a given name in the world script
	if (engine->isWorldVariable("worldscript_var"))
	{
		// getting the value of the world script variable		
		int ws_var = engine->getWorldVariable("worldscript_var").getInt();
		if (ws_var <= 110)
		{
			Log::message("\nSYSTEMLOGIC UPDATE: Worldscript variable (modified value) worldscript_var = %d\n\n", ws_var);
			// changing the value of the world script variable
			engine->setWorldVariable("worldscript_var", Variable(ws_var + 1));
		}
	}

	return 1;
}
/*  */

Using Member Variables#

In the AppWorldLogic.h file, add the following code to define a pointer to the AppSystemLogic instance and a world logic variable. We must also include the AppSystemLogic.h header file.

Source code (C++)
// AppWorldLogic.h

/*  */

#include "AppSystemLogic.h"

class AppSystemLogic;

class AppWorldLogic : public Unigine::WorldLogic {
	
public:

/*  */

	// pointer to the systemlogic
	AppSystemLogic *systemlogic_ptr;

	// world logic variable
	int worldlogic_var = 11;
};

/*  */

In the AppSystemLogic.h file, add the following code to define a pointer to the AppWorldLogic instance and a system logic variable. And again we must include the AppWorldLogic.h header file.

Source code (C++)
// AppSystemLogic.h

/*  */

#include "AppWorldLogic.h"

class AppWorldLogic;

class AppSystemLogic : public Unigine::SystemLogic {
	
public:

/*  */

	// pointer to the worldlogic
	AppWorldLogic *worldlogic_ptr;

	// system logic variable
	int systemlogic_var = 22;
};

/*  */

In order to get access to the member variables of the instances of the AppSystemLogic and AppWorldLogic classes we should initialize our pointers. So we must insert the following code to the main file of our project called <YOUR_PROJECT_NAME>.cpp file.

Source code (C++)
// <YOUR_PROJECT_NAME>.cpp
#include <UnigineEngine.h>
#include "AppSystemLogic.h"
#include "AppWorldLogic.h"
#include "AppEditorLogic.h"

#ifdef _WIN32
	int wmain(int argc,wchar_t *argv[]) {
#else
	int main(int argc,char *argv[]) {
#endif
	
	Unigine::EnginePtr engine(argc,argv);
	

	AppSystemLogic system_logic;
	AppWorldLogic world_logic;
	AppEditorLogic editor_logic;
	
	// initialize pointers
	system_logic.worldlogic_ptr = &world_logic;
	world_logic.systemlogic_ptr = &system_logic;

	engine->main(&system_logic,&world_logic,&editor_logic);
	
	return 0;
}

Now we can manage these variables in the methods of AppWorldLogic and AppSystemLogic classes by simply using systemlogic_ptr->systemlogic_var and worldlogic_ptr->worldlogic_var respectively.

Combining All Three Approaches#

Let us combine the three approaches and use our global, world script and member variables in the methods of the AppWorldLogic class:

Source code (C++)
// AppWorldLogic.cpp
/*  */

// including the global.h to use our global variable
#include "global.h"
using namespace Unigine;

/*  */

int AppWorldLogic::init() {

	Log::warning("\nWorldlogic reporting:\n");
	
	// modifying a global variable
	global_var += 1000;
	Log::message("\nGlobal variable (+1000 value) global_var = %d", global_var);
	Log::message("\nSystemlogic variable (initial value) system_var = %d", systemlogic_ptr->systemlogic_var);
	
	// modifying a system logic variable
	systemlogic_ptr->systemlogic_var = 11;
	Log::message("\nSystemlogic variable (modified value) system_var = %d \n\n", systemlogic_ptr->systemlogic_var);

	return 1;
}

int AppWorldLogic::update() {
	
	// managing a system logic variable
	if (systemlogic_ptr->systemlogic_var > 0){
		systemlogic_ptr->systemlogic_var--;
		Log::message("\nWORLDLOGIC UPDATE: Systemlogic variable (modified value) system_var = %d", systemlogic_ptr->systemlogic_var);
	}
	return 1;
}

/* .. */
and the AppSystemLogic class:
Source code (C++)
// AppSystemLogic.cpp
/*  */

#include <UnigineInterface.h>

// including the global.h to use our global variable
#include "global.h"

using namespace Unigine;
Engine *engine;

/*  */

int AppSystemLogic::init() {
	// getting an engine pointer
	engine = Engine::get();

	Log::warning("\nSystemlogic reporting:\n ");

	// modifying a global variable
	global_var += 1000;
	Log::message("\nGlobal variable (+1000 value) global_var = %d", global_var);
	Log::message("\nWorldlogic variable (initial value) world_var = %d", worldlogic_ptr->worldlogic_var);
	
	// modifying a world logic variable
	worldlogic_ptr->worldlogic_var += 100;
	Log::message("\nWorldlogic variable (modified value) world_var = %d\n\n", worldlogic_ptr->worldlogic_var);

	return 1;
}

int AppSystemLogic::update() {
	// checking if there is a variable with a given name in the world script
	if (engine->isWorldVariable("worldscript_var")){
		// getting the value of the world script variable		
		int ws_var = engine->getWorldVariable("worldscript_var").getInt();
		if (ws_var <= 110)
		{
			Log::message("\nSYSTEMLOGIC UPDATE: Worldscript variable (modified value) worldscript_var = %d\n\n", ws_var);
			// changing the value of the world script variable
			engine->setWorldVariable("worldscript_var", Variable(ws_var + 1));
		}
	}

	return 1;
}

/* .. */
Last update: 2023-06-23
Build: ()