Tutorial: working with GetFocusInterface
Ai-Symbol Generated

Tutorial: working with GetFocusInterface

Updated Generated

Mastering GetFocusInterface in Verse

In Fortnite UEFN, making your NPCs feel alive often requires more than just movement; they need to react to threats, track players, and maintain attention. The GetFocusInterface method is your gateway to this behavioral control. By calling this method on a FortCharacter, you gain access to the focus_interface, which allows you to dictate what the character is "focusing" on—whether that’s a specific enemy, a location, or a direction. This tutorial will guide you through retrieving this interface and using it to make an NPC flee from damage while keeping its eyes on the threat.

Step 1: Accessing the Character and Focus Interface

Before you can control an NPC's focus, you must first have a reference to the character itself. In Verse, this is typically done within an event handler like OnDamaged or during initialization. You start by getting the FortCharacter from your agent, and then immediately call .GetFocusInterface[] on it. This returns the focus_interface instance you will use for all subsequent focus-related actions.

# Inside an event like OnDamaged(Result : damage_result)
Agent := GetAgent[]
Character := Agent.GetFortCharacter[]

# Retrieve the focus interface for this specific character
FocusInterface := Character.GetFocusInterface[]

Step 2: Identifying the Threat

To make the NPC react intelligently, it needs to know what it is focusing on. In a flee scenario, the threat is usually the instigator of the damage. You can extract the instigator from the damage_result and then get their FortCharacter reference. This allows you to pass the attacker as the target for the focus interface.

# Get the instigator from the damage result
AttackerAgent := Agent[Result.Instigator?]
AttackerChar := AttackerAgent.GetFortCharacter[]

# Ensure the attacker exists before proceeding
if:
    AttackerChar != null
then:
    # We have a valid threat to focus on
    ThreatTarget := AttackerChar

Step 3: Calculating the Flee Direction

Now that we have the focus interface and the threat, we need to calculate where the NPC should run. We get the current position of our NPC and the position of the attacker. By subtracting the attacker's position from our NPC's position, we get a vector pointing away from the threat. Normalizing this vector and scaling it gives us a safe distance to flee to.

MyPos := Character.GetTransform().Translation
ThreatPos := AttackerChar.GetTransform().Translation

# Vector pointing from threat TO us
AwayVector := MyPos - ThreatPos

# Normalize and scale by a desired flee distance (e.g., 500 units)
FleeDistance := 500.0
FleePoint := MyPos + (AwayVector / AwayVector.Length) * FleeDistance

Step 4: Applying Focus and Moving

The core of this tutorial is using FocusInterface.MaintainFocus(). This method tells the character to keep its attention on a specific target. Crucially, MaintainFocus does not complete on its own; it runs continuously until interrupted or canceled. Therefore, it must be spawned as a separate fiber (using spawn{}) so it doesn't block the rest of your logic. Simultaneously, you can trigger the movement logic to run the NPC to the calculated FleePoint.

Recap

  • GetFocusInterface[] is a method called on a FortCharacter to retrieve the focus_interface instance.
  • The focus_interface allows you to control what an NPC is visually and behaviorally focused on.
  • MaintainFocus(Target) keeps the character looking at or tracking a specific target.
  • MaintainFocus is asynchronous and must be run in a spawn{} block to avoid blocking the main execution flow.
  • Combining focus with movement logic creates dynamic behaviors, such as fleeing while keeping eyes on the pursuer.

Get the complete code — free

You've read the full walkthrough. The complete, copy-paste-ready Verse solution is free for members — sign in to unlock it.

Free with your BrainDead.TV / BrainDeadGuild Discord account. The walkthrough above is always free.

Turn this into a guided course

Add GetFocusInterface to your free study plan — we'll suggest related pages and stitch the lot into one compile-checked, self-guided lesson with worked examples and quizzes.

Original ai-symbol generated by Verse Island from the Verse/UEFN knowledge base, with references to the Epic Games sources above. Code is validated against the knowledge base.

Comments

    Sign in to vote, comment, or suggest an edit. Sign in