How can I use this function in my C++ code? Give me an example UFUNCTION…
- User
How can I use this function in my C++ code? Give me an example
UFUNCTION (BlueprintCallable, Category="Input", Meta=(AutoCreateRefTerm="Modifiers,Triggers"))
virtual void InjectInputForAction
(
const UInputAction * Action,
FInputActionValue RawValue,
const TArray< UInputModifier * > & Modifiers,
const TArray< UInputTrigger * > & Triggers
) - Assistant
The function you're referring to:
UFUNCTION(BlueprintCallable, Category="Input", Meta=(AutoCreateRefTerm="Modifiers,Triggers")) virtual void InjectInputForAction( const UInputAction* Action, FInputActionValue RawValue, const TArray<UInputModifier*>& Modifiers, const TArray<UInputTrigger*>& Triggers )is most likely part of Unreal Engine 5’s Enhanced Input System. It allows you to simulate input for a specific input action programmatically. This can be useful for gameplay systems, AI, or automated testing scenarios where you want to trigger input actions as if a player had pressed a key, moved an axis, etc.
✅ Prerequisites
To use this method in code:
- You must ensure your class includes the necessary headers from the Enhanced Input System, such as
InputAction.h,InputModifier.h,InputTrigger.h, and so on. - The function should be called on a valid instance that owns the input component implementing this method (usually an
UEnhancedInputComponentor something similar).
💡 Simple Example in C++
Suppose you're in a
PlayerControllerorCharacterclass with anUEnhancedInputComponent:- You inject input manually — mimicking an action like "Jump" or "Fire".
Here's what it could look like:
#include "EnhancedInputComponent.h" #include "InputAction.h" #include "InputModifiers.h" #include "InputTriggers.h" #include "InputActionValue.h" // Example function in your ACharacter or APlayerController-derived class void AMyCharacter::SimulateJumpInput() { // Make sure the InputComponent is valid and cast to EnhancedInputComponent if (UEnhancedInputComponent* EnhancedInput = Cast<UEnhancedInputComponent>(InputComponent)) { // Get or reference your input action static const UInputAction* JumpAction = LoadObject<UInputAction>(nullptr, TEXT("/Game/Input/IA_Jump.IA_Jump")); // Adjust path accordingly if (!JumpAction) return; // Set up the raw value (e.g., a bool for an action like Jump) FInputActionValue JumpValue(true); // Simulate "pressed" input // Empty arrays for modifiers and triggers if not used TArray<UInputModifier*> Modifiers; TArray<UInputTrigger*> Triggers; // Inject the input (simulating player input) EnhancedInput->InjectInputForAction(JumpAction, JumpValue, Modifiers, Triggers); } }
🧠 Notes
FInputActionValueis a wrapper and can represent bool (pressed/not), float (axes), or FVector (for movement), depending on the action's configured type.UInputActioninstances should already be loaded and registered with the Input Mapping Context.- Modifiers and triggers can be added if you want to preprocess/validate the input before activation (optional).
📘 Blueprint Usage
Since the function is marked
BlueprintCallable, you could also use this in Blueprints for similar simulation — for instance, simulating input when a UI event happens.
✅ When To Use
InjectInputForAction- Simulating player input for testing or bootstrapping gameplay.
- Feeding AI or scripted input driving Gameplay Ability System or core game logic.
- Replaying inputs for network replays or tutorials.
If you provide your context (e.g., what type of input you're simulating or what system is driving this input injection), I can tailor the example further!
- You must ensure your class includes the necessary headers from the Enhanced Input System, such as
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.