# Scheduler

## Description

The *Scheduler* is an important component in *FP Motion*; its main function is to allow developers to run certain motions with delays, loops, or other 'playback' functionality.

Some components that use the *Scheduler* are:

* **Land Component.** This component uses the *Scheduler* to apply a force for a couple of seconds once the player lands. This provides a much nicer effect that applying it all at once.
* **Non-Monobehaviour Updates.** Some components use the *Scheduler* as a replacement for being a `MonoBehaviour`. The way this works is by subscribing to it, and the *Scheduler* invokes the subscribed function on an Update.
* **Complex Motions.** We will include more complex motions like reloading in the asset, the way we will make them work is by relying on the *Scheduler’s* capabilities.

{% hint style="info" %}
Usually, we will place this component on the player, or on a Manager object, and leave it there for other components to use during play mode.
{% endhint %}

## How does it work?

The inner workings of this component are fairly simple, it **receives requests** for things that need scheduling, whether they be on an update function, or after a certain period, and fulfills those requests.

{% hint style="info" %}
*FP Motion* 1.0, only has one method of scheduling. Version 2.0 will contain more!
{% endhint %}

### **Scheduling**

The way to schedule something to happen is by using the TickSchedule method, which looks the following way:

```csharp
TickSchedule(scheduledTime, duration, scheduledMethod)
```

{% hint style="info" %}
There is a variation to this function that allows you to schedule a method when the duration elapses.
{% endhint %}

All this function does is take a method, and schedule it to get **invoked** inside an Update, FixedUpdate, or whatever the developer picks as a scheduledTime.

The duration determines the time that it will stay scheduled, when that timer ends, the method is **removed** from that scheduling and stops being invoked.

## Usage

To use the *Scheduler* there is a need to first access it. We made this simple by making it a Singleton.

To access the instance of this component:

```csharp
Scheduler.Instance
```

Now the TickSchedule function can schedule methods, a good example would be:

```csharp
Scheduler.Instance.TickSchedule(ScheduledTimes.Update, 5.0f, LogThings);

private void LogThings(){
    Debug.Log("Logging in Update for 5 seconds!");
}
```

A good example of how scheduling can produce cool results is the Land component.

{% hint style="info" %}
Usually, we will place this component on the player, or on a Manager object, and leave it there for other components to access during play mode.
{% endhint %}
