Talk:FindClosestActorFromRef - Game

From the Fallout4 CreationKit Wiki
Jump to navigation Jump to search

Can recreate this function that will ignore arCenter in the search if it's an actor.

Actor Function FindTheClosestActorFromRef(ObjectReference center, Float radius)
    Keyword ActorType = Game.GetFormFromFile(0x0013794, "Fallout4.ESM") as Keyword
    ObjectReference[] ActorsFound = Game.GetPlayer().FindAllReferencesWithKeyword(ActorType, radius)
    Int maxActorsFound = ActorsFound.length
    Int lowestDist = 99999999
    Int closestActorIndex = 0
    Int i = 0
	
    if maxActorsFound > 0
		
	; is the source ref an actor?
	if (center as actor) == true
			
		; remove the source ref from the array
		Int sourceRefIndex = ActorsFound.Find(center)
		ActorsFound.Remove(sourceRefIndex)
			
		; reassign the new length of the array
		maxActorsFound = ActorsFound.length
			
	endif
		
	    while i < maxActorsFound
		if (ActorsFound[i].getDistance(center)) as int < lowestDist
			lowestDist = (ActorsFound[i].getDistance(center)) as int
			closestActorIndex = i
		endif
	        i += 1
	   endwhile
		
	       return ActorsFound[closestActorIndex] as Actor
    else
	return none
    endif
EndFunction

--Rasikko (talk) 2021-09-02T04:25:31 (EDT)