Skip to content

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: player is their pid, id their body’s entity id, name their name
  • "entity" - one of the server’s entities (an actor, an animal, a horse, a prop, a pickup, a cart …): id is the id the game mode knows it by, name its name
  • "object" - a thing of the level with a script (a door, a chest, a bed …): name is 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’s GetNearestContainer) - and class its 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.

GetLookTarget([range])
Parameter Type
range number how far to look, in metres (100 by default) (optional)

table | nil - {x, y, z, distance, normal, surface, vegetation, kind, player, id, name, class, mesh, bbox, center}; nil when nothing is hit

-- a woodcutter: the key the mode declared, while looking at a tree within reach; the mode knows the tree by its mesh and centre
function 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
end
end
-- what is that door's key?
local t = GetLookTarget(5)
if t and t.class == "AnimDoor" then print("door: " .. t.name) end

SendServerEvent · OnKeyBinding · WorldToScreen · the The player, the level and the frame group of the index