# Stat Value

## 📊 StatValue Struct

The `StatValue` struct represents a value of a [stat](https://stylishesper.gitbook.io/inventool/scripting/api/stats/stat "mention") and is designed to provide a flexible alternative to numeric types.

> Namespace: `Esper.Inventool.Stats`\
> Access: `public`\
> Type: `struct` \
> \\

***

### 📦 Fields

| Access   | Field         | Type                       | Description       |
| -------- | ------------- | -------------------------- | ----------------- |
| `public` | `numericType` | `NumericValue.NumericType` | The numeric type. |
| `public` | `value`       | `NumericValue.Value`       | The value.        |

***

### 🧰 Methods

| Access   | Method                                                                      | Returns         | Description                                                            |
| -------- | --------------------------------------------------------------------------- | --------------- | ---------------------------------------------------------------------- |
| `public` | `StatValue(NumericValue.Value value)`                                       | — (constructor) | Creates a new `StatValue` from a `NumericValue.Value`.                 |
| `public` | `StatValue(NumericValue.NumericType numericType)`                           | — (constructor) | Creates a new `StatValue` with the specified numeric type.             |
| `public` | `StatValue(NumericValue.NumericType numericType, NumericValue.Value value)` | — (constructor) | Creates a new `StatValue` with a numeric type and a value.             |
| `public` | `StatValue(NumericValue.NumericType numericType, int value)`                | — (constructor) | Creates a new `StatValue` with a numeric type and integer value.       |
| `public` | `StatValue(NumericValue.NumericType numericType, float value)`              | — (constructor) | Creates a new `StatValue` with a numeric type and float value.         |
| `public` | `SetNumericType(NumericValue.NumericType numericType)`                      | `void`          | Sets the numeric type, converting the stored value if needed.          |
| `public` | `SetValue(int value)`                                                       | `void`          | Sets the stat value as an integer.                                     |
| `public` | `SetValue(float value)`                                                     | `void`          | Sets the stat value as a float.                                        |
| `public` | `SetValueWithConversion(int value)`                                         | `void`          | Sets the value with conversion based on the numeric type.              |
| `public` | `SetValueWithConversion(float value)`                                       | `void`          | Sets the value with conversion based on the numeric type.              |
| `public` | `IsComparable(StatValue other)`                                             | `bool`          | Checks if this value is comparable with another based on numeric type. |
| `public` | `ToString()`                                                                | `string`        | Converts the stat value to a readable string.                          |
| `public` | `ToFloat()`                                                                 | `float`         | Converts the stat value to a float.                                    |
| `public` | `ToInt()`                                                                   | `int`           | Converts the stat value to an int.                                     |

***

### ➕ Operator Overloads

#### Arithmetic Operators

| Operator | Description                                                   |
| -------- | ------------------------------------------------------------- |
| `/`      | Supports division with `StatValue`, `int`, and `float`.       |
| `*`      | Supports multiplication with `StatValue`, `int`, and `float`. |
| `+`      | Supports addition with `StatValue`, `int`, and `float`.       |
| `-`      | Supports subtraction with `StatValue`, `int`, and `float`.    |

#### Comparison Operators

| Operator             | Description                                                                  |
| -------------------- | ---------------------------------------------------------------------------- |
| `==`, `!=`           | Equality and inequality comparisons between `StatValue`, `int`, and `float`. |
| `<`, `>`, `<=`, `>=` | Standard comparison operators for numeric comparison.                        |

#### Implicit Conversions

| Conversion          | Description                                      |
| ------------------- | ------------------------------------------------ |
| `int → StatValue`   | Implicitly converts an integer to a `StatValue`. |
| `float → StatValue` | Implicitly converts a float to a `StatValue`.    |
| `StatValue → float` | Implicitly converts a `StatValue` to a float.    |
| `StatValue → int`   | Implicitly converts a `StatValue` to an integer. |

***

### 🧩 Nested Enums

#### ValueType

Defines how a stat value should be treated.

| Value     | Description                               |
| --------- | ----------------------------------------- |
| `Value`   | The stat’s value is exact.                |
| `Percent` | The stat’s value represents a percentage. |

***

## Creating a Stat Value

A `StatValue` requires a `NumericValue.NumericType` and a numeric value (`int` or `float`).

```csharp
StatValue myStatValue = new StatValue(myStatIdentity, 100);
```

## Operators

You can use arithmetic and comparison operators for mathematical and comparison purposes.

#### Arithmetic Example

You can use arithmetic operators for a `StatValue` for mathematical purposes with another `StatValue`, `int`, or `float` value.

```csharp
StatValue statValueC = statValueA + statValueB;
```

#### Comparison Example

You can use comparison operators for to compare a `StatValue` with another `StatValue`, `int`, or `float` value.

```csharp
if (statValueA >= statValueB)
{
    // Do something...
}
```
