Turret Issue

Subject: Help Needed with Implementing Automated Turrets in a 5v5 Game Mode

Hello Epic Games Community,

I’m working on a project that involves creating an automated turret system for a 5v5 game mode using Verse scripting in Fortnite Creative. Here’s an overview of what I aim to achieve:
Game Mechanics

Team-Based Turrets:
    Each team (Team 1 and Team 2) will have automated turrets.
    Turrets will prioritize attacking enemy players who attack their team members.
    If no players are attacking team members, turrets will target enemy guards.

Guard Spawners:
    Each team will have guard spawners that generate AI guards to patrol and attack the enemy team.
    Turrets must be able to switch targets from guards to players if a player attacks a team member within the turret's range.

Event Handling:
    Turrets will use the prop_manipulator_device to detect when props or structures are damaged and respond by targeting the attacking agent.
    Turrets need to dynamically change targets based on the current threat level (players attacking team members take precedence over guards).

Devices Used

automated_turret_device:
    Used to create customizable turrets that search for and attack nearby targets.

team_settings_and_inventory_device:
    Provides team configurations and handles events such as team member damage and enemy eliminations.

prop_manipulator_device:
    Detects damage to props and triggers events to make turrets respond to attacks on structures.

guard_spawner_device:
    Spawns AI guards that patrol and attack enemies.

Example Code

I’ve written some example code for the turrets, which includes event subscriptions for detecting team member damage and prop damage, and logic for prioritizing targets. Below is the code for both Team 1 and Team 2 turrets:

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Fortnite.com/AI }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Playspaces }
using { /Fortnite.com/Game }
using { /Fortnite.com/Teams }
using { /Fortnite.com/GameActions }

# Class for team 2 turret
team2_turret := class(creative_device):

    @editable
    Turret: automated_turret_device := automated_turret_device{}

    @editable
    Team1Settings: team_settings_and_inventory_device := team_settings_and_inventory_device{}

    @editable
    Team2Settings: team_settings_and_inventory_device := team_settings_and_inventory_device{}

    @editable
    Team2PropManipulator: prop_manipulator_device := prop_manipulator_device{}

    var CurrentTarget: ?agent = false

    OnBegin<override>()<suspends>:void =
        # Subscribe to events
        Team2Settings.TeamMemberDamagedEvent.Subscribe(OnTeamMemberDamaged)
        Team2PropManipulator.DamagedEvent.Subscribe(OnPropDamaged)
        Turret.TargetFoundEvent.Subscribe(OnTargetFound)
        Turret.TargetLostEvent.Subscribe(OnTargetLost)
        Turret.Enable()

    OnTeamMemberDamaged(DamageResult: damage_result): void =
        # If a team 2 member is attacked, check if the attacking agent is a team 1 player
        if (Team1Settings.IsOnTeam(DamageResult.Instigator)):
            ChangeTarget(DamageResult.Instigator)

    OnPropDamaged(DamageAgent: agent): void =
        # If a team 2 prop is damaged, check if the attacking agent is a team 1 player
        if (Team1Settings.IsOnTeam(DamageAgent)):
            ChangeTarget(DamageAgent)

    OnTargetFound(Target: agent): void =
        # When the turret finds a new target
        if (CurrentTarget == false):
            ChangeTarget(Target)

    OnTargetLost(LostTarget: agent): void =
        # When the turret loses a target
        if (CurrentTarget == LostTarget):
            set CurrentTarget = false

    ChangeTarget(Target: agent): void =
        # Change the turret's target
        set CurrentTarget = option{Target}
        Turret.SetTarget(Target)
        Print("Turret is now targeting: " + ToString([Target]))

# Class for team 1 turret
team1_turret := class(creative_device):

    @editable
    Turret: automated_turret_device := automated_turret_device{}

    @editable
    Team1Settings: team_settings_and_inventory_device := team_settings_and_inventory_device{}

    @editable
    Team2Settings: team_settings_and_inventory_device := team_settings_and_inventory_device{}

    @editable
    Team1PropManipulator: prop_manipulator_device := prop_manipulator_device{}

    var CurrentTarget: ?agent = false

    OnBegin<override>()<suspends>:void =
        # Subscribe to events
        Team1Settings.TeamMemberDamagedEvent.Subscribe(OnTeamMemberDamaged)
        Team1PropManipulator.DamagedEvent.Subscribe(OnPropDamaged)
        Turret.TargetFoundEvent.Subscribe(OnTargetFound)
        Turret.TargetLostEvent.Subscribe(OnTargetLost)
        Turret.Enable()

    OnTeamMemberDamaged(DamageResult: damage_result): void =
        # If a team 1 member is attacked, check if the attacking agent is a team 2 player
        if (Team2Settings.IsOnTeam(DamageResult.Instigator)):
            ChangeTarget(DamageResult.Instigator)

    OnPropDamaged(DamageAgent: agent): void =
        # If a team 1 prop is damaged, check if the attacking agent is a team 2 player
        if (Team2Settings.IsOnTeam(DamageAgent)):
            ChangeTarget(DamageAgent)

    OnTargetFound(Target: agent): void =
        # When the turret finds a new target
        if (CurrentTarget == false):
            ChangeTarget(Target)

    OnTargetLost(LostTarget: agent): void =
        # When the turret loses a target
        if (CurrentTarget == LostTarget):
            set CurrentTarget = false

    ChangeTarget(Target: agent): void =
        # Change the turret's target
        set CurrentTarget = option{Target}
        Turret.SetTarget(Target)
        Print("Turret is now targeting: " + ToString([Target]))

Request

I’m seeking assistance with the following:

Validating the logic and implementation of the turret targeting system.
Ensuring that the turrets properly prioritize targets based on the described mechanics.
Any optimization tips or best practices for handling these events and actions in Verse.

Thank you in advance for your help and suggestions!

Best regards,
type or paste code here

type or paste code here

What is the issue? It seems like you have everything you need already typed up; you just have to work out the logic in more detail and code it up.

For instance, ‘Turrets will use the prop_manipulator_device to detect when props or structures are damaged and respond by targeting the attacking agent.
Turrets need to dynamically change targets based on the current threat level (players attacking team members take precedence over guards).’

If this is the logic you want to use, you can use the damageable interface of the prop to subscribe your own custom OnDamaged(Agent : agent) funciton to its DamagedEvent. Then in your OnDamaged function, you can check if each new attacking player is a player or a guard, switching targets depending on that logic, and you can even add them to an array of agents to keep track of the targets each turret should cycle through after its current target is eliminated.

Also, just curious, is there a use for the == in verse, or is that perhaps a bit of c++ coming through here? :slight_smile: