GetLookTarget
What the player looks at - the point, the thing and its surface.
The first solid thing on the line from the camera through the middle of the screen (where the crosshair would be, in first
and in third person alike), at most range metres away (100 by default, 1000 at most); the player’s own body and the horse
they ride are looked through. nil when nothing solid is that close (the sky, open water).
The table: x, y, z the point hit (metres), distance from the camera, normal ({x, y, z}, which way the surface
faces there), surface the game’s surface type ("mat_wood", "mat_rock", "mat_grass", "mat_vegetation" …),
vegetation (true for a tree, a bush or leaves), and kind:
"player"- another player’s body:playeris their pid,idtheir body’s entity id,nametheir name"entity"- one of the server’s entities (an actor, an animal, a horse, a prop, a pickup, a cart …):idis the id the game mode knows it by,nameits name"object"- a thing of the level with a script (a door, a chest, a bed …):nameis its level name - for a door or a chest the key the game mode’s door and container functions take (a name the level gives several chests may need the point too: the mode’sGetNearestContainer) - andclassits class ("AnimDoor","Stash"…)"vegetation"- a tree or a bush of the level"static"- a building, a wall, a rock: the level’s solid scenery"terrain"- the ground
A tree, a bush or a piece of scenery also comes with mesh - the model it is drawn with, lower case
("objects/natural/vegetation/trees/normal_trees/quercus_robur/quercus_a.cgf": the folder is the species, which tells an oak
from a pine) - and its box: bbox ({min = {x, y, z}, max = {x, y, z}}) and center ({x, y, z}, the middle of that box).
The mesh and the centre are the same on every player’s game, so together they name one tree for the whole server, and they
are what the game mode’s RemoveLevelObject takes to take that tree away -
with "vegetation" when kind is "vegetation" and "brush" when it is "static" (the level places many of its small
stones as vegetation, so tell a stone from a tree by its mesh). They are nil when the game cannot tell which piece of the
level was hit (some walls and rocks). The look only meets what is solid: a young tree, a bush or a stone players walk
through is looked through, and the answer is what lies behind it.
It is this player’s own look, measured on their screen; send it with SendServerEvent when the mode should act on it.
Cheap enough to call when a key is pressed, not meant for every frame.
Syntax
Section titled “Syntax”GetLookTarget([range])| Parameter | Type | |
|---|---|---|
range |
number | how far to look, in metres (100 by default) (optional) |
Returns
Section titled “Returns”table | nil - {x, y, z, distance, normal, surface, vegetation, kind, player, id, name, class, mesh, bbox, center}; nil when nothing is hit
Example
Section titled “Example”-- a woodcutter: the key the mode declared, while looking at a tree within reach; the mode knows the tree by its mesh and centrefunction OnKeyBinding(name, pressed) if name == "chop" and pressed then local t = GetLookTarget(3) if t and t.kind == "vegetation" and t.mesh then SendServerEvent("chop", string.format("%s %.3f %.3f %.3f", t.mesh, t.center.x, t.center.y, t.center.z)) end endend
-- what is that door's key?local t = GetLookTarget(5)if t and t.class == "AnimDoor" then print("door: " .. t.name) endSee also
Section titled “See also”SendServerEvent · OnKeyBinding · WorldToScreen · the The player, the level and the frame group of the index
