Difference between revisions of "Talk:FindClosestActorFromRef - Game"
Jump to navigation
Jump to search
Talk:FindClosestActorFromRef - Game (edit)
Revision as of 03:41, 31 December 2017
, 03:41, 31 December 2017→SKSE alternative: Made a better function than the last one I made.
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...") |
imported>Rasikko (→SKSE alternative: Made a better function than the last one I made.) |
||
Line 1: | Line 1: | ||
== SKSE alternative == | == SKSE alternative == | ||
You basically have to make your own | You basically have to make your own version of this 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. | ||
<source lang="papyrus"> | <source lang="papyrus"> | ||
Actor Function GetClosestActorFromRef(ObjectReference akCenter) Global | |||
; returns the closest actor to akCenter. akCenter is excluded from the search. | |||
; Arrays | |||
Actor Function | ObjectReference[] kRefs = new ObjectReference[128] | ||
; returns | Float[] fDistances = new Float[128] | ||
ObjectReference[] | ; Forms | ||
Cell kCell = akCenter.GetParentCell() | Cell kCell = akCenter.GetParentCell() | ||
Int CellRefs = kCell.GetNumRefs( | ; Values | ||
Int CellRefs = kCell.GetNumRefs(43) ; 43 = kNPC, 62 = kCharacter | |||
Float fLowestValue = | Int i | ||
Float fLowestValue = 1000000.0 | |||
if (CellRefs = | |||
; Bail if the cell contains more actors than an array can hold. | |||
if (CellRefs >= 129 || CellRefs == 0) | |||
return none | return none | ||
endif | endif | ||
while i < CellRefs | |||
ObjectReference kActors = kCell.GetNthRef(i, 43) | |||
if kActors != akCenter | |||
kRefs[i] = kActors | |||
fDistances[i] = kActors.GetDistance(akCenter) | |||
Float fCurrentValue = fDistances[i] | |||
if fCurrentValue <= fLowestValue | |||
fLowestValue = fCurrentValue | |||
endif | |||
endif | |||
i += 1 | |||
endWhile | |||
return | ; get the index of the actor that had the smallest distance from akCenter. | ||
Int refIndex = fDistances.Find(fLowestValue) | |||
return kRefs[refIndex] as Actor | |||
EndFunction | EndFunction | ||
</source> | </source> | ||
This will return the closest actor. | This will return the closest actor. How fast this function returns, depends on how many actors are in the cell. --[[User:Rasikko|Rasikko]] ([[User talk:Rasikko|talk]]) 2017-12-15T14:47:39 (EST) |