# Observer

### Repository

<https://github.com/didier-v/GodotNotificationCenter>

### Code

The following thing lives in the global space and delivers messages from senders to observers.

```gdscript
extends Node

var notifications

func _ready():
	notifications = {};

func post_notification(notificationName,notificationData):
	if notifications.has(notificationName):
		var currentObservers=notifications[notificationName].observers
		for i in currentObservers:
			var anObserver =  currentObservers[i]
			if anObserver.object.has_method(anObserver.action):
				anObserver.object.call(anObserver.action,anObserver.object,notificationName,notificationData)

func add_observer(observer,notificationName,action):
	if not notifications.has(notificationName):
		notifications[notificationName]={
			"observers":{}
		}
	var currentObservers=notifications[notificationName].observers
	currentObservers[observer.get_instance_id()]={
		"object":observer,
		"action":action
	}

func remove_observer(observer, notificationName):
	if notifications.has(notificationName):
		var currentObservers=notifications[notificationName].observers
		if currentObservers.has(observer.get_instance_id()):
			currentObservers.erase(observer.get_instance_id())

```

### Usage

From the place where observer (or several of them) is accessible with `get_node` add it to `NotificationCenter`:

```gdscript
func _ready():
	NotificationCenter.add_observer($State, "state", "led_status")
```

Add action to the observer meaning you need function with a specific name and specific interface

```gdscript
func led_status(observer, notification_name, notification_data):
	# do your thing with `notification_data`
```

Now from any other place (node, scene) you can send data to the observers and trigger the action with:

```gdscript
NotificationCenter.post_notification("led_status", "scatter")
```


---

# 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://neupokoev-n.gitbook.io/godot-recipes/patterns/observer.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.
