The following guide shows how to set up player input using Verse in your Unreal Editor for Fortnite project. For more details on what player input is and the event lifecycle, check out Player Input.
Adding Player Input
Follow these steps to add player input to your project.
Import the input modules.
Verseusing { /Verse.org/Input } using { /Fortnite.com/Input/Character }Call
GetPlayerInput, which is a failable function, and add a mapping to activate its input actions. This example usesTraversalMapping.Verseif (PlayerInput := GetPlayerInput[Player]): PlayerInput.AddInputMapping(TraversalMapping)Use
GetInputEventswith a specific input action to get its event container, then subscribe to the event you want. This example subscribes theOnJumphandler function to theTriggerActivationEventevent from the Jump input action.Verseif (PlayerInput := GetPlayerInput[Player]): PlayerInput.AddInputMapping(TraversalMapping) PlayerInput.GetInputEvents(Jump).TriggerActivationEvent.Subscribe(OnJump)Define the handler function whose signature matches the event payload. In this example, the payload is of type
tuple(player, logic).VerseOnJump(Payload : tuple(player, logic)) : void = Print("Player jumped!")Subscribereturns acancelable. Store it in a variable and callCancel()when you no longer need to receive events.Versevar JumpSubscription : ?cancelable = false # Later, when subscribing: if (PlayerInput := GetPlayerInput[Player]): Sub := PlayerInput.GetInputEvents(Jump).TriggerActivationEvent.Subscribe(OnJump) set JumpSubscription = option{Sub} # When done listening: if (Sub := JumpSubscription?): Sub.Cancel()Use
RemoveInputMappingto deactivate a set of input actions. Once removed, input actions within that mapping stop producing events.Verseif (PlayerInput := GetPlayerInput[Player]): PlayerInput.RemoveInputMapping(RangedWeaponMapping)
Complete Example
The following example demonstrates a device that subscribes all players to Jump and WeaponPrimary events, then cleans up after a set duration.
using { /Fortnite.com/Devices }
using { /Fortnite.com/Input/Character }
using { /Verse.org/Input }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
input_example_device := class(creative_device):
var JumpSub : ?cancelable = false
var FireSub : ?cancelable = false
Tips for Coding Player Input
Keep the following tips in mind when working with player input in Verse:
Always add the input mapping before subscribing to its actions. Subscriptions to actions without an active mapping will not fire.
Store
cancelablereferences and cancel them during cleanup. Subscriptions persist until explicitly canceled, which can lead to unintended behavior if left active.Use
BeginDetectEventwhen you want an immediate response to a key press.Use
TriggerActivationEventwhen the action should only fire after all activation conditions are met.