Difference between revisions of "Talk:FindClosestActorFromRef - Game"

Jump to navigation Jump to search
m
→‎SKSE alternative: Cleaned up function to make more readible.
imported>Rasikko
imported>Rasikko
m (→‎SKSE alternative: Cleaned up function to make more readible.)
 
Line 3: Line 3:
<source lang="papyrus">
<source lang="papyrus">
Actor Function GetClosestActorFromRef(ObjectReference akCenter) Global
Actor Function GetClosestActorFromRef(ObjectReference akCenter) Global
; returns the closest actor to akCenter. akCenter is excluded from the search.
;/ 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
        /;
; Arrays
ObjectReference[] kRefs = new ObjectReference[128]
ObjectReference[] kRefs = new ObjectReference[128]
Float[] fDistances = new Float[128]
Float[] fDistances = new Float[128]
; Forms
Cell kCell = akCenter.GetParentCell()
Cell kCell = akCenter.GetParentCell()
Int CellRefs = kCell.GetNumRefs(43)  
; Values
Int CellRefs = kCell.GetNumRefs(43) ; 43 = kNPC, 62 = kCharacter
Int i
Float fLowestValue = 1000000.0
Float fLowestValue = 1000000.0
Int i
; Bail if the cell contains more actors than an array can hold, or if the only actor is akCenter.
if (CellRefs >= 129 || CellRefs == 1)
if (CellRefs >= 129 || CellRefs == 1)
return none
return none
Line 37: Line 35:
endWhile
endWhile
; get the index of the actor that had the smallest distance from akCenter.
Int refIndex = fDistances.Find(fLowestValue)
        Int refIndex = fDistances.Find(fLowestValue)
return kRefs[refIndex] as Actor
return kRefs[refIndex] as Actor
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-31T04:01:58 (EST)
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)
Anonymous user

Navigation menu