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
统一的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版本。

Splitting Logic Between Several WorldLogic Classes

Each world script is associated with a certain world. Although the basic recommended workflow when writing application logic in C++ / C# is to have a single AppWorldLogic class to process all worlds in your project, sometimes it might be necessary to split world logic between several separate classes. In such a case the basic AppWorldLogic class is used as a manager calling corresponding methods (init(), update(), postUpdate(), etc.) of a particular class implementing logic of the current world.

This example illustrates how to split world-specific code between separate classes. Suppose in our project we have a default world named "world1" and another one named "world2" and we want to have two separate world logic classes (WorldLogic1 and WorldLogic2) for each of these worlds. Suppose we also want to switch between these worlds by pressing PGUP and PGDOWN keys.

1. Creating WorldLogic Classes#

First, we just inherit two new classes (WorldLogic1 and WorldLogic2) from Unigine::WorldLogic. You can simply copy the contents of AppWorldLogic.h and AppWorldLogic.cpp files and modify their init() and update() methods like this:

Notice
You can implement your logic for other methods of the execution sequence if necessary.

WorldLogic1.cpp

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

int WorldLogic1::init()
{
	// reporting that we're processing the first world and displaying a path to the .world file
	Log::message("Initializing the FIRST world (%s)\n", World::getPath());

	// insert your init logic for the first world

	return 1;
}

int WorldLogic1::update()
{
	// insert your update logic for the first world

	return 1;
}

// ...

WorldLogic2.cpp

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

int WorldLogic2::init()
{
	// reporting that we're processing the first world and displaying a path to the .world file
	Log::message("Initializing the SECOND world (%s)\n", World::getPath());

	// insert your init logic for the second world

	return 1;
}

int WorldLogic2::update()
{
	// insert your update logic for the second world

	return 1;
}

// ...

2. Managing World Logics via AppWorldLogic#

Now, we implement world logic management in the AppWorldLogic, so we should add all our world logics, and a pointer to the one currently used. In the init() method, as it is called each time a new world is loaded, we should change the current world logic. So we modify the AppWorldLogic.h file as follows:

AppWorldLogic.h

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

#include "WorldLogic1.h"		//<-- include world logic class declaration for the first world
#include "WorldLogic2.h"		//<-- include world logic class declaration for the second world

class AppWorldLogic : public Unigine::WorldLogic
{

public:

// ...

	
	Unigine::WorldLogic* current_world_logic = &wl1;	//<-- pointer to the current world logic, we set the first one by default

private:
	WorldLogic1 wl1;									//<-- world logic for the first world
	WorldLogic2 wl2;									//<-- world logic for the second world
};

//...

Inside each method of the AppWorldLogic class, we simply call the corresponding method of the current world logic class, like for the init() and update() methods below. In the init() method, as it is called each time a new world is loaded, we also should change the current world logic, if necessary:

AppWorldLogic.cpp

Source code (C++)
#include <UnigineWorld.h>	//<-- include the UnigineWorld.h for the World class

using namespace Unigine;
// ...

int AppWorldLogic::init()
{
	// checking the name of the loaded world and updating current world logic
	if (strstr(World::getPath(), "world1.world"))
		current_world_logic = &wl1;
	else if (strstr(World::getPath(), "world2.world"))
		current_world_logic = &wl2;

	// calling the init() method of the current world logic
	current_world_logic->init();

	return 1;
}

int AppWorldLogic::update()
{
	// calling the update() method of the current world logic
	current_world_logic->update();

	return 1;
}

3. Keyboard World Loading via AppSystemLogic#

Switching between the worlds is to be performed in the AppSystemLogic class. In the update() method we should check keyboard input and load the corresponding world, if necessary. So, we modify the AppSystemLogic.cpp file as follows:

AppSystemLogic.cpp

Source code (C++)
#include <UnigineWorld.h>

// ...
int AppSystemLogic::update()
{
	// checking key states and loading worlds
	if (Input::isKeyPressed(Input::KEY_PGUP))
	{	// loading the second world if it is not already loaded
		if (strstr(World::getPath(), "world1.world"))
			World::loadWorld("world2");
	}
	else if (Input::isKeyPressed(Input::KEY_PGDOWN))
	{	// loading the first world if it is not already loaded
		if (strstr(World::getPath(), "world2.world"))
			World::loadWorld("world1");
	}

	return 1;
}
Last update: 2024-04-19
Build: ()