# Hooks

Utility Objectify uses **hooks** to define and control the lifecycle of object scripts and to support plugin-style behaviors. Hooks are executed at specific moments in an entity’s lifecycle and can be overridden or extended.

***

### <mark style="color:purple;">OnRegister(coords, rotation)</mark>

<mark style="color:blue;">`CLIENT`</mark>

Triggered when an entity is registered (even if not rendered), this can be used for purposes such as blip creation.\
At this stage of execution, the entity and classes instances do not yet exist and so any `UtilityNet.` function will not work as expected.

The first and second parameters are the `coords` and `rotation` of the entity

{% hint style="info" %}
can access only [#temp-object-properties](https://utility-2.gitbook.io/utility-objectify/client/object-management#temp-object-properties "mention")
{% endhint %}

### <mark style="color:purple;">OnUnregister(coords, rotation)</mark>

<mark style="color:blue;">`CLIENT`</mark>

Triggered when an entity is about to be unregistered or removed from the system.\
Utilize this hook to execute any necessary cleanup of resources or de-initialization processes.\
This ensures that no residual data or active references remain that could lead to memory leaks or unexpected behavior.

The first and second parameters are the `coords` and `rotation` of the entity

{% hint style="info" %}
can access only [#temp-object-properties](https://utility-2.gitbook.io/utility-objectify/client/object-management#temp-object-properties "mention")
{% endhint %}

### <mark style="color:purple;">OnAwake</mark>

<mark style="color:blue;">`CLIENT`</mark> <mark style="color:red;">`SERVER`</mark>

Triggered just before an entity is spawned.\
this is useful for initialization or setup before [#onspawn](#onspawn "mention").

### <mark style="color:purple;">OnSpawn</mark>

<mark style="color:blue;">`CLIENT`</mark> <mark style="color:red;">`SERVER`</mark>

Triggered right after the entity is rendered/spawned.\
This occurs following the [#onawake](#onawake "mention") event.

### <mark style="color:purple;">AfterSpawn</mark>

<mark style="color:blue;">`CLIENT`</mark> <mark style="color:red;">`SERVER`</mark>

Triggered after all scripts and plugins have been loaded.\
Ideal for logic that relies on complete initialization.

### <mark style="color:purple;">OnDestroy</mark>

<mark style="color:blue;">`CLIENT`</mark> <mark style="color:red;">`SERVER`</mark>

Triggered when the entity is no longer rendered/deleted.\
Here you can run cleanup processes such as disabling listeners, removing UI components, and any other teardown logic.

### <mark style="color:purple;">OnStateChange(key, value)</mark>

<mark style="color:blue;">`CLIENT`</mark>

Triggered whenever **ANY** state value changes.\
Provides the key and value of the changed state as arguments

***

### 🧰 Hook Execution Order

1. ~~`OnRegister`~~ <mark style="color:blue;">`CLIENT ONLY`</mark>
2. `OnAwake`
3. `OnSpawn`
4. `AfterSpawn`
5. `OnDestroy` (on unrender or deletion)
6. ~~`OnUnregister`~~ <mark style="color:blue;">`CLIENT ONLY`</mark>

Plugins follow the same order but are initialized after the main script.


---

# 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://utility-2.gitbook.io/utility-objectify/shared/hooks.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.
