Getrss icon Getrss — Fetch RSS via GetRSS command; populates $RSSSuccess, $RSSErrorMsg and arrays $RSSTitles, $RSSDescriptions, $RSSLinks, $RSSDates for scripts. Try it →
Asked
Resolved Resolved by Athena!
1 Participant
Implicit Global Variable Creation

Implicit Global Variable Creation

I'm a little confused and looking for clarification from the community.

I'm reviewing all my major scripts in my ARC project to make sure there are no real issues. In the script attached below there is a command that sets a global variable to 1, and then at the end of the script it is reset to 0. When I wrote this script I failed to place the "\(" before the variable name. However, watching the Variable Watcher control in ARC, I see that it actually registers as a global variable even without the "\)" placed before it. Why does this happen?

The two lines in question (as I intended them):

setVar("Personality", "1", "Sets variable to 1 so other scripts know this script is running");
setVar("Personality", "0", "Sets variable to 0 so other scripts know this script has stopped running");

For context: this script is part of my ARC project and issues ControlCommand calls to other Robot Skills and interacts with EZB hardware (ADC, Servo, UART, Digital, PWM). The Variable Watcher control in ARC shows the global variable "Personality" even though I did not prefix the name with "$" in the setVar calls. The full script is below.

Full script (reformatted):

const POWER_PACK_ADC = ADC0;
const PULL_THRESHOLD = 200;
const INSERT_THRESHOLD = 120;
const ADC_POLL_MS = 500;
const GLOBAL_POLL_MS = 500;

function waitForPowerPackPull() {
  print("Waiting for power pack to be pulled...");
  const value = ADC.waitForHigher(POWER_PACK_ADC, PULL_THRESHOLD, ADC_POLL_MS, 0);
  if (value === -1) {
    print("Pull detect timed out, retrying...");
    return false;
  }
  return true;
}

function waitForPowerPackInsert() {
  print("Waiting for power pack to be inserted...");
  const value = ADC.waitForLower(POWER_PACK_ADC, INSERT_THRESHOLD, ADC_POLL_MS, 0);
  if (value === -1) {
    print("Insert detect timed out, retrying...");
    return false;
  }
  return true;
}

function waitForGlobal(varName, expectedValue) {
  while (getVar(varName) !== expectedValue) {
    sleep(GLOBAL_POLL_MS);
  }
}

function ensureEZB0Connected() {
  if (!EZB.isConnected(0)) {
    print("Reconnect EZB Connection 0 or restart Robot");
    halt();
  }
}

function releaseWrists() {
  Servo.release(D1, 2); // Rt Up/Dn Wrist (EZB2)
  Servo.release(D0, 2); // Rt side-to-side Wrist (EZB2)
  Servo.release(D4, 2); // Lft Up/Dn Wrist (EZB2)
  Servo.release(D5, 2); // Lft side-to-side Wrist (EZB2)
  UART.hardwareUartWriteString(0, "1, powerdown\r", 2); // Power down Rt Elbow Motor, connected to UART 0 on EZB 2
  //sleep(250);
  UART.hardwareUartWriteString(0, "2, powerdown\r", 2); // Power down left Elbow Motor. Connected to UART 2 on EZB 2
  //sleep(250);
  UART.hardwareUartWriteString(2, "1, Powerdown\r", 2); // Power down Right Arm Carriage Motor, EZB, UART Port 2
  //sleep(250);
  UART.hardwareUartWriteString(2, "2, Powerdown\r", 2); // Power down Left Arm Carriage Motor, EZB, UART Port 2
  sleep(250);
  print("Power Down Complete");
}

function pullPowerPack() {
  ensureEZB0Connected();
  controlCommand("Personality Generator", "PauseOn");
  setVar("Personality", "1", "Sets variable to 1 so other scripts know this script is running");
  sleep(100);
  controlCommand("Script Manager", "ScriptStart", "Mechalical Background Sounds - Stop");
  controlCommand("Auto Both Arms", "AutoPositionAction", "Both Ars Out");
  sleep(100);
  controlCommand("Leg Section", "ScriptStart", "Hip Down Full");
  sleep(250);
  controlCommand("Head Section", "ScriptStart", "Bubble Full Down");
  sleep(500);
  Digital.set(D8, true); // Kill Brain Lights
  controlCommand("ADC Watcher", "ScriptStop");
  controlCommand("Weather Loop", "ScriptStop");
  waitForGlobal("$Both_ARS_Extended", 1);
  controlCommand("Auto Both Arms", "AutoPositionAction", "Dead_Down");
  sleep(1000);
  releaseWrists();
}

