# Habits Module

## Overview

Habits overrides the calendar section of the [Time Module](time-module) allowing for more precise and extensible events within the COZY ecosystem. Automatically setup repeat patterns, start and end times, multi-day events, routines, and multiple calendars all without leaving the COZY editor. Habits is a "no-code-required" event system, but you can also extend in it easily with Habit listeners via the [API](#api).

### Key Features

* Setup robust events
* Manage time with a realistic Gregorian calendar and weekdays.
* Link events to your world using the Habit Listener script

## Profiles

### Habits Year

<figure><img src="https://2041269432-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrqKsGNTyQFMrNJSWMHS0%2Fuploads%2FJtV7Yf22lTXn8vducWA5%2Fimage.png?alt=media&#x26;token=b9d8f8c7-8bda-4944-9cbe-d8834af26de5" alt=""><figcaption></figcaption></figure>

The **Habits Year Profile** holds references to all of the months of the year (with display names and number of days), the start day of the week, and a list of all of the events used by this calendar. This profile is the core of Habits and provides the module with most of the setup data.

### Cozy Habit Profile

<figure><img src="https://2041269432-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrqKsGNTyQFMrNJSWMHS0%2Fuploads%2FHwZLJsRG0me19lUMhaSC%2Fimage.png?alt=media&#x26;token=f9784ec9-2a04-4855-bb40-91475fe3e899" alt=""><figcaption></figcaption></figure>

The **Cozy Habit Module** defines the actual events or habits that are triggered in your world. Define repeating patterns, start and end dates, start and end times, and determine when the Habit event is triggered.

While these can be edited in the profiles, it is best to edit them in the actual module UI.

## Usage Examples

### Understanding the UI

Habits is visible in three different sections that all have different levels of detail and have pros and cons when it comes to editing.

#### The Toolbar

Each view has a toolbar in the bottom-right corner that lets you add a Habit (new or existing), see and edit the full list of active Habits, and edit the global editor settings to show/hide calendars.

#### Month View

See the whole month at a glance. See the number of events on all of the days of the month as well as track the weekdays. Select a new day on the calendar to swap to that day. Select the month name at the top of the screen to change the month or use the \[ + ] and \[ - ] buttons to raise or lower the current month.

<figure><img src="https://2041269432-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrqKsGNTyQFMrNJSWMHS0%2Fuploads%2FfuVx4Yhn7QxtsSjEaEAA%2Fimage.png?alt=media&#x26;token=991fc3b0-5094-4411-8229-a0fa7d698a4d" alt=""><figcaption></figcaption></figure>

#### Week View

See the current week at a glance. See a few details about each day and look at the Habits that play on each. The white line across all of the days shows you the current time, clicking the day initial will swap to that day and clicking on each Habit's name will let you edit that Habit.

<figure><img src="https://2041269432-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrqKsGNTyQFMrNJSWMHS0%2Fuploads%2F7Ft8KrgVdjZtOVx3623W%2Fimage.png?alt=media&#x26;token=a891794b-f4dc-4279-acd5-99b43083a93d" alt="" width="563"><figcaption></figcaption></figure>

#### Day View

See all the Habits for today in detail. The white line across all of the days shows you the current time, clicking the plus and minus buttons will increase and decrease the current day, and clicking on each Habit's name will let you edit that Habit.

<figure><img src="https://2041269432-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrqKsGNTyQFMrNJSWMHS0%2Fuploads%2FA1f0nIfN0v1ffLsJdAKc%2Fimage.png?alt=media&#x26;token=ee1d0fb1-8beb-4e25-ab64-fbe4c22288f3" alt="" width="563"><figcaption></figcaption></figure>

### Habits Listeners

**COZY Habit Listeners** let you link your Habits directly to Unity Events in the inspector with no code.

Select the Habit that you want to use for the event in the first field and setup your Unity Events in the fields below it. Start Event will be called when the Habit starts playing, Update Event is called every frame that the Habit is active, and End Event is called the last frame that the Habit is active.

<figure><img src="https://2041269432-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrqKsGNTyQFMrNJSWMHS0%2Fuploads%2FN3Gr8kraiiQxduljb10b%2Fimage.png?alt=media&#x26;token=2c710c14-042b-4e9f-94ca-ddd1f9ffce94" alt="" width="563"><figcaption></figcaption></figure>

## API

### C# Event Listeners

You can also set up events using C# instead of Cozy Habit Listeners. This is the format that you need to follow to include Habits in your scripts

{% code lineNumbers="true" fullWidth="true" %}
```csharp
//Be sure to include the Cozy Namespace
using DistantLands.Cozy;

//...
    
//Reference your habit here:
public CozyHabitProfile habitToUse;

//Add your methods to the event listener when the object is enabled and clean them up when the object is disabled:
void OnEnable()
{
    habitToUse.onStart += StartMethod;
    habitToUse.onUpdate += UpdateMethod;
    habitToUse.onEnd += EndMethod;
}
void OnDisable()
{
    habitToUse.onStart -= StartMethod;
    habitToUse.onUpdate -= UpdateMethod;
    habitToUse.onEnd -= EndMethod;
}


public void StartMethod()
{
    //This method will run when the habit starts.
}
public void UpdateMethod()
{
    //This method will run every frame during the time that the habit is running.
}
public void EndMethod()
{
    //This method will run when the habit ends.
}
```
{% endcode %}
