IsNearPlayer - ObjectReference

From the CreationKit Wiki
Jump to navigation Jump to search

Member of: ObjectReference Script

A function that checks to see if this reference is either in the same cell or within 3000 units of the player. Commonly used in situations where the developer wants to be confident that an object can be enabled/disabled/moved/etc without the player being able to witness the action happening.

Syntax[edit | edit source]

bool Function IsNearPlayer()
	Actor player = Game.GetPlayer()
	Cell targetCell = self.GetParentCell()
	Cell playerCell = player.GetParentCell()
	if (targetCell != playerCell)
		if (targetCell && targetCell.IsInterior() || playerCell && playerCell.IsInterior())
			return false
		else
			if (player.GetDistance(self) > 3000)
				return false
			else
				return true
			endif
		endif
	else
		return true
	endif
endFunction

Parameters[edit | edit source]

None.

Return Value[edit | edit source]

Returns TRUE if the player is NOT in the same cell as the reference object and the player is also more than 3000 units away from the reference object. Alternatively returns TRUE if the Player is in an interior cell and the reference object is not (or vice-versa), even if the distance would otherwise be less than 3000 units.

If neither of the above conditions are met, returns FALSE.

Notes[edit | edit source]

This function is useful for getting a general idea of if an object, NPC, or other entity is potentially visible or otherwise likely to be loaded by the game. For more specific instances of visibility, it would be better to use something like IsDetectedBy - Actor or Is3DLoaded - ObjectReference.

Examples[edit | edit source]

; Disable the box, but only if the player isn't nearby
if (!Box.IsNearPlayer())
  Box.Disable()
endif

See Also[edit | edit source]