Hooks: run your code at the right moment

A hook lets your plugin respond when Pulse does something, or change a value Pulse is about to use. You write the extra code in your plugin. You do not need to edit the engine's files.

Start with your first plugin so you have an installed plugin to edit. That lesson ends by switching the plugin off. Turn it back on before trying these examples:

docker compose exec -u www-data backend php artisan hub:enable hello-pulse

The examples below belong in backend/app/Plugins/hello-pulse/hooks.php if you followed that tutorial. Keep any existing listeners and add these underneath them.

Two kinds of hook

Kind What you do Example
Action Perform a task when something happens. Write a log entry when a user is created.
Filter Receive a value, change it, and return it. Change a label in the admin menu.

A listener is the function you attach to a hook. A payload is the data that function receives. Pulse passes one value to each listener. That value might be an array, a string, or an object: check the hook you are using.

Example 1: log a new user

Hook: user.created — an action.

When: after the core User model creates a database record. This includes more than public registration: code that creates a core user can also trigger it.

Input: an array containing a user object. Think of it as ['user' => the newly created user].

Add this to your plugin's hooks.php. If the file already begins with <?php, keep only one opening tag.

<?php

app('hook')->addAction('user.created', function (array $payload): void {
    $user = $payload['user'];

    logger()->info('My plugin saw a new user', ['user_id' => $user->id]);
});

What changes: before, your plugin writes no message. After, creating a user with ID 42 writes My plugin saw a new user with user_id: 42 in the backend log.

Return: nothing. Returning a different user would not replace the saved user.

Try this by creating a test account in your local app, then reading the latest log in backend/storage/logs/. No message appears just from saving hooks.php: you must trigger the event after enabling the plugin.

Source: User::booted() (backend/app/Core/Models/User.php).

Example 2: rename an admin menu section

Hook: admin.sidebar — a filter.

When: Pulse builds the admin sidebar through AdminSidebarService.

Input: the list of menu sections directly, not ['sections' => ...]. A section looks like ['id' => 'configuration', 'label' => 'Configuration', ...].

Add this below the previous listener in the same hooks.php file:

app('hook')->addFilter('admin.sidebar', function (array $sections): array {
    foreach ($sections as $index => $section) {
        if (($section['id'] ?? '') === 'configuration') {
            $sections[$index]['label'] = 'Game Settings';
        }
    }

    return $sections;
});

What changes: refresh the admin page. The section label changes from Configuration to Game Settings. Its existing links still point to the same pages.

Return: the entire list of sections, including the ones you did not change. Returning only your changed section would replace the whole list with the wrong shape.

Sources: AdminSidebarService (backend/app/Core/Admin/AdminSidebarService.php), CoreAdminMenu (backend/app/Core/Admin/CoreAdminMenu.php).

Example 3: react to a minute pulse

Hook: pulse.minute — an action.

When: Pulse processes a minute tick. A tick is a scheduled opportunity to run game tasks; opening a page does not cause this hook by itself.

Input: the context array directly. It contains interval, timestamp, iso, minute, hour, and day. For example, $context['interval'] is 'minute' and $context['minute'] is a number such as 30. Do not look for $context['context'].

app('hook')->addAction('pulse.minute', function (array $context): void {
    logger()->info('My plugin received a minute tick', [
        'interval' => $context['interval'],
        'minute' => $context['minute'],
    ]);
});

What changes: processing a minute tick adds a log entry. This example does not change player stats.

Return: nothing.

To try one tick immediately in your local development game, run from the repository root:

docker compose exec backend php artisan pulse:run --interval=minute --sync

This runs every enabled listener for that interval, including other plugins' game tasks. Normal scheduled processing also needs a running Laravel scheduler and a queue worker that handles the ticks queue.

Sources: PulseService::executePulse() (backend/app/Core/Services/PulseService.php), PulseCommand (backend/app/Console/Commands/PulseCommand.php).

Which listener runs first?

The default priority is 10. Higher numbers run first.

app('hook')->addFilter('admin.sidebar', function (array $sections): array {
    // Make your change here.
    return $sections;
}, priority: 20);

This listener runs before one with priority 10. In a filter, the next listener receives the previous listener's returned value. Returning null leaves the value unchanged. Prefer explicitly returning the complete value.

A hook name in a list does not mean it runs

Pulse has both a hook catalogue (HookRegistry) and code that actually runs hooks (HookService). Adding a catalogue entry or a listener does not make an event happen. Some other code must dispatch it.

Name Verified behaviour in core
user.created Dispatched by the core user model; receives ['user' => $user].
admin.sidebar Applied by the admin sidebar service; receives the sections array.
pulse.minute Dispatched by the pulse service; receives the context array.
moduleLoad Registered/defined, but no core dispatcher was found. Do not use it to intercept page loading.
auth.registered Defined in the catalogue, but no core dispatcher was found. Use the verified user.created example for user creation.

The Gangster Legends moduleLoad example describes another engine's behaviour. Pulse does not currently provide that same page-loading behaviour through this name. The two core catalogue locations even describe different moduleLoad inputs, so there is no reliable runtime contract to demonstrate yet.

When learning a new hook, find the code that calls action('name', ...), filter('name', ...), or run('name', ...). That call shows when it runs, the actual input, and whether its return value is used. Custom plugins may dispatch additional hooks.

If nothing happens

Check that your plugin is enabled, the filename is exactly hooks.php, and you have triggered the right event. Check the backend log for Hook '...' callback failed; Pulse catches listener errors, so the rest of the page may still work. See troubleshooting for the commands to check registration and clear caches.

The loader also accepts return ['hook.name' => function (...) { ... }]; files. Use one registration style for a listener so it is not registered twice.

For the runtime methods and failure handling, see HookService (backend/app/Core/Services/HookService.php). Plugin hook files are loaded by AutoPluginHookLoader (backend/app/Core/Providers/AutoPluginHookLoader.php).