# Observers, Global Observers, and Observer Objects

Observers are passive watchers of lifecycle events, and can act depending on what they see. [**Observer Objects**](#observer-object-example) are `Monobehaviours` which can watch a Projectile.

## Observer Scriptable Object

The `Observer` class derives from `ProjectileBehavior`, as do all of the other [**Behavior**](/magic-pig-games/projectile-factory/projectile-factory-documentation/behaviors.md) classes, giving it access to lifecycle events on Projectiles. While behaviors aim to act upon the Projectile itself, an Observer is intended to act upon something external to the Projectile.

The *Demo Actor Observer* in the demo scene is used to recognize when a projetile fired by the "Player" `Actor` collides with a "Target" `Actor` in order to compute and transfer damage to the target.

The *Turn Off On Collision Observer* is used to turn off a `GameObject` whenever a specific Projectile collides with something.

{% hint style="info" %}
Observers are a great way to hook into the Projectile Factory system, and connect your game to the lifecycle events. This means you'll likely want to create your own Observers. Check out the [**Create Custom Behaviors**](/magic-pig-games/projectile-factory/projectile-factory-documentation/create-custom-behaviors.md) page for more details on how (easy it is) to create custom behaviors and observers.
{% endhint %}

There are two ways to attach an Observer `ScriptableObject` to a `Projectile`.

### Attach via the ProjectileSpawner

The [**Projectile Spawner**](/magic-pig-games/projectile-factory/projectile-factory-documentation/projectile-spawner.md) has an Observers `List<Observer>`. Each Projectile that is created by this Spawner will be given an instance of each Observer in the list. This is how the *Demo Actor Observer* is added to all Projectiles the player spawns.

## Global Observer

A **Global Observer** is an object that intends to be interested in Projectiles spawned by more than just one `ProjectileSpawner`. In the demo scenes, the `Camera` has a `GlobalObserver` class on it, which shakes the camera anytime a projectile collides.

**Global Observers** subscribe to the [**Factory Manager**](/magic-pig-games/projectile-factory/projectile-factory-documentation/factory-manager.md), which repeats events from the Projectiles registered to it.

## Observer Example

The **Projectile Spawner** also has one **Observer** assigned to the `Observers` list.

<figure><img src="/files/tEPAgkuP6NhKf3GsTF83" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/LzYZ2hTdavhyGpmLSeiP" alt=""><figcaption></figcaption></figure>

This observer has no exposed parameters. As an `ProjectileObserver`, it automatically subscribes to all lifecycle events on each Projectile spawned -- this class inherits from `ProjectileBehavior`, as do all of the [**Behaviors**](/magic-pig-games/projectile-factory/projectile-factory-documentation/behaviors.md).

Whenever `CollisionEnter` or `TriggerEnter` is called by a Projectile, the `HitObject` method on this observer is called. If the `HitObject` has a `ProjectileDemoActor` component, then the `Actor` who spawned the projectile will run the `RegisterHit` method, which computes damage and passes it to the hit `Actor`.&#x20;

{% hint style="info" %}
This **Projectile Observer** is a demo class, intended to showcase one way of connecting your project to the **Projectile Factory** events. Each project is unique, and you can choose for yourself the most efficient way to get the information you need to process data for your game.
{% endhint %}

## Global Observer Example

The [**Factory Manager**](/magic-pig-games/projectile-factory/projectile-factory-documentation/factory-manager.md) exists in all scenes, as it is a `Static Scriptable Object`. This manager maintains a `List` of all `ProjectileSpawner` and `Projectile` objects in the scene, along with [**global observers**](/magic-pig-games/projectile-factory/projectile-factory-documentation/observers-global-observers-and-observer-objects.md), which are `ProjectileObserver` objects.

In the demo scene, the `Camera` has a `CameraShakeOnProjectileCollision` component on it. This class inherits from `GlobalObserver`. This means it automatically subscribes to the `FactoryManager` lifecycle events.

The class overrides the `CollisionEnter` method, which is called whenever ANY projectile that is registered with the `FactoryManager` collides.

```csharp
protected override void CollisionEnter(Projectile basicProjectile, Collision collision, GameObject objectHit = null, Vector3 contactPoint = default)
{
     var valueOnCurve = DistanceValueOnCurve(contactPoint);
     ShakeCamera(shakeDuration * valueOnCurve, shakeMagnitude * valueOnCurve, shakeFrequency * valueOnCurve);
}
```

Whenever this occurs, the method will shake the camera, with the shake impact determined by the distance the collision is from the camera itself.

## Observer Object Example

**Observer Objects** are `MonoBehaviours` which connect to an external **Projectile**, and have access to all the lifecycle events on that **Projectile**. Each one will derive from `ObserverObject`.

<figure><img src="/files/Op0XeGL4KFUurLeOJ7UY" alt=""><figcaption></figcaption></figure>

In the example above, the [**`ToggleObjectAtEventsObserverObject`**](/magic-pig-games/projectile-factory/projectile-factory-documentation/observers-global-observers-and-observer-objects/toggle-objects-at-events-observer-object.md) will turn the list of objects on and off at the defined events.

{% hint style="success" %}
I prefer to include "ObserverObject" in the class name. It's easier to find, and more clear what kind of class it is.
{% endhint %}

## Spawner Observer Object Example

**Spawner Observer Objects** are `MonoBehaviours` which connect to a `ProjectileSpawner`, and have access to all the lifecycle events on that spawner. Each one will derive from `SpawnerObserverObject`.

<figure><img src="/files/yXdg2AxepoIGGvhSOkAw" alt=""><figcaption></figcaption></figure>

The SpawnOnSpawnPointsSpawnerObserverObject will spawn objects onto one or all of the Spawn Points at the events you select. In the example above, the objects will be spawned when the projecitles start shooting, and will be destroyed when they stop shooting. We use it in one of the Integraitons to show a "charging" particle, along with a Projectile that has a 0.5 second delay.

<figure><img src="/files/bgo34LlDyvk3mylosRQD" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://infinitypbr.gitbook.io/magic-pig-games/projectile-factory/projectile-factory-documentation/observers-global-observers-and-observer-objects.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