function insertPowerPack() {
  controlCommand("Script Manager", "ScriptStart", "Mechalical Background Sounds - Loop");
  controlCommand("Leg Section", "ScriptStart", "Up with Hips");
  sleep(500);
  Digital.set(D0, true);   // Ear sensor motors
  Digital.set(D8, false);  // Brain lights on
  Digital.set(D10, true);  // Fast brain lights
  Digital.set(D2, true);   // Scanner lights
  PWM.set(D9, 50);         // Crown motor
  UART.initHardwareUart(1, 9600, 2); // Turn on Torso EZB 0 UART, #2 port - bubble lifter. Pot does not need to be homed.
  sleep(100);
  UART.hardwareUartWriteString(1, "1, start\r", 2); // Start Kangaroo channel 1 for bubble lifter through Torso Section EZB 0, UART port 2
  sleep(400);
  controlCommand("Head Section", "ScriptStart", "Bubble Up and Down");
  wakeup = Utility.GetRandomUnique(1, 3);
  sleep(100); // Response depending on random number
  if (wakeup == 1) {
    ControlCommand("Phrases", "Play", "PowerUp_BatteryCharge.mp3");
  } else if (wakeup == 2) {
    ControlCommand("Phrases", "Play", "Powerup-Where_Am_I.mp3");
  }
  sleep(4000);
  Digital.set(D2, false);  // Scanner lights off
  sleep(500);
  Digital.set(D4, true);   // Box lights
  controlCommand("Head Section", "ScriptStart", "Radar 1/4 Sweep");
  controlCommand("Auto Both Arms", "AutoPositionAction", "Both Ars In");
  sleep(5000);
  Digital.set(D0, false);   // Ear sensor motors off
  Digital.set(D10, false);  // Fast brain lights off
  Digital.set(D4, false);   // Box lights off
  PWM.set(D9, 0);           // Crown motor off
  controlCommand("ADC Watcher", "ScriptStart");
  sleep(250);
  controlCommand("Weather Loop", "ScriptStart");
  sleep(250);
  controlCommand("Personality Generator", "PauseOff");
  setVar("Personality", "0", "Sets variable to 0 so other scripts know this script stopped running");
}

function runPowerCycle() {
  while (true) {
    if (waitForPowerPackPull()) {
      pullPowerPack();
    }
    if (waitForPowerPackInsert()) {
      insertPowerPack();
    }
  }
}

runPowerCycle();

Again: my question is simply why the Variable Watcher in ARC registers "Personality" as a global variable when I called setVar("Personality", ...) without a leading "\(". Any clarification about how ARC/global variables are recognized (and whether setVar implicitly creates globals without requiring "\)") would be helpful.



Related Hardware (view all EZB hardware)
EZ-B V4 by EZ-Robot
EZ-B v4 with Synthiam ARCx controls servos, sensors, camera and audio over Wi-Fi or USB serial, no firmware needed.
Wi-Fi / USB
Servos 24
Camera
Audio
UART 3
I2C
ADC 8
Digital 24

Related Robot Skills (view all robot skills)
Variable Watch by Synthiam
Live view of script variables, types, sizes and values for debugging; auto-refresh, hex view, pause/clear; may affect program performance.
Script by Synthiam
Multi-language ARC Script: build, run and debug Blockly, JavaScript, EZ-Script or Python with Intellisense, run/save/load and Roboscratch support

ARC Pro

Upgrade to ARC Pro

ARC Pro is more than a tool; it's a creative playground for robot enthusiasts, where you can turn your wildest ideas into reality.

Author Avatar
PRO
Canada
Twitter YouTube GitHub
#1  

Yes - in ARC JavaScript, setVar() creates or updates a global variable automatically. You do not need to put the $ in the setVar() call.

Why this happens

In ARC:

  • setVar("$Personality", 1) is the normal global-variable form
  • but setVar("Personality", 1) can still create a variable that appears in Variable Watch
  • getVar() / setVar() work with ARC’s global variable manager, and the $ prefix is part of ARC’s global variable naming convention, not always a strict requirement for the function call itself

So what you’re seeing is expected behavior: ARC is recognizing and storing the variable in its global scope even without the $ prefix in that call.

Important best practice

Even though your code works, the recommended ARC convention is to always use the $ prefix for global variables:

setVar("$Personality", 1, "Sets variable to 1 so other scripts know this script is running");
setVar("$Personality", 0, "Sets variable to 0 so other scripts know this script has stopped running");

And your waits should also use the same exact global name:

while (getVar("$Personality") !== 1) {
  sleep(500);
}

Why using $ matters

Using $ consistently helps avoid:

  • confusion between local script variables and global ARC variables
  • accidental naming collisions
  • harder debugging later when projects grow larger

One thing I’d correct in your script

Your script uses:

setVar("Personality", "1")

That stores the value as a string, not a number. Since you compare numerically elsewhere, it’s better to use numbers:

setVar("$Personality", 1);
setVar("$Personality", 0);

The better pattern in ARC

For signaling "this routine is active," ARC projects are usually cleaner when you:

  • use a global status variable
  • set it with a $ name
  • read it from other scripts with getVar("$Personality")

That is the proven approach in ARC.

Relevant docs

If you want, I can also review this script for a more ARC-standard way to structure the state signaling and controlCommand() flow so it’s easier to maintain. What’s the bigger goal of this "Personality" flag - is it just a run/stop indicator, or are other scripts using it as a mode/state machine?

I am a customer support AI and still learning. Please verify important answers in our support documentation. For advanced AI features, use the AI Script Agent built into ARC.
#2  

The editor stripped out the dollar sign \(that I wrote in my question making the Var global. It placed "\\)" there instead. Just to be clear I was asking about why this works as a global var even with the dollar sign\) placed before the Var name?

#3  

It did it again. I'm trying to ask about dollar sign.

#4  

OK, that was fast. Thanks I understand now.

If I use SetVar ARC now it's a global variable and if I forget to place the dollar sign I will still know what I want to do. Same with using just Var. That will tell ARC it's a local variable just to be used in that script.

This "Personality" var is simply a run/stop indicator to let other scripts that this one is running so they will not run at the same time.

I'll consider your other suggestions. Thanks for the ARC education.

📚 This thread has been preserved as a helpful resource!

While new comments are no longer being accepted, the information here remains available for learning and reference. Check the most recent comment for additional details.