Difference between revisions of "Talk:FindClosestActorFromRef - Game"
Jump to navigation
Jump to search
imported>Rasikko (→SKSE alternative: Made a better function than the last one I made.) |
imported>Rasikko |
||
Line 18: | Line 18: | ||
; Bail if the cell contains more actors than an array can hold. | ; Bail if the cell contains more actors than an array can hold, or if the only actor is akCenter. | ||
if (CellRefs >= 129 || CellRefs == | if (CellRefs >= 129 || CellRefs == 1) | ||
return none | return none | ||
endif | endif | ||
Line 43: | Line 43: | ||
EndFunction | EndFunction | ||
</source> | </source> | ||
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- | 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-31T04:01:58 (EST) |
Revision as of 04:01, 31 December 2017
SKSE alternative
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.
Actor Function GetClosestActorFromRef(ObjectReference akCenter) Global
; returns the closest actor to akCenter. akCenter is excluded from the search.
; Arrays
ObjectReference[] kRefs = new ObjectReference[128]
Float[] fDistances = new Float[128]
; Forms
Cell kCell = akCenter.GetParentCell()
; Values
Int CellRefs = kCell.GetNumRefs(43) ; 43 = kNPC, 62 = kCharacter
Int i
Float fLowestValue = 1000000.0
; Bail if the cell contains more actors than an array can hold, or if the only actor is akCenter.
if (CellRefs >= 129 || CellRefs == 1)
return none
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
; get the index of the actor that had the smallest distance from akCenter.
Int refIndex = fDistances.Find(fLowestValue)
return kRefs[refIndex] as Actor
EndFunction
This will return the closest actor. How fast this function returns, depends on how many actors are in the cell. --Rasikko (talk) 2017-12-31T04:01:58 (EST)