Skip to content

Instantly share code, notes, and snippets.

@LionGet
Last active November 21, 2023 15:07
Show Gist options
  • Save LionGet/f5f0f7bbfe434d1d619e1ea20df868ef to your computer and use it in GitHub Desktop.
Save LionGet/f5f0f7bbfe434d1d619e1ea20df868ef to your computer and use it in GitHub Desktop.
Extension of Teleporter Device - Teleport Agent to Location with Validation & Reattempts
#==========================================================
# Teleport Agent to Location with Validation & Reattempts #
#==========================================================
(Teleporter:teleporter_device).TeleportAgent<public>(Agent:agent,Translation:vector3,Rotation:rotation)<suspends>:void=
# MaxAttempts = How many times teleport will attempt to find a new location before failing
# **Warning: Each attempt increases function duration by 1 frame at minimum
# Radius = The radius of the area in which the teleport will reattempt to find a new location
MaxAttempts:int=3
Radius:float=50.0
#
var OriginalLocation:vector3=vector3{}
var ReturnLocation:transform=Teleporter.GetTransform()
if(Fort:=Agent.GetFortCharacter[]):
set OriginalLocation = Fort.GetTransform().Translation
if(Teleporter.TeleportTo[Translation,Rotation]):
Teleporter.Teleport(Agent)
Sleep(0.0)
if(Fort:=Agent.GetFortCharacter[]):
var NewLocation:vector3=Fort.GetTransform().Translation
if(not DistanceSquared(NewLocation,OriginalLocation)>0.0):
Print("Invalid Destination Teleport Failed, Attempting Random Adjustments")
var LoopCount:int=0
loop:
var L_Translation:vector3=Translation
set L_Translation.X += GetRandomFloat(-Radius,Radius)
set L_Translation.Y += GetRandomFloat(-Radius,Radius)
if(Teleporter.TeleportTo[L_Translation,Rotation]):
Teleporter.Teleport(Agent)
Sleep(0.0)
set NewLocation = Fort.GetTransform().Translation
if(not DistanceSquared(NewLocation,OriginalLocation)>0.0):
set LoopCount += 1
if(LoopCount > MaxAttempts):
Print("No Valid Destination Were Found, Aborting")
break
Print("Invalid Destination, Reattempt #{LoopCount}")
if(Teleporter.TeleportTo[ReturnLocation]):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment