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版本。

Sharing Data between the Logic System Components

Sometimes it is necessary to share data between the components 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 class in a separate file, so we add a file to our project:

  • GlobalVar.cs file.
    Source code (C#)
    // GlobalVar.cs
    				
    namespace UnigineApp
    {
        public static class GlobalVar
        {
           public static int int_var = 33;
        }
    }

Now we can manage this global variable in the methods of AppSystemLogic, AppWorldLogic or AppEditorLogic classes by simply typing GlobalVar.int_var.

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 add the following code to the AppSystemLogic.cs file:

Source code (C#)
// AppSystemLogic.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Unigine;

namespace UnigineApp
{
    class AppSystemLogic : SystemLogic
    {

        /*  */

        public override bool Update()
        {
            /*  */

            // checking if there is a variable with a given name in the world script
            if (Engine.engine.IsWorldVariable("worldscript_var"))
            {
                // getting the value of the world script variable		
                int ws_var = Engine.engine.GetWorldVariable("worldscript_var").Int;
                if (ws_var <= 110)
                {
                    Log.Message("\nSYSTEMLOGIC UPDATE: Worldscript variable (modified value) worldscript_var = {0}\n\n", ws_var);
                    // changing the value of the world script variable
                    Engine.engine.SetWorldVariable("worldscript_var", new Variable(ws_var + 1));
                }
            }

            return true;
        }

        /*  */

    }
}

Using Member Variables#

In the AppWorldLogic.cs file, add the following code to define a pointer to the AppSystemLogic instance and a world logic variable.

Source code (C#)
// AppWorldLogic.cs

/*  */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Unigine;


namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{
        // world logic variable
		public int worldlogic_var = 11;
		
		// pointer to the systemlogic
        AppSystemLogic system_logic;
		
		// method setting the pointer to the systemlogic
        public void setSL(AppSystemLogic SL)
        {
            system_logic = SL;
        }
		
		/*  */

	}
}

In the AppSystemLogic.cs file, add the following code to define a pointer to the AppWorldLogic instance and a system logic variable.

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

/*  */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Unigine;


namespace UnigineApp
{

	class AppSystemLogic : SystemLogic
	{
        // system logic variable 
        public int systemlogic_var = 22;
		
		// pointer to the worldlogic
        AppWorldLogic world_logic;
		
		// method setting the pointer to the worldlogic
        public void setWL(AppWorldLogic WL)
        {
            world_logic = WL;
        }
		
		/*  */

	}
}

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>.cs file.

Source code (C#)
// <YOUR_PROJECT_NAME>.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Unigine;

namespace UnigineApp
{
	class UnigineApp
	{

		static void Main(string[] args)
		{
			AppSystemLogic systemLogic = new AppSystemLogic();
			AppWorldLogic worldLogic = new AppWorldLogic();
			AppEditorLogic editorLogic = new AppEditorLogic();

            // initialize pointers
            worldLogic.setSL(systemLogic);
            systemLogic.setWL(worldLogic);

			Engine engine = Engine.Init(Engine.VERSION, args);
			
			engine.Main(systemLogic, worldLogic, editorLogic);

			Engine.Shutdown();
		}
	}
}

Now we can manage these variables in the methods of AppWorldLogic and AppSystemLogic classes by simply using system_logic->systemlogic_var and world_logic->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.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Unigine;

namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{
        // world logic variable
		public int worldlogic_var = 11;
		
		// pointer to the systemlogic
        AppSystemLogic system_logic;

		/*  */
		
        // method setting the pointer to the systemlogic
        public void setSL(AppSystemLogic SL)
        {
            system_logic = SL;
        }

		public override bool Init()
		{
	
        	// modifying a global variable
	        GlobalVar.int_var += 1000;
            Log.Message("\nGlobal variable (+1000 value) global_var = {0}", GlobalVar.int_var);
            Log.Message("\nSystemlogic variable (initial value) system_var = {0}", system_logic.systemlogic_var);
	
	        // modifying a system logic variable
	        system_logic.systemlogic_var = 11;
            Log.Message("\nSystemlogic variable (modified value) system_var = {0} \n\n", system_logic.systemlogic_var);

			return true;
		}

		// start of the main loop
		public override bool Update()
		{
	        // managing a system logic variable
            if (system_logic.systemlogic_var > 0)
            {
                system_logic.systemlogic_var--;

                Log.Message("\nWORLDLOGIC UPDATE: Systemlogic variable (modified value) system_var = {0}", system_logic.systemlogic_var);
            }
			return true;
		}

		/*  */
	}
}
and the AppSystemLogic class:
Source code (C#)
// AppSystemLogic.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Unigine;

namespace UnigineApp
{
	class AppSystemLogic : SystemLogic
	{
		// system logic variable
        public int systemlogic_var = 22;
		
		// pointer to the worldlogic
        AppWorldLogic world_logic;

        /*  */

        public void setWL(AppWorldLogic WL)
        {
            world_logic = WL;
        }

		public override bool Init()
		{
	        Log.Warning("\nSystemlogic reporting:\n ");

	        // modifying a global variable
            GlobalVar.int_var += 1000;
            Log.Message("\nGlobal variable (+1000 value) global_var = {0}", GlobalVar.int_var);
	
        	// managing a world logic variable
			Log.Message("\nWorldlogic variable (initial value) world_var = {0}", world_logic.worldlogic_var);
	        world_logic.worldlogic_var += 100;
            Log.Message("\nWorldlogic variable (modified value) world_var = {0}\n\n", world_logic.worldlogic_var);

			return true;
		}

		// start of the main loop
		public override bool Update()
		{
	        // managing a worldscript variable
	        if (Engine.engine.IsWorldVariable("worldscript_var")){
	    	    int ws_var = Engine.engine.GetWorldVariable("worldscript_var").Int;
	    	    if (ws_var <= 110)
	    	    {
	    		    Log.Message("\nSYSTEMLOGIC UPDATE: Worldscript variable (modified value) worldscript_var = {0}\n\n", ws_var);
	    		    Engine.engine.SetWorldVariable("worldscript_var", new Variable(ws_var + 1));
	    	    }
	        }

			return true;
		}

		/*  */
	}
}
Last update: 2020-11-24
Build: ()