Okay, here is a Verse script designed to control an NPC, making it more dynamic and hopefully fun to fight. This script includes features like searching for players, attacking, dodging, and a simple "special move" (a quick dash).
Key Features:
- Target Acquisition: Scans for the nearest player within a defined range.
- Combat Engagement: Moves towards the target and attacks when in range.
- Line of Sight: Checks if it can actually see the target before engaging/attacking.
- Dodging: Has a chance to perform a quick sideways dash when taking damage.
- Special Move (Dash): Periodically performs a short dash (forward or backward) during combat for repositioning.
- Patrolling (Simple): If no target is found, it can optionally move between predefined points (requires Patrol Point Devices).
- Customizable: Many parameters (ranges, speed, cooldowns, chances) are exposed as
@editable properties in UEFN.
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Fortnite.com/AI }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /Verse.org/Random } # Required for random numbers (dodge chance, patrol points)
# Define log channel for easy debugging
log_fun_npc := module_log{Channel := "LogFunNPC"}
fun_npc_controller := class(creative_device):
# -- EDITABLE PROPERTIES --
# Configure these values in the UEFN editor after placing the device.
@editable # The spawner device that creates our NPC
NPCSpawner : npc_spawner_device = npc_spawner_device{}
@editable # The weapon the NPC will use (Item Spawner containing the weapon)
WeaponSpawner : item_spawner_device = item_spawner_device{}
@editable # How far the NPC can see/detect players
DetectionRadius : float = 3000.0
@editable # The ideal distance the NPC tries to maintain while fighting
AttackRange : float = 1500.0
@editable # How close the NPC needs to be to start shooting
FireRange : float = 1600.0 # Slightly larger than AttackRange to allow firing while moving into ideal range
@editable # Minimum distance the NPC wants to keep from the target (prevents face-hugging)
MinDistance : float = 500.0
@editable # Time in seconds between searching for new targets when idle
TargetScanInterval : float = 1.0
@editable # Chance (0.0 to 1.0) to perform a dodge maneuver when hit
DodgeChanceOnHit : float = 0.6
@editable # How far the NPC dodges sideways
DodgeDistance : float = 400.0
@editable # Duration of the dodge maneuver in seconds
DodgeDuration : float = 0.3
@editable # Cooldown in seconds after dodging before another dodge can occur
DodgeCooldown : float = 2.0
@editable # Cooldown in seconds for the special dash move
SpecialDashCooldown : float = 8.0
@editable # Distance for the special forward/backward dash
SpecialDashDistance : float = 600.0
@editable # Duration of the special dash move
SpecialDashDuration : float = 0.4
@editable # Set to true if you want the NPC to patrol between points when idle
EnablePatrol : logic = true
@editable # Array of Patrol Point devices for idle patrolling
PatrolPoints : []patrol_point_device = array{}
# -- INTERNAL STATE VARIABLES --
# Stores the actual agent (character) once spawned
var MaybeNPCCharacter : ?agent = false
# Stores the navigation interface for the agent
var MaybeNavigator : ?navigatable = false
# Stores the current target agent (player)
var MaybeCurrentTarget : ?agent = false
# Flag to indicate if the NPC is currently dodging
var IsDodging : logic = false
# Flag to indicate if the dodge ability is on cooldown
var DodgeOnCooldown : logic = false
# Flag to indicate if the special dash ability is on cooldown
var SpecialDashOnCooldown : logic = false
# Stores the index of the current patrol point target
var CurrentPatrolIndex : int = 0
# -- INITIALIZATION --
# OnBegin is called when the device starts up in the game
OnBegin<override>()<suspends>:void=
log_fun_npc.Print("Fun NPC Controller Initializing...")
# Subscribe to the spawner's event to know when an NPC is actually spawned
NPCSpawner.SpawnedEvent.Subscribe(OnNPCSpawned)
# Subscribe to know when the NPC is eliminated
NPCSpawner.EliminatedEvent.Subscribe(OnNPCEliminated)
# Attempt to spawn the NPC immediately if the spawner is enabled
NPCSpawner.Spawn()
# -- NPC LIFECYCLE EVENTS --
# Called when the NPC Spawner successfully spawns an agent
OnNPCSpawned(SpawnedAgent : agent):void=
log_fun_npc.Print("NPC Spawned: {SpawnedAgent}")
# Store the spawned agent
set MaybeNPCCharacter = option{SpawnedAgent}
# Attempt to grant the specified weapon to the NPC
GrantWeapon(SpawnedAgent)
# Get the navigation interface for the agent
if (Navigator := SpawnedAgent.GetNavigatable[]):
set MaybeNavigator = option{Navigator}
log_fun_npc.Print("Obtained Navigatable interface.")
else:
log_fun_npc.Print("Failed to get Navigatable interface for NPC.", log_level.Warning)
# Subscribe to the agent's damaged event to trigger dodges
SpawnedAgent.GetFortCharacter[].DamagedEvent().Subscribe(OnNPCDamaged)
# Start the main behavior loop for this NPC in a separate coroutine
spawn { MainBehaviorLoop() }
# Called when the controlled NPC is eliminated
OnNPCEliminated(Agent : agent):void=
log_fun_npc.Print("NPC Eliminated: {Agent}")
# Clear the stored agent and target information
set MaybeNPCCharacter = false
set MaybeNavigator = false
set MaybeCurrentTarget = false
# Reset state flags
set IsDodging = false
set DodgeOnCooldown = false
set SpecialDashOnCooldown = false
# Optionally, you could trigger a respawn here after a delay if the spawner doesn't handle it
# spawn { RespawnAfterDelay(5.0) }
# -- WEAPON HANDLING --
# Grants the weapon from the WeaponSpawner to the specified agent
GrantWeapon(Agent</suspends></override>