Difference between revisions of "Talk:FindClosestActorFromRef - Game"

→‎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 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:
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">
Keyword property PlayerKeyword auto
Actor Function GetClosestActorFromRef(ObjectReference akCenter) Global
</source>
; returns the closest actor to akCenter. akCenter is excluded from the search.
And now for the function:
<source lang="papyrus">
; Arrays
Actor Function FindClosestActorFromRefWithoutKeyword(ObjectReference akCenter, Keyword akKeyword)
ObjectReference[] kRefs = new ObjectReference[128]
; returns all references of the specified type that doesn't have this keyword.
Float[] fDistances = new Float[128]
ObjectReference[] kActorRefs = new ObjectReference[128]
; Forms
Cell kCell = akCenter.GetParentCell()
Cell kCell = akCenter.GetParentCell()
Int i
Int CellRefs = kCell.GetNumRefs(62) ; kCharacter
; Values
Float[] fDistances = new Float[128]
Int CellRefs = kCell.GetNumRefs(43) ; 43 = kNPC, 62 = kCharacter
Float fLowestValue = 10000000.0
Int i
Float fLowestValue = 1000000.0
if (CellRefs == 0 || CellRefs >= 129)
; 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
while i < CellRefs
            if kCell.GetNthRef(i, 62).HasKeyword(akKeyword) == false
ObjectReference kActors = kCell.GetNthRef(i, 43)
        kActorRefs[i] = kCell.GetNthRef(i, 62)
if kActors != akCenter
        fDistances[i] = kCell.GetNthRef(i, 62).GetDistance(akCenter)
kRefs[i] = kActors
        Float fCurrentValue = fDistances[i]
fDistances[i] = kActors.GetDistance(akCenter)
        if fCurrentValue <= flowestValue
    fLowestValue = fCurrentValue
Float fCurrentValue = fDistances[i]
        endif
        if fCurrentValue <= fLowestValue
    endif
fLowestValue = fCurrentValue
    i += 1
endif
EndWhile
endif
i += 1
Int refIndex = fDistances.Find(fLowestValue)
endWhile
return kActorRefs[refIndex] as Actor
; 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. It's not efficient, and on the slow side but it's the best I could come up with. --[[User:Rasikko|Rasikko]] ([[User talk:Rasikko|talk]]) 2017-12-15T14:47:39 (EST)
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)
Anonymous user