Talk:FindClosestActorFromRef - Game

From the CreationKit Wiki
Revision as of 14:47, 15 December 2017 by imported>Rasikko (Created page with "== SKSE alternative == You basically have to make your own findref function with SKSE, if you want to avoid certain references, but especially the player because it's so easy...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

SKSE alternative

You basically have to make your own findref function with SKSE, if you want to avoid certain references, but especially the player because it's so easy for it to be the return value. First of all you need to pass this keyword into the function:

Keyword property PlayerKeyword auto

And now for the function:

Actor Function FindClosestActorFromRefWithoutKeyword(ObjectReference akCenter, Keyword akKeyword)
	; returns all references of the specified type that doesn't have this keyword.
	
	ObjectReference[] kActorRefs = new ObjectReference[128]
	Cell kCell = akCenter.GetParentCell()
	Int i
	Int CellRefs = kCell.GetNumRefs(62) ; kCharacter
	Float[] fDistances = new Float[128]
	Float fLowestValue = 10000000.0
	
	if (CellRefs == 0 || CellRefs >= 129)
		return none
	endif
	
	While i < CellRefs
            if kCell.GetNthRef(i, 62).HasKeyword(akKeyword) == false
	        kActorRefs[i] = kCell.GetNthRef(i, 62)
	        fDistances[i] = kCell.GetNthRef(i, 62).GetDistance(akCenter)
	        Float fCurrentValue = fDistances[i]
	        if fCurrentValue <= flowestValue
		    fLowestValue = fCurrentValue
	        endif
	    endif
	    i += 1
	EndWhile
	
	Int refIndex = fDistances.Find(fLowestValue)
	
	return kActorRefs[refIndex] as Actor
EndFunction

This will return the closest actor. It's not efficient, and on the slow side but it's the best I could come up with. --Rasikko (talk) 2017-12-15T14:47:39 (EST)