I am trying to expand from the verse shooting gallery tutorial and wanted to use an array for the targets but I’m now a little lost. So i would like to know how I actually get the target reference and knock them down from the array rather than having to do a lots of repeating code if i want to add new targets.
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
# A device that manages shooting range gameplay.
shooting_range_manager_device := class(creative_device):
@editable GoodTrackTargets : \[\]shooting_range_target_track_device = array{} @editable BadTargets : \[\]shooting_range_target_device = array{} @editable Timer: timer_device = timer_device{} @editable ItemGranter: item_granter_device = item_granter_device{} @editable ScoreManager:score_manager_device = score_manager_device{} @editable SignalRemoteManager:signal_remote_manager_device = signal_remote_manager_device{} @editable StationaryTarget: shooting_range_target_device = shooting_range_target_device{} \# @editable \# GoodTarget1:shooting_range_target_track_device = shooting_range_target_track_device{} \# @editable \# GoodTarget2:shooting_range_target_track_device = shooting_range_target_track_device{} \# @editable \# GoodTarget3:shooting_range_target_track_device = shooting_range_target_track_device{} \# @editable \# GoodTarget4:shooting_range_target_track_device = shooting_range_target_track_device{} @editable BadTarget1:shooting_range_target_device = shooting_range_target_device{} @editable BadTarget2:shooting_range_target_device = shooting_range_target_device{} @editable BadTarget3:shooting_range_target_device = shooting_range_target_device{} @editable BadTarget4:shooting_range_target_device = shooting_range_target_device{} @editable BadTarget5:shooting_range_target_device = shooting_range_target_device{} @editable var GoodTargetScore:int = 100 @editable var BadTargetScore:int = -100 @editable var BullseyeGoodTargetScore: int = 250 @editable var BullseyeBadTargetScore: int =-250 \# Runs when the device is started in a running game. \# 1. Update your Loop to use a single "Universal" function OnBegin<override>()<suspends>:void= for (Target : GoodTrackTargets): \# We subscribe EVERY target to the same function Target.HitEvent.Subscribe(OnAnyTrackTargetHit) \# 2. This function now handles EVERY target in your array OnAnyTrackTrackHit(Result : shooting_range_target_track_hit_result):void= if (ShotDevice := Result.TargetDevice): AdjustScore(GoodTargetScore) ShotDevice.PopDown() # Only the one you shot pops down! Print("An array target was hit and popped down.") Print("--- Initializing Target Array ---") \# This loops through the array and gives us the position (X) \# and the device (Target) for (X -> Target : GoodTrackTargets): Print("Target found at Index {X}") \# This will print the internal name of the device from UEFN Print("--- Total Targets: {GoodTrackTargets.Length} ---") if (TrackTargets := GoodTrackTargets\[0\]): TrackTargets.HitEvent.Subscribe(OnGoodTrackTargetsHit) \# for (Target :GoodTrackTargets): \# Target.HitEvent.Subscribe(OnGoodTarget1Hit) \# Target.BullseyeHitEvent.Subscribe(OnGoodTarget1HitBullseyeHitEvent) \# Subscribing to the StationaryTarget HitEvents. StationaryTarget.HitEvent.Subscribe(OnStationaryTargetHit) StationaryTarget.BullseyeHitEvent.Subscribe(OnStationaryBullseyeHit) #StationaryTarget.BullseyeHitEvent.Subscribe() \# Subscribing to the GoodTarget HitEvents. \# GoodTarget1.HitEvent.Subscribe(OnGoodTarget1Hit) \# GoodTarget1.BullseyeHitEvent.Subscribe(OnGoodTarget1HitBullseyeHitEvent) \# GoodTarget2.HitEvent.Subscribe(OnGoodTarget2Hit) \# GoodTarget2.BullseyeHitEvent.Subscribe(OnGoodTarget2HitBullseyeHitEvent) \# GoodTarget3.HitEvent.Subscribe(OnGoodTarget3Hit) \# GoodTarget3.BullseyeHitEvent.Subscribe(OnGoodTarget3HitBullseyeHitEvent) \# GoodTarget4.HitEvent.Subscribe(OnGoodTarget4Hit) \# GoodTarget4.BullseyeHitEvent.Subscribe(OnGoodTarget4HitBullseyeHitEvent) \# Subscribing to the BadTarget HitEvents. BadTarget1.HitEvent.Subscribe(OnBadTarget1Hit) BadTarget1.BullseyeHitEvent.Subscribe(OnBadTargetBullseye1Hit) BadTarget2.HitEvent.Subscribe(OnBadTarget2Hit) BadTarget2.BullseyeHitEvent.Subscribe(OnBadTargetBullseye2Hit) BadTarget3.HitEvent.Subscribe(OnBadTarget3Hit) BadTarget3.BullseyeHitEvent.Subscribe(OnBadTargetBullseye3Hit) BadTarget4.HitEvent.Subscribe(OnBadTarget4Hit) BadTarget4.BullseyeHitEvent.Subscribe(OnBadTargetBullseye4Hit) BadTarget5.HitEvent.Subscribe(OnBadTarget5Hit) BadTarget5.BullseyeHitEvent.Subscribe(OnBadTargetBullseye5Hit) \# A signal device trigger to stop movement of all targets \# On starting timer make all the targets stationary# 2. This function now handles EVERY target in your array
OnAnyTrackTrackHit(Result : shooting_range_target_track_hit_result):void= if (ShotDevice := Result.TargetDevice): AdjustScore(GoodTargetScore) ShotDevice.PopDown() # Only the one you shot pops down! Print("An array target was hit and popped down.") \# A hit call back on the stationary target OnStationaryTargetHit():void= AdjustScore(GoodTargetScore) #StationaryTarget OnStationaryBullseyeHit():void= AdjustScore(BullseyeGoodTargetScore) \# A hit callback that scores and pops down the first GoodTarget. OnGoodTarget1Hit():void= AdjustScore(GoodTargetScore) Print("Target") #GoodTarget1.PopDown() \# A Bullseye callback that scores and pops down the first GoodTarget. OnGoodTarget1HitBullseyeHitEvent():void= AdjustScore(BullseyeGoodTargetScore) Print("Bullseye") #GoodTarget1.PopDown() \# A hit callback that scores and pops down the second GoodTarget. OnGoodTarget2Hit():void= AdjustScore(GoodTargetScore) #GoodTarget2.PopDown() \# A Bullseye callback that scores and pops down the second GoodTarget. OnGoodTarget2HitBullseyeHitEvent():void= AdjustScore(BullseyeGoodTargetScore) #GoodTarget2.PopDown() \# A hit callback that scores and pops down the third GoodTarget. OnGoodTarget3Hit():void= AdjustScore(GoodTargetScore) #GoodTarget3.PopDown() \# A Bullseye callback that scores and pops down the third GoodTarget. OnGoodTarget3HitBullseyeHitEvent():void= AdjustScore(BullseyeGoodTargetScore) #GoodTarget3.PopDown() \# A hit callback that scores and pops down the fourth GoodTarget. OnGoodTarget4Hit():void= AdjustScore(GoodTargetScore) #GoodTarget4.PopDown() \# A Bullseye callback that scores and pops down the fourth GoodTarget. OnGoodTarget4HitBullseyeHitEvent():void= AdjustScore(BullseyeGoodTargetScore) #GoodTarget4.PopDown() \# A hit callback that scores and pops down the first BadTarget. OnBadTarget1Hit():void= AdjustScore(BadTargetScore) BadTarget1.PopDown() OnBadTargetBullseye1Hit():void= AdjustScore(BullseyeBadTargetScore) BadTarget1.PopDown() \# A hit callback that scores and pops down the second BadTarget. OnBadTarget2Hit():void= AdjustScore(BadTargetScore) BadTarget2.PopDown() OnBadTargetBullseye2Hit():void= AdjustScore(BullseyeBadTargetScore) BadTarget2.PopDown() \# A hit callback that scores and pops down the third BadTarget. OnBadTarget3Hit():void= AdjustScore(BadTargetScore) BadTarget3.PopDown() OnBadTargetBullseye3Hit():void= AdjustScore(BullseyeBadTargetScore) BadTarget3.PopDown() \# A hit callback that scores and pops down the third BadTarget. OnBadTarget4Hit():void= AdjustScore(BadTargetScore) BadTarget4.PopDown() OnBadTargetBullseye4Hit():void= AdjustScore(BullseyeBadTargetScore) BadTarget4.PopDown() \# A hit callback that scores and pops down the third BadTarget. OnBadTarget5Hit():void= AdjustScore(BadTargetScore) BadTarget5.PopDown() OnBadTargetBullseye5Hit():void= AdjustScore(BullseyeBadTargetScore) BadTarget5.PopDown() \# Adjusts the player's score by the provided value. AdjustScore(Value:int):void= ScoreManager.SetScoreAward(Value) \# Gets the first player in the playspace. if (Player:player = GetPlayspace().GetPlayers()\[0\]): \# Awards the points to the player. ScoreManager.Activate(Player) ItemGranter.GrantItem(Player)