> For the complete documentation index, see [llms.txt](https://infinitypbr.gitbook.io/magic-pig-games/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://infinitypbr.gitbook.io/magic-pig-games/game-modules-4/module-documentation/items/itemobject.cs/item-attribute-variables/variables-methods.md).

# Variables Methods

Each `GameItemObject` has a `List<ItemObjectVariable>` which contains all the variables, and the current values.

```csharp
// GameItemObject.cs
public List<ItemObjectVariable> variables

// Returns the ItemObjectVariable with the variableName provided
public ItemObjectVariable Variable(string variableName)

// Note: The Variable() method will return a value that is cached in a Dictionary
// that is populated at runtime. This is more optimized for values that are called
// often. However, depending on your project, you may wish to cache the
// ItemObjectVariable in another location where it is often accessed, perhaps in
// another class.
```

{% hint style="success" %}
When the `Value` changes, if `variableAttributes` are in use, then the system will automatically check to see if the existing `GameItemAttribute` needs to be updated. If so, the system will remove all `GameItemAttributes` of the `ObjectType` from the list, and add the new one based on the current `RangeValue`.

`SetStatsDirt()` will then be called on the `GameItemObject`, triggering stats to recompute their values.
{% endhint %}

## Methods

### Get Value & RangeValue

```csharp
var durability = gameItemObject.Variable("Durability");

// Get the current value
var durabilityValue = durability.value;

// Get the current RangeValue
var durabilityRangeValue = durability.RangeValue;
```

#### Example

You can use the value of durability to modify the "price" of a `GameItemObject`.

```csharp
// Get the price, perhaps it is stored using the Dictionaries module
var price = sword.dictionaries.Value<int>("Price");
var valuePrice = price * durability.RangeValue;

if (valuePrice > 0)
    Merchant.Speak($"I'll give you {valuePrice} for that {sword.FullName()}. Fair price.");
else
    Merchant.Speak($"That hunk of junk isn't worth a single ounce of my sweat!");
```

### Add Value

Use the `AddValue()` method to add value to the Variable. Add a negative value to subtract. The final Value will be clamped between `Min` and `Max`, and `RangeValue` will be updated.&#x20;

Returns the `RangeValue` of the Variable.

<pre class="language-csharp"><code class="lang-csharp">// Decrease Durability
var durability = gameItemObject.Variable("Durability");
<strong>var durabilityRangeValue = durability.AddValue(-1f); // Removes 1 from the value
</strong></code></pre>

### Set Value

Use the `SetValue()` method to set the value to a specific amount. Add a negative value to subtract. The final Value will be clamped between `Min` and `Max`, and `RangeValue` will be updated.&#x20;

Returns the `RangeValue` of the Variable.

```csharp
// Set to a specific value
var durability = gameItemObject.Variable("Durability");
var durabilityRangeValue = durability.SetValue(0.8f);

// Set to Min or Max values (Returns the RangeValue)
durability.SetToMax();
durability.SetToMin();
```

{% hint style="info" %}
You can change the values of `Min` and `Max` at runtime! While they will always start at the values set in the `ItemObject` scriptable object, you can modify them later for even more game mechanic opportunities.
{% endhint %}

### Set to a specific `RangeValue`

`RangeValue` is always between `0f` and `1f`, between the `Min` and `Max` values and modified by the curve, if in use. Rather than setting the specific `Value`, you can opt to set a `RangeValue`, and the `Value` will be computed based on that.&#x20;

Returns the `Value` of the Variable.

```csharp
// Set to 0.5, and get the new Value
var durability = gameItemObject.Variable("Durability");
var newDurabilityValue = durability.SetToRangeValue(0.5);
```
