Sharing Objects Across Multiple Tasks
Please refer to _shared for more information.
Introduction:
In this example, we demonstrate how to share data across different scripts, modules, or terminals to enable cross-environment collaboration. Using _shared, you can pass state, function references, or business data between multiple scripts or tasks, allowing seamless communication and coordination.
Typical use cases include:
- Multi-terminal collaborative debugging
- Sharing results across multiple tasks
- Task scheduling and state synchronization
- Passing tokens, configuration, or business data between scripts
Before Running:
- Download and install Total Control 11.0 (Update 20) or later (Download)
- Open two terminals:
- Main Panel → Script → Terminal → Terminal 1
- Main Panel → Script → Terminal → Terminal 2
Step 0: Unified Initialization
All scripts should first perform the following initialization to prevent errors if _shared or _shared.ns do not exist. Execute this in Terminal 1 or any script entry point:
if (typeof _shared !== "object") _shared = {};
if (!_shared.ns) _shared.ns = {};
Step 1: Write Shared Data in Terminal 1
_shared.ns.userInfo = { id: 12345, name: "Tom" };
_shared.ns.counter = 0;
_shared.ns.config = {
apiUrl: "https://api.example.com",
timeout: 5000,
debugMode: true
};
Step 2: Read and Modify Shared Data in Terminal 2
print(_shared.ns.userInfo.name);
_shared.ns.counter++;
print("counter = " + _shared.ns.counter);
print("api = " + _shared.ns.config.apiUrl);
Execution Result (Terminal 2)
Tom
counter = 1
api = https://api.example.com