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.
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.
Parameters of the UI_COMMAND
Macro
UI_COMMAND
MacroThe UI_COMMAND
macro is used to create an instance of a command. Here are the parameters it typically requires:
Command Handle: A reference to a
TSharedPtr<FUICommandInfo>
, used to manage the command.Command Name: A string used primarily in menus and toolbars.
Command Description: Provides a tooltip or description, explaining what the command does.
User Interface Type: Determines how the command appears and behaves in the UI.
Input Chord: Defines a keyboard shortcut that triggers the command.
Understanding EUserInterfaceActionType
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
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
, orEModifierKey::Command
(mainly for macOS).
Example of setting an input chord:
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
Consistent Naming Conventions: Use clear and consistent identifiers for your commands to ensure maintainability.
Descriptive Labels and Tooltips: Enhance user experience by providing concise and helpful descriptions.
Debugging Commands: Use Unreal's console commands like
"DumpCommands"
to help debug and test your command implementations.Context Management: If your plugin spans multiple editor areas, use distinct namespaces to avoid conflicts and confusion.
Performance Considerations: Avoid direct heavy computations in command actions. Use them to trigger asynchronous tasks or show progress dialogs.
Last updated