This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
UnigineEditor
界面概述
资产工作流程
设置和首选项
项目开发
调整节点参数
Setting Up Materials
Setting Up Properties
照明
Landscape Tool
Sandworm (Experimental)
使用编辑器工具执行特定任务
Extending Editor Functionality
嵌入式节点类型
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Objects
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
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
注意! 这个版本的文档是过时的,因为它描述了一个较老的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.cs file 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.cs

Source code (C#)
// ...

namespace UnigineApp
{
    class WorldLogic1 : WorldLogic
    {
        public WorldLogic1()
		{
		}

		public override bool Init()
		{
            Log.Message("Initializing the FIRST world ({0})\n", World.Path);
			
			// insert your init logic for the first world
			
			return true;
		}
		
		public override bool Update()
		{
			// insert your update logic for the first world
			
			return true;
		}
		
		// ...
    }
}

WorldLogic2.cs

Source code (C#)
// ...

namespace UnigineApp
{
    class WorldLogic2: WorldLogic
    {
        public WorldLogic2()
		{
		}

		public override bool Init()
		{
            Log.Message("Initializing the SECOND world ({0})\n", World.Path;
			
			// insert your init logic for the second world
			return true;
		}
		
		public override bool Update()
		{
			// insert your update logic for the second world
			
			return true;
		}
		
		// ...
    }
}

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. 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 should change the current world logic. So, we modify the AppWorldLogic.cs file as follows:

AppWorldLogic.cs

Source code (C#)
// ...

using Unigine;

namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{
        public WorldLogic1 wl1;						//<-- world logic for the first world
        public WorldLogic2 wl2;						//<-- world logic for the second world
        public WorldLogic current_world_logic;		//<-- pointer to the current world logic

		public AppWorldLogic()
		{
            // creating world logic instances and setting the first one as current
            wl1 = new WorldLogic1();
            wl2 = new WorldLogic2();
            current_world_logic = wl1;
		}

		public override bool Init()
		{
            // checking the name of the loaded world and changing current world logic if necessary
            if (string.Compare(World.Path, "world1") == 0)
                current_world_logic = wl1;
            else if (string.Compare(World.Path, "world2") == 0)
                current_world_logic = wl2;

            // calling the init() method of the current world logic
            current_world_logic.Init();

			return true;
		}

		// start of the main loop
		public override bool Update()
		{
			// Write here code to be called before updating each render frame: specify all graphics-related functions you want to be called every frame while your application executes.
            
			// calling the update() method of the current world logic
            current_world_logic.Update();
			
			return true;
		}

		// ...
	}
}

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.cs file as follows:

AppSystemLogic.cs

Source code (C#)
// ...

using Unigine;

namespace UnigineApp
{
    class AppSystemLogic : SystemLogic
    {
        // ...

        public override bool Update()
        {
            // checking key states and switching worlds
            if (App.ClearKeyState(App.KEY_PGUP) == 1)
            {   // loading the second world if it is not already loaded
                if (World.Path.EndsWith("world2.world"))
                    World.LoadWorld("world2");
            }
            else if (App.ClearKeyState(App.KEY_PGDOWN) == 1)
            {   // loading the first world if it is not already loaded
                if (World.Path.EndsWith("world1.world"))
                    World.LoadWorld("world1");
            }
            return true;
        }

        // ...
    }
}

Now, the logic of each of our world files (world1 and world2) is controlled by the corresponding WorldLogic class.

Last update: 2020-11-24
Build: ()