Difference between revisions of "Talk:FindClosestActorFromRef - Game"
Jump to navigation
Jump to search
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 m (→SKSE alternative: Cleaned up function to make more readible.) |
||
(2 intermediate revisions by the same user not shown) | |||
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. | |||
*1. Check if there is more than 128 actors or only akCenter. | |||
*2. Iterate through all actors found and record their distances to the player. | |||
Actor Function | *3. Find the smallest distance from the player and return the actor. | ||
; | *4. Valid types for actors: 43 or 62 | ||
/; | |||
ObjectReference[] | |||
ObjectReference[] kRefs = new ObjectReference[128] | |||
Float[] fDistances = new Float[128] | |||
Cell kCell = akCenter.GetParentCell() | Cell kCell = akCenter.GetParentCell() | ||
Int CellRefs = kCell.GetNumRefs(43) | |||
Float fLowestValue = 1000000.0 | |||
Int i | Int i | ||
if (CellRefs >= 129 || CellRefs == 1) | |||
if (CellRefs = | |||
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 | |||
Int refIndex = fDistances.Find(fLowestValue) | Int refIndex = fDistances.Find(fLowestValue) | ||
return | return kRefs[refIndex] as Actor | ||
EndFunction | EndFunction | ||
</source> | </source> | ||
How fast this function returns, depends on how many actors are in the cell. --[[User:Rasikko|Rasikko]] ([[User talk:Rasikko|talk]]) 2018-01-02T02:58:34 (EST) |
Latest revision as of 02:58, 2 January 2018
SKSE alternative[edit source]
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.
*1. Check if there is more than 128 actors or only akCenter.
*2. Iterate through all actors found and record their distances to the player.
*3. Find the smallest distance from the player and return the actor.
*4. Valid types for actors: 43 or 62
/;
ObjectReference[] kRefs = new ObjectReference[128]
Float[] fDistances = new Float[128]
Cell kCell = akCenter.GetParentCell()
Int CellRefs = kCell.GetNumRefs(43)
Float fLowestValue = 1000000.0
Int i
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
Int refIndex = fDistances.Find(fLowestValue)
return kRefs[refIndex] as Actor
EndFunction
How fast this function returns, depends on how many actors are in the cell. --Rasikko (talk) 2018-01-02T02:58:34 (EST)