# Methods

The `methods` module provides a system to register, remove, and trigger custom method callbacks on both the client and server.\
These are useful for extending resource behavior without modifying the core logic.

***

### Accessing the Module

```lua
local METHODS <const> = exports.boii_utils:get("modules.methods")
```

***

## Shared

### add\_method(event\_name, cb, options?)

Adds a method callback to a specific event.

#### Parameters

| Name        | Type     | Description                               |
| ----------- | -------- | ----------------------------------------- |
| event\_name | string   | Name of the event to hook into.           |
| cb          | function | Function to call when the event triggers. |
| options?    | table    | Optional table for method filtering/data. |

#### Returns

| Type   | Description                         |
| ------ | ----------------------------------- |
| number | ID of the method for later removal. |

#### Example

```lua
METHODS.add("on_some_event", function(data)
    if data.thing == "important" then
        print("Handled important thing")
    end
end)
```

***

### remove\_method(event\_name, id)

Removes a method callback from an event.

#### Parameters

| Name        | Type   | Description                      |
| ----------- | ------ | -------------------------------- |
| event\_name | string | The event name to remove from.   |
| id          | number | ID returned from `add_method()`. |

#### Example

```lua
local id = METHODS.add("custom_event", function(data) end)
METHODS.remove("custom_event", id)
```

***

### trigger\_method(event\_name, response)

Triggers all registered methods for an event. If any method returns `false`, execution halts.

#### Parameters

| Name        | Type   | Description                               |
| ----------- | ------ | ----------------------------------------- |
| event\_name | string | The event to trigger.                     |
| response    | table  | The table passed to all method callbacks. |

#### Returns

| Type    | Description                                                 |
| ------- | ----------------------------------------------------------- |
| boolean | `false` if any callback returned `false`, otherwise `true`. |

#### Example

```lua
local success = METHODS.trigger("custom_event", { player = source })
if not success then return end
```


---

# 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://docs.boii.dev/old-docs/boii_utils/api/methods.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.
