> 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/gametime-and-timeboard/code-examples.md).

# Code Examples

## Setting the "Age" of characters

Gametime works with negative `gametime` values as well. You can set up random ages with code like this.

{% code title="NewGameSetup.cs" %}

```csharp
using static InfinityPBR.LegendOfTheStones.Game;
using static InfinityPBR.Modules.Timeboard;
using static InfinityPBR.Modules.StatsRepository;

//.....

foreach (Player player in game.data.players)
{    
     var years = 18 * timeboard.gametime.GameTimePerYear;
     var months = timeboard.gametime.GameTimePerMonth 
          * Random.Range(0, timeboard.gametime.MonthsPerYear - 1);
     var days = timeboard.gametime.GameTimePerDay 
          * Random.Range(0, timeboard.gametime.DaysPerMonth - 1);
     var age = years + months + days * -1;
            
     // Use the "Properties" feature of Game Modules to easily grab
     // the uid of the Stat scriptable object.
     var ageStatUid = Properties.Stats.MainStats.AgeUid;
     
     // Set the age to be the gameTime value for when the player was born
     player.stats.Get(ageStatUid).SetPoints(age);
     
     // ...
     // Later we can convert the gameTime value to years
     // This subtracts the birth gameTime (Stored in the stat "Age") from 
     // the "Now()" gametime, and gets the year from the result gameTime.
     // We call "false" at the end to avoid adding the startYear to the
     // year value.
     var agePoints = player.stats.Get(ageStatUid).Points;
     var playerAgeInYears 
            = timeboard.gametime.GetYear((Now(false) - agePoints), false);
}
```

{% endcode %}

## Subscribe to TimeMod

In this example, a UI element subscribes to the `TimeMod` of the [**Timeboard**](/magic-pig-games/game-modules-4/module-documentation/gametime-and-timeboard/timeboard.md) and updates the text value when the TimeScale changes.

```csharp
public class TimeScaleDisplay : MonoBehaviour
{
    [SerializeField]
    private Text timeScaleText;
    private TimeMod timeMod => Timeboard.timeboard.timeMod;

    private void OnEnable()
    {
        if (timeMod != null && timeScaleText != null)
        {
            timeMod.OnTimeScaleChanged += UpdateTimeScaleText;
            UpdateTimeScaleText(timeMod.TimeScale); // Initialize display
        }
    }

    private void OnDisable()
    {
        if (timeMod != null && timeScaleText != null)
            timeMod.OnTimeScaleChanged -= UpdateTimeScaleText;
    }

    private void UpdateTimeScaleText(float newTimeScale) =>
        timeScaleText.text = $"Time Scale: {newTimeScale:F2}";
}
```
