# Step One: Set up commands

Creating commands in Unreal Engine is a foundational step in extending the editor's functionality, particularly when developing plugins that interact with the user interface. Commands serve as the backbone for user interactions, such as clicking buttons or selecting menu options, enabling developers to encapsulate complex operations into user-friendly actions.

**Step 1: Define Your Commands Class**

First, define a class that inherits from `TCommands`. This class acts as a container for all the commands associated with a specific part of your plugin or module.

```
// MyModuleCommands.h
#pragma once

#include "CoreMinimal.h"
#include "Framework/Commands/Commands.h"
#include "MyModuleStyle.h"  // Include your module's style set if available

class FMyModuleCommands : public TCommands<FMyModuleCommands>
{
public:
    FMyModuleCommands()
        : TCommands<FMyModuleCommands>(TEXT("MyModule"), NSLOCTEXT("Contexts", "MyModule", "My Module Commands"), NAME_None, FMyModuleStyle::GetStyleSetName())
    {
    }

    // Function to register commands
    virtual void RegisterCommands() override;

public:
    TSharedPtr<FUICommandInfo> OpenPluginWindow;
};
```

**Explanation:**

* `TCommands<FMyModuleCommands>`: The template is instantiated with your command class, providing necessary command handling functionalities.
* **Constructor Parameters**:
  * **Namespace** (`"MyModule"`): Defines a unique namespace for your commands to prevent conflicts.
  * **Localized Text Context**: Helps in localizing the command's text, making your plugin support multiple languages.
  * **Style Set Name**: Links to your module’s visual style settings.

**Step 2: Implement the Commands**

Define the specific actions that each command will perform in the `RegisterCommands` function of your commands class.

```cpp
cppCopy code// MyModuleCommands.cpp
#include "MyModuleCommands.h"

#define LOCTEXT_NAMESPACE "FMyModule"

void FMyModuleCommands::RegisterCommands()
{
    UI_COMMAND(OpenPluginWindow, "Open Window", "Bring up the plugin window", EUserInterfaceActionType::Button, FInputChord());
}

#undef LOCTEXT_NAMESPACE
```

#### Parameters of the `UI_COMMAND` Macro

The `UI_COMMAND` macro is used to create an instance of a command. Here are the parameters it typically requires:

1. **Command Handle**: A reference to a `TSharedPtr<FUICommandInfo>`, used to manage the command.
2. **Command Name**: A string used primarily in menus and toolbars.
3. **Command Description**: Provides a tooltip or description, explaining what the command does.
4. **User Interface Type**: Determines how the command appears and behaves in the UI.
5. **Input Chord**: Defines a keyboard shortcut that triggers the command.

#### Understanding `EUserInterfaceActionType`

The `EUserInterfaceActionType` parameter specifies the type of UI element the command is associated with and influences how it is displayed or interacted with in the UI. Here are the main options:

* **Button**: Simple clickable button.
* **ToggleButton**: A button that retains its pressed state until clicked again.
* **RadioButton**: A toggle button that is part of a group where only one item can be active at a time.
* **Check**: Similar to a toggle button but typically used for settings that can be turned on or off.

These types help define the behavior of UI components that are linked to the command, ensuring that the user interface reflects the state and function of the command appropriately.

#### Exploring `FInputChord`

The `FInputChord` parameter allows you to define a keyboard shortcut that activates the command. This is particularly useful for providing quick access to frequently used commands. An `FInputChord` can be constructed using key codes and modifier keys:

* **Key Code**: Such as `EKeys::F1`, `EKeys::Delete`, etc.
* **Modifiers**: Include keys like `EModifierKey::Control`, `EModifierKey::Alt`, `EModifierKey::Shift`, or `EModifierKey::Command` (mainly for macOS).

Example of setting an input chord:

```cpp
FInputChord EKeys::H, EModifierKey::Control | EModifierKey::Alt)
```

This defines a shortcut that triggers when the user presses Ctrl+Alt+H.

**Explanation:**

* `UI_COMMAND`: This macro simplifies the definition of a command. It sets up:

  * **Command Handle**: (`OpenPluginWindow`)
  * **Command Name and Description**: For display in UI elements and tooltips.
  * **UI Type and Shortcut**: Determines how the command is presented in the UI and any default keyboard shortcuts.

  **Constructor Parameters**:

  * **Namespace** (`"MyModule"`): Defines a unique namespace for your commands to prevent conflicts.
  * **Localized Text Context**: Helps in localizing the command's text, making your plugin support multiple languages.
  * **Style Set Name**: Links to your module’s visual style settings.

#### Additional Information on Commands

**Understanding the Importance of Commands**

Commands abstract the action logic from UI elements, making it easy to manage and update:

* **Consistency**: Use commands across multiple UI elements ensuring consistent behavior and appearance.
* **Maintenance**: Centralizing command logic simplifies updates and maintenance.

**Command Registration and Execution Flow**

Commands are registered during the module's startup to ensure they're available as soon as the editor loads. This registration is typically handled in the `StartupModule` function of your module.

#### Tips and Tricks

1. **Consistent Naming Conventions**: Use clear and consistent identifiers for your commands to ensure maintainability.
2. **Descriptive Labels and Tooltips**: Enhance user experience by providing concise and helpful descriptions.
3. **Debugging Commands**: Use Unreal's console commands like `"DumpCommands"` to help debug and test your command implementations.
4. **Context Management**: If your plugin spans multiple editor areas, use distinct namespaces to avoid conflicts and confusion.
5. **Performance Considerations**: Avoid direct heavy computations in command actions. Use them to trigger asynchronous tasks or show progress dialogs.


---

# 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://mohamed-elbeskeri.gitbook.io/unreal-editor-extension/step-one-set-up-commands.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.
