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:
- Using a global variable.
- Using a script variable.
- Using a member variable of AppSystemLogic, AppWorldLogic or AppEditorLogic classes.
- 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.
// 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.
// <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:
// AppSystemLogic.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unigine;
namespace UnigineApp
{
class AppSystemLogic : SystemLogic
{
/* */
public override int update()
{
/* */
// checking if there is a variable with a given name in the world script
if (Engine.get().isWorldVariable("worldscript_var"))
{
// getting the value of the world script variable
int ws_var = Engine.get().getWorldVariable("worldscript_var").getInt();
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.get().setWorldVariable("worldscript_var", new Variable(ws_var + 1));
}
}
return 1;
}
/* */
}
}
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.
// 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.
// 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.
// <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)
{
Wrapper.init();
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:
// 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 int 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 1;
}
// start of the main loop
public override int 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 1;
}
/* */
}
}
// 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 int 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 1;
}
// start of the main loop
public override int update()
{
// managing a worldscript variable
if (Engine.get().isWorldVariable("worldscript_var")){
int ws_var = Engine.get().getWorldVariable("worldscript_var").getInt();
if (ws_var <= 110)
{
Log.message("\nSYSTEMLOGIC UPDATE: Worldscript variable (modified value) worldscript_var = {0}\n\n", ws_var);
Engine.get().setWorldVariable("worldscript_var", new Variable(ws_var + 1));
}
}
return 1;
}
/* */
}
}