# Events and Actions

We have seen that State-machines provide a simple framework to model complex-dynamic systems such as games and simulations because many dynamic systems can be represented using a set of states, events, and transitions. We can also consider Actions as a component of State-Machines, where an event may trigger a set of actions to be executed, where any number of these actions can also be considered to trigger an event.

## Events

When viewing a dynamic system through a State-machine lens: "an event is something that happens which affects the system." [Wikipedia](https://en.wikipedia.org/wiki/UML_state_machine#Events)

### C# Event

Events enable a [class](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/class) or object to notify other classes or objects when something of interest occurs. The class that sends (or *raises*) the event is called the *publisher* and the classes that receive (or *handle*) the event are called *subscribers*. [- MSDN](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/)\
Events are a special kind of **multicast delegate** that **can only be invoked from within the class or struct where they are declared** (the publisher class). If other classes or structs subscribe to the event, their event handler methods will be called when the publisher class raises the event. [MSDN](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/event)

## Actions

A state machine responds to events by performing actions.

For example: changing a variable, performing I/O, invoking a function, generating another event instance, or changing to another state. Any parameter values associated with the current event are available to all actions directly caused by that event. [Wikipedia](https://en.wikipedia.org/wiki/UML_state_machine#Actions_and_transitions)

## Event-Driven Programming

Steven Ferg has written an excellent paper, [Event-Driven Programming Introduction, Tutorial, History](http://eventdrivenpgm.sourceforge.net/), which discusses the complexities of programming to create event-driven systems.

## Event Notification

Some gameObjects know that they need to be notified when another gameObject does something. If both objects are in the same scene throughout the scene execution lifetime, then we can create an object reference to the gameObject that wants to be notified of an event, and we can create a method in that gameObject's custom script, that we can call from the object where the event occurs:

GameObject someSubscribeObject = GameObject.Find("SomeObject").GetComponent();

someSubscribeObject.NotifyMe();

## Event Handlers

Delegates define the syntax for Event handlers.


---

# 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://kdoore.gitbook.io/cs2335/f20_bkup_v2/optional-supplemental-content/optional-content/events_and_actions.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.
