> ## Documentation Index
> Fetch the complete documentation index at: https://ugcore.urging.ch/llms.txt
> Use this file to discover all available pages before exploring further.

# UgCore.Events

> Local event bus with spoofing protection for ug-core events.

<Badge color="purple">Shared</Badge> Runs in your resource.

## On

```lua theme={null}
UgCore.Events.On(name, handler) -> id
```

<ResponseField name="name" type="string" required>Event name. Use `UgCore.Enums.Events` values for core events.</ResponseField>
<ResponseField name="handler" type="fun(...)" required>Called with the event arguments.</ResponseField>
<ResponseField name="id" type="integer">Pass to `Off` to remove the handler.</ResponseField>

`ug-core:*` events are dropped unless ug-core triggered them. Dropped events log at Debug.

## Once

```lua theme={null}
UgCore.Events.Once(name, handler) -> id
```

Like `On`, but the handler runs at most once. A dropped spoofed event does not use it up.

## Off

```lua theme={null}
UgCore.Events.Off(id) -> removed
```

<ResponseField name="removed" type="boolean">`false` for unknown or already removed ids.</ResponseField>

## Emit

```lua theme={null}
UgCore.Events.Emit(name, ...)
```

<ResponseField name="name" type="string" required>MUST start with your resource name, such as `my-shop:Purchased`. Raises otherwise.</ResponseField>

Uses a local event: every resource on the same side receives it.

## Example

```lua theme={null}
local id = UgCore.Events.On(UgCore.Enums.Events.Lifecycle.Changed, function(newState, oldState)
    UgCore.Logger.Info('ug-core: %s -> %s', oldState, newState)
end)

UgCore.Events.Emit('my-shop:Opened', source)
```

See every core event in the [Events reference](/reference/events).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.