# Local Time Scale

## Creating and Using `LocalTimeScale`

The `LocalTimeScale` class is the thing that helps objects determine their final `TimeScale` value. There are three main ways you'll utilize Local Time Scale objects.

### 1️⃣ Create a Local Time Scale Scriptable Object

Right click in your project and select `Create/Magic Time/Time Scale` to create a new Scriptable Object.

<figure><img src="https://2431624982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MY3N_li2jPq7az6mYfq%2Fuploads%2FQxR9j10rYsisBKwcHS9P%2FScreenshot%202024-09-15%20at%206.00.34%E2%80%AFPM.png?alt=media&#x26;token=d6736721-7177-46e9-af68-0e7ce0455d63" alt=""><figcaption><p>A "Global" <code>LocalTimeScale</code> can be used to handle game-wide time scaling, including pausing.</p></figcaption></figure>

Often the pre-created objects will populate the [**Magic Time Manager**](https://infinitypbr.gitbook.io/magic-pig-games/magic-time-local-time-scale/magic-time-manager) and [**Magic Time User**](https://infinitypbr.gitbook.io/magic-pig-games/magic-time-local-time-scale/magic-time-user) lists of "Initial Time Scales".&#x20;

{% hint style="success" %}
With this pattern, all objects will be accessing the same runtime instance of these `LocalTimeScale` objects. Use this for "Global" scales, as well as time scales that are meant to effect specific types of objects, such as "Enemies" or "Projectiles".
{% endhint %}

### 2️⃣ Magic Time User and IUseMagicTime

The interface [`IHaveLocalTime`](https://infinitypbr.gitbook.io/magic-pig-games/magic-time-local-time-scale/magic-time-user/ihavelocaltime) is implemented by [`MagicTimeUser`](https://infinitypbr.gitbook.io/magic-pig-games/magic-time-local-time-scale/magic-time-user). Each Magic Time User object will automatically create its own `LocalTimeScale`, which you can think of as that objects internal clock.

{% hint style="success" %}
Any object that will be changing its time scale using Magic Time should implement `IUseMagicTime`. Inheriting from `MagicTimeUser` is likely a great option most of the time.
{% endhint %}

### 3️⃣ Create at Runtime

`LocalTimeScale` objects can be created at runtime as well. Classes like the [`TimeZone`](https://infinitypbr.gitbook.io/magic-pig-games/magic-time-local-time-scale/time-zone) demonstrate how this can be done.&#x20;

{% hint style="success" %}
Simialr to the **Time Zone** objects, creating your own `LocalTimeScale` enables more options and flexibility to help create the in-game experience you're looking for.
{% endhint %}

## Events & UnityEvents

Each `LocalTimeScale` includes `UnityEvents` and other events you can subscribe to.

<figure><img src="https://2431624982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MY3N_li2jPq7az6mYfq%2Fuploads%2FbWSB9XI44aIX90sF22xt%2FScreenshot%202024-09-15%20at%206.15.37%E2%80%AFPM.png?alt=media&#x26;token=2caade52-1ed9-4707-89ad-daeed68dd2c6" alt=""><figcaption></figcaption></figure>

### Subscribing to Unity Events at Runtime

Since these objects are Scriptable Objects, in most cases you'll need to subscribe to the `UnityEvents` at runtime, specifically subscribing to the `instance` of the Scriptable Object that is created at runtime. Your object should reference the LocalTimeScale and add itself as a listener.

```csharp
// Your script which wants to subscribe to the UnityEvent
LocalTimeScale localTimeScale;
localTimeScale.OnTimeScaleChangedUnityEvent.AddListener(OnTimeScaleChanged);

// Note the UnityEvent only provides the float value
public void OnTimeScaleChanged(float value)
{
    Debug.Log($"The new value is {value}");
}
```

### Subscribing to the Events at Runtime

The `Events` also include the `LocalTimeScale` object, which is useful if you have one object that is subscribed to multiple `LocalTimeScale` objects, and needs to know which one called the event.

<pre class="language-csharp"><code class="lang-csharp"><strong>// Your script which wants to subscribe to the Event
</strong>LocalTimeScale localTimeScale;
<strong>localTimeScale.OnTimeScaleChanged += OnTimeScaleChanged;
</strong><strong>
</strong><strong>// This event also tells you which object triggered it
</strong>public void OnTimeScaleChanged(float value, LocalTimeScale localTimeScale)
{
    HandleChangedTimeScaleValues(localTimeScale); // Do something!
}
</code></pre>

## Additional Scripting

Be sure to check out the [**Local Time Scale Scripting**](https://infinitypbr.gitbook.io/magic-pig-games/magic-time-local-time-scale/local-time-scale/local-time-scale-scripting) page with examples on how to use this class.

{% content-ref url="local-time-scale/local-time-scale-scripting" %}
[local-time-scale-scripting](https://infinitypbr.gitbook.io/magic-pig-games/magic-time-local-time-scale/local-time-scale/local-time-scale-scripting)
{% endcontent-ref %}
