# GlobalVars

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

**Since:** 1.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 {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_globalRaw}

```js
globalRaw: Map<String, Object>
```

Live backing map. Direct writes bypass the guest-object check in
[putObject(String, Object)](#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_putObject-String-Object-); typed getters return `null` for values of other
types. Counter and toggle operations use atomic map computations.

## Methods

### decrementAndGetInt {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_decrementAndGetInt-String-}

```js
decrementAndGetInt(String name): Integer
```

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

**Since:** 1.6.5

### getAndDecrementInt {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_getAndDecrementInt-String-}

```js
getAndDecrementInt(String name): Integer
```

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

**Since:** 1.6.5

### getAndIncrementInt {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_getAndIncrementInt-String-}

```js
getAndIncrementInt(String name): Integer
```

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)](#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_incrementAndGetInt-String-), this returns the value
before incrementing.  
```js
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

**Since:** 1.6.5

### getBoolean {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_getBoolean-String-}

```js
getBoolean(String name): Boolean
```

Returns the value under `name` only if it is a [Boolean](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Boolean.html).

**Parameters:**
- `name`: key to look up

**Returns:** the boolean value, or `null` if absent or of another type

**Since:** 1.1.7

### getDouble {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_getDouble-String-}

```js
getDouble(String name): Double
```

Returns the value under `name` only if it is a [Double](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Double.html).

**Parameters:**
- `name`: key to look up

**Returns:** the double value, or `null` if absent or of another type

**Since:** 1.0.8

### getInt {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_getInt-String-}

```js
getInt(String name): Integer
```

Returns the value under `name` only if it is an [Integer](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Integer.html).

**Parameters:**
- `name`: key to look up

**Returns:** the integer value, or `null` if absent or of another type

**Since:** 1.0.4

### getObject {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_getObject-String-}

```js
getObject(String name): Object
```

Returns the value stored under `name`, regardless of its type.

**Parameters:**
- `name`: key to look up

**Returns:** the stored value, or `null` if absent

**Since:** 1.1.7

### getRaw {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_getRaw-}

```js
getRaw(): Map<String, Object>
```

Returns the live backing map, not a copy. Direct writes bypass the guest-object check in
[putObject(String, Object)](#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_putObject-String-Object-).

**Returns:** the live backing map

### getString {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_getString-String-}

```js
getString(String name): String
```

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

**Since:** 1.0.4

### getType {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_getType-String-}

```js
getType(String name): String
```

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.  
```js
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

**Since:** 1.0.4

### incrementAndGetInt {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_incrementAndGetInt-String-}

```js
incrementAndGetInt(String name): Integer
```

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)](#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_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

**Since:** 1.6.5

### putBoolean {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_putBoolean-String-boolean-}

```js
putBoolean(String name, boolean b): boolean
```

Stores a boolean under `name`.

**Parameters:**
- `name`: key to store under
- `b`: value to store

**Returns:** the stored value

**Since:** 1.1.7

### putDouble {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_putDouble-String-double-}

```js
putDouble(String name, double d): double
```

Stores a double under `name`.

**Parameters:**
- `name`: key to store under
- `d`: value to store

**Returns:** the stored value

**Since:** 1.0.8

### putInt {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_putInt-String-int-}

```js
putInt(String name, int i): int
```

Stores an integer under `name`.

**Parameters:**
- `name`: key to store under
- `i`: value to store

**Returns:** the stored value

**Since:** 1.0.4

### putObject {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_putObject-String-Object-}

```js
putObject(String name, Object o): Object
```

Stores a Java object under `name`. Guest objects are rejected with an
[AssertionError](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/AssertionError.html); any existing value is replaced regardless of type.  
```js
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

**Since:** 1.1.7

### putString {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_putString-String-String-}

```js
putString(String name, String str): String
```

Stores a string under `name`.

**Parameters:**
- `name`: key to store under
- `str`: value to store

**Returns:** the stored value

**Since:** 1.0.4

### remove {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_remove-String-}

```js
remove(String key): void
```

Removes a key from the global variable space.

**Parameters:**
- `key`: key to remove

**Since:** 1.2.0

### toggleBoolean {#com.jsmacrosce.jsmacros.core.library.impl.FGlobalVars_toggleBoolean-String-}

```js
toggleBoolean(String name): Boolean
```

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

**Since:** 1.6.5
