Background for Skyrim SE
Member of the Game script
ObjectReferencefunctionFindClosestReferenceOfTypeFromRef(FormarBaseObject, ObjectReferencearCenter, floatafRadius)Global

Description

Wiki Description

Finds the closest ObjectReference of the given type from the location of the target ObjectReference and within the given radius.

Documentation Comment

Finds the closest reference of a given base object within a given radius of a reference


Caveats

CK Wiki - Notes

  • If the ObjectReference arCenter is of the same type as arBaseObject, this function will return arCenter. That is to say that this function does not exclude arCenter from it's search.

Parameters

FormarBaseObject

CK Wiki Description

The type of base object to look for

ObjectReferencearCenter

CK Wiki Description

ObjectReference to use as a center to search from

floatafRadius

CK Wiki Description

Maximum distance from center to look for a ObjectReference


Examples

; Find the closest diamond reference to the player, limiting the search to 5 units
ObjectReference closestDiamond = Game.FindClosestReferenceOfTypeFromRef(Diamond, Game.GetPlayer(), 5.0)
scriptName OpenShackDoorScript extends ActiveMagicEffect
{Script for a Self Spell that opens the nearest Whiterun shack door}

import game
Door property WRShackDoor01 auto ; Switching to Door because Form properties are not auto-filled?
;======================================================================================;
;  VARIABLES   /
;=============/
objectReference CastFromHereRef
ObjectReference closestDoor
float RadiusToFindDoor = 1000.0
;======================================================================================;
;   EVENTS     /
;=============/

Event OnEffectStart(Actor Target, Actor Caster)
    CastFromHereRef = Caster
    closestDoor  = Game.FindClosestReferenceOfTypeFromRef(WRShackDoor01, CastFromHereRef, RadiusToFindDoor )

    if ( closestDoor == None) 
        Debug.Notification("There is no shack door near!")
    else
        if( closestDoor.isLocked() )
            Debug.Notification("Opening door!")
            closestDoor.Lock(false)
        else
            Debug.Notification("The door is not locked!")
        endif    
    endif
EndEvent

Auto-Generated Example

Form myForm__arBaseObject
ObjectReference myObjectReference__arCenter
float myFloat__afRadius

ObjectReference returnedValue = Game.FindClosestReferenceOfTypeFromRef(myForm__arBaseObject, myObjectReference__arCenter, myFloat__afRadius)

Related Pages


Additional References

View this function’s page on the Skyrim Creation Kit Wiki

Some data provided by the Skyrim Creation Kit Wiki. Licensed under the Creative Commons Attribution-ShareAlike license.