Skip to content

GlobalVars ​

com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars

Since1.0.4

Stores named Java values shared by scripts in this JsMacros runtime. Values remain until removed or the game session ends; loading another profile does not clear them. Typed getters return null for a missing value or a value of another type; they do not coerce.

js
GlobalVars.putInt("sessions", 0);  
const next = GlobalVars.incrementAndGetInt("sessions");  
print(`session ${next}`);

Accessible in scripts via the global GlobalVars variable.

Fields ​

globalRaw ​

globalRaw: Map<String, Object>#
Live backing map. Direct writes bypass the guest-object check in putObject(String, Object); typed getters return null for values of other types. Counter and toggle operations use atomic map computations.

Methods ​

decrementAndGetInt ​

decrementAndGetInt(String name): IntegerSince1.6.5#
Decrements the integer under name and returns the new value. A missing key is set to and returns -1; a non-integer value is unchanged and returns null. The read and update are atomic.

Parameters:

  • name: key to decrement
Returns: the value after decrement, -1 if absent, or null if not an integer

getAndDecrementInt ​

getAndDecrementInt(String name): IntegerSince1.6.5#
Decrements the integer under name and returns its previous value. A missing key is set to -1 and returns 0; a non-integer value is unchanged and returns null. The read and update are atomic.

Parameters:

  • name: key to read and decrement
Returns: the value before decrement, 0 if absent, or null if not an integer

getAndIncrementInt ​

getAndIncrementInt(String name): IntegerSince1.6.5#
Atomically increments the integer under name and returns its previous value. An absent key is set to 1 and returns 0; a non-integer value is unchanged and returns null. Unlike incrementAndGetInt(String), this returns the value before incrementing.
print(`first call: ${GlobalVars.getAndIncrementInt("hits")}`);
print(`second call: ${GlobalVars.getAndIncrementInt("hits")}`);

Parameters:

  • name: the name to read and increment
Returns: the value before the increment, 0 for a name that held nothing, or null if the name holds something that is not an integer

getBoolean ​

getBoolean(String name): BooleanSince1.1.7#
Returns the value under name only if it is a Boolean.

Parameters:

  • name: key to look up
Returns: the boolean value, or null if absent or of another type

getDouble ​

getDouble(String name): DoubleSince1.0.8#
Returns the value under name only if it is a Double.

Parameters:

  • name: key to look up
Returns: the double value, or null if absent or of another type

getInt ​

getInt(String name): IntegerSince1.0.4#
Returns the value under name only if it is an Integer.

Parameters:

  • name: key to look up
Returns: the integer value, or null if absent or of another type

getObject ​

getObject(String name): ObjectSince1.1.7#
Returns the value stored under name, regardless of its type.

Parameters:

  • name: key to look up
Returns: the stored value, or null if absent

getRaw ​

getRaw(): Map<String, Object>#
Returns the live backing map, not a copy. Direct writes bypass the guest-object check in putObject(String, Object).
Returns: the live backing map

getString ​

getString(String name): StringSince1.0.4#
Returns the value under name only if it is a string.

Parameters:

  • name: key to look up
Returns: the string value, or null if absent or of another type

getType ​

getType(String name): StringSince1.0.4#
Returns the stored value's type name: "Int", "String", "Double", "Boolean", or "Object"; returns null if the name holds nothing. "Object" covers all other stored Java types, including numbers stored under a different numeric type; typed getters do not coerce them.
GlobalVars.putInt("count", 1);
GlobalVars.putDouble("ratio", 0.5);
GlobalVars.putString("name", "notch");
GlobalVars.putBoolean("enabled", true);
print(`${GlobalVars.getType("count")} and ${GlobalVars.getType("ratio")}`);
print(`${GlobalVars.getType("name")} and ${GlobalVars.getType("enabled")}`);

GlobalVars.putObject("raw", JavaUtils.createArrayList());
print(GlobalVars.getType("raw")); // "Object"

Parameters:

  • name: the name to look up
Returns: one of the five type names above, or null if the name holds nothing

incrementAndGetInt ​

incrementAndGetInt(String name): IntegerSince1.6.5#
Increments the integer under name and returns the new value. A missing key is set to 1 and returns 1; a non-integer value is unchanged and returns null. The read and update are atomic. Unlike getAndIncrementInt(String), this returns the value after incrementing.

Parameters:

  • name: the name to increment
Returns: the value after the increment, 1 for a name that held nothing, or null if the name holds something that is not an integer

putBoolean ​

putBoolean(String name, boolean b): booleanSince1.1.7#
Stores a boolean under name.

Parameters:

  • name: key to store under
  • b: value to store
Returns: the stored value

putDouble ​

putDouble(String name, double d): doubleSince1.0.8#
Stores a double under name.

Parameters:

  • name: key to store under
  • d: value to store
Returns: the stored value

putInt ​

putInt(String name, int i): intSince1.0.4#
Stores an integer under name.

Parameters:

  • name: key to store under
  • i: value to store
Returns: the stored value

putObject ​

putObject(String name, Object o): ObjectSince1.1.7#
Stores a Java object under name. Guest objects are rejected with an AssertionError; any existing value is replaced regardless of type.
GlobalVars.putObject("lastResult", JavaUtils.createHashMap());
print(GlobalVars.getType("lastResult")); // "Object"

Parameters:

  • name: the name to store under
  • o: the Java object to store
Returns: o, the same object that was stored

putString ​

putString(String name, String str): StringSince1.0.4#
Stores a string under name.

Parameters:

  • name: key to store under
  • str: value to store
Returns: the stored value

remove ​

remove(String key): voidSince1.2.0#
Removes a key from the global variable space.

Parameters:

  • key: key to remove

toggleBoolean ​

toggleBoolean(String name): BooleanSince1.6.5#
Atomically toggles the boolean under name and returns the resulting value. A missing key is set to true; a non-boolean value is left unchanged and yields null.

Parameters:

  • name: key to toggle
Returns: the new value, or null if the existing value is not boolean