Code Examples

v4.0

Setting the "Age" of characters

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

NewGameSetup.cs
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);
}

Subscribe to TimeMod

In this example, a UI element subscribes to the TimeMod of the Timeboard and updates the text value when the TimeScale changes.

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}";
}

Last updated