faktehwin Posted March 7, 2021 Share Posted March 7, 2021 Trying to make a spell mod that teleport the player to the nearest marker in a formlist. How would I cycle through a list of objectreference(marker) in a formlist and detect which is the nearest to the player? Link to comment Share on other sites More sharing options...
Evangela Posted March 7, 2021 Share Posted March 7, 2021 (edited) Formlist property markerList auto Function MoveToNearestMarker(Formlist akList, Actor akActor, Float afDistance) int index = akList.GetSize() While index index -= 1 if (akList.GetAt(index) as ObjectReference).GetDistance(akActor) <= afDistance akActor.MoveTo(akList.GetAt(index) as ObjectReference) endif Endwhile EndFunction MoveToNearestMarker(MarkerList, Game.GetPlayer(), 500.0) How this works:Each time the loop iterates, it goes up the list and checks if the objectreference at that position in the list is 500.0 units or less to the player, and itereates to the next form if not. This continues until the last form in the list( actually the first; index 0) is reached. If 2 or more markers are exactly the same distance, it doesn't matter, the loop will move you to the first one found. Edited March 7, 2021 by Rasikko Link to comment Share on other sites More sharing options...
faktehwin Posted March 7, 2021 Author Share Posted March 7, 2021 Thanks! This really help me alot with understanding how the loop function works. Although its not exactly what I was looking for, when I used the script you posted I got ported to each marker listed in the form so long as it was under the 500 unit. However with abit of tinkering I managed to make it work how I wanted. This is the function I used: Function MoveToNearestMarker(Formlist akList, Actor akActor, Float afDistance) ;afDistance was set to 1000000 to roughly cover the world map, not sure whats the limit. ObjectReference ClosestMarker int index = akList.GetSize() While index index -= 1 if (akList.GetAt(index) as ObjectReference).GetDistance(akActor) <= afDistance afDistance = (akList.GetAt(index) as ObjectReference).GetDistance(akActor) ClosestMarker = (akList.GetAt(index) as ObjectReference) endif Endwhile akActor.MoveTo(ClosestMarker) EndFunction Link to comment Share on other sites More sharing options...
Evangela Posted March 7, 2021 Share Posted March 7, 2021 Oops, sorry about that! Link to comment Share on other sites More sharing options...
Recommended Posts