# Stats

## Overview

The **Stats** module allows you to easily set up and access "Stat" data for your project. Creatively used, this could easily even add more functionality if `Stat` classes are used for other things, such as skills and counters (Gold, Experience, Health, Points).

A main concept of **Stats** is that each `Stat` object could potentially affect or be affected by others `Stat` objects, or other Game Module objects. These values can be set up in the Inspector, and then utilized in your project in whatever way makes sense.

{% hint style="success" %}
The final value of `Stat` objects can be affected by other Stats, Conditions, Item Objects, Item Attributes, and even Quests. **All of this is handled automatically at runtime!**
{% endhint %}

The system will automatically recalculate the final value of each `Stat` when the underlying data changes, meaning once it is set up in your code, the values will always remain up to date.

{% embed url="<https://youtu.be/pmGM3ZPo7sM>" %}

{% embed url="<https://youtu.be/i33lDZsQ4JA>" %}
\[EARLIER VERSION] Quickstart Video #1 - Creating some Stats, an Item Object and Item Attribute and linking them all up in a demo scene. The general logic has not changed, though the UI has, and there are new options now.
{% endembed %}

## Final Stat Computation

Each [**GameStat**](/magic-pig-games/game-modules-4/module-documentation/stats-and-skills/gamestat.cs.md) (the in-game version of the **Stat**), will have three numbers:

* **Points**: These provide a constant fixed amount. Generally this is used as a counter, with value being added or subtracted throughout the game.
* **Base Value**: This is similar to Points, but will combined with "value" modifications from other objects.
* **Base Proficiency**: This is a `float` default to `0.0f`, meaning "no change". The final proficiency value may be modified by other objects.

<figure><img src="/files/Kg2p5jkzuq7mjngzKspu" alt=""><figcaption></figcaption></figure>

The **Final Stat** is computed using Points, Value, and Proficiency. The **Final Value** is the sum of the Base Value and modifications from other objects which affect this stat -- [**Items**](/magic-pig-games/game-modules-4/module-documentation/items.md), [**Quests**](/magic-pig-games/game-modules-4/module-documentation/quests/quest.md), [**Conditions**](/magic-pig-games/game-modules-4/module-documentation/conditions.md), other **Stats**, etc.

**Final Proficiency** is the sum of Base Proficiency and modifications from other objects. A value of `0f` would mean "no effect".

The sum of **Points + Final Value** is then multiplied by **1 + Final Proficiency** to get the **Final Stat**.

{% hint style="success" %}
Example: Lets say our "Strength" stat has these values:\
**Points: 10**\
**Base Value: 0**\
**Base Proficiency: 0**\
\
Without anything affecting this stat, the Final Stat is:  **(10 + 0) \* (1 + 0) = 10**\
\
Lets say the Actor has the following modifications:\
\&#xNAN;**"Sword of Strength": +3 Value**\
**"Bodybuilding" Stat: +0.2 Proficiency**

**"Elf" Character Race: -0.1 Proficiency**\
\
These modifications are then included when computing the final stat. Note the "Proficiency" modifications are summed, so the final Proficiency modification is: **0.2 + -0.1 = 0.1**\
\
The Final Stat is: **(10 + 3) \* (1 + 0.1) = 13 \* 1.1 = 14.3**
{% endhint %}

If your Actors inherit from [**`GameModulesActor`**](/magic-pig-games/game-modules-4/module-documentation/game-modules-actor.md), then all of this is handled automatically!!

### Example

As an example, you may set up a `Stat` called *Experience* which may hold the experience points a player has collected. You may also set up another `Stat` called *Experience Mod* which is modifyable, but not trainable.&#x20;

<figure><img src="/files/5Kk0EWqaIqXychtS4uKe" alt=""><figcaption><p>"Experience Modifier" can be modified by other objects, and starts with a value of 0.</p></figcaption></figure>

Finally, you may set up a trainable `Stat` called *Learning*, a skill which the player could learn, and then master / upgrade, throughout the game.&#x20;

*Learning* could be set to modify *Experience Mod* per "skill point", a value which may be something players can advance throughout the game. Perhaps *Learning* provides 0.02 points per skill point to *Experience Mod*. (Thats 2% more)

<figure><img src="/files/Gc3agKXucGolxjjoKTts" alt=""><figcaption><p>Learning will modify Experience Modifier, at Novice level, by 0.09 plus 0.01 per "Point" added to the Learning stat. (That means a 10% boost to experience at the start!)</p></figcaption></figure>

In the Inspector for *Experience*, you may choose to modify any "*points*" added to the `Stat` by the *Experience Mod* you set up.&#x20;

<figure><img src="/files/ywqf6ZQSwu43SLYNRp8M" alt=""><figcaption><p>Now, whenever points are added to the Experience stat, which is being used as a "counter", the value will be modified by the Final Value of Experience Modifier. A character with Learning at Novice level with 1 skill point will get a 10% boost. Adding 100 points to Experience would result in 110 points being added, automatically.</p></figcaption></figure>

So instead of simply adding "100" to the *Experience* `Stat`, the following math is used automatically:

$$
Points = Points + (100 \* (1 + \[ExperienceMod Proficiency]))
$$

In this example, a player with 5 skill points on the *Learning* `Stat` would have a total modification to the *Experience Mod* of 0.1, and the 100 experience points will be worth 110 for this player.

{% hint style="success" %}
The goal of the `Stats` module is to make all of this relatively complex connections more easy to set up and manage, with runtime operations as automatic as possible. Time setting it all up, and thinking about how various `Stat` objects can work together, can enable a very smooth operation later on.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://infinitypbr.gitbook.io/magic-pig-games/game-modules-4/module-documentation/stats-and-skills.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
