Skip to content

Placing objects ​

client

One placement tool for every resource that lets a player put something into the world: vending machines, screens, cameras, dart boards, plants. np_prophandler owns the ghost, the camera raycast, the gizmo, the keys, the help text and the valid / invalid look. Your resource decides what is allowed and what happens on confirm.

lua
-- client
local handle = exports.np_prophandler:startPlacement({
  model = 'prop_vend_soda_01',
  surfaces = { 'ground' }, maxSlope = 15.0, maxDistance = 6.0,
  validate = function(pose) return not isTooCloseToAnother(pose.coords), 'too_close' end,
  onConfirm = function(pose) TriggerServerEvent('np_vending:place', pose.coords, pose.heading) end,
  onCancel = function(reason) end,  -- 'cancelled', 'replaced', or what cancelPlacement passed
})

For a preview you position yourself, use createLocal instead.

Modes ​

  • Aim (the default). The ghost follows the crosshair (a camera raycast) and sits on what it hits: its bottom on the ground, its back against a wall (facing out), its top under a ceiling. The wheel turns it, Page Up / Page Down lift it.
  • Gizmo. G switches to a mouse cursor and three arrows (red x, green y, blue z). Drag an arrow to move the object along that axis. R switches the arrows to rings that rotate it: only the heading ring, or x / y / z with rotationMode = 'full'. The arrow keys nudge the object relative to the camera. Hold the right mouse button to turn the camera.

The ghost gets a green or red outline. The built-in help text shows the keys and why a spot is refused. Input pauses while a NUI has focus without keeping game input, so you can open your own form on top. A HUD that keeps input (SetNuiFocusKeepInput) does not pause it.

Keys ​

Key Action Name in controls
E / Enter Confirm. Refused with an error sound while the spot is invalid confirm
Backspace / Esc Cancel cancel
G Switch between aim and gizmo toggleGizmo
R Gizmo: switch between arrows (move) and rings (rotate) toggleMode
Wheel Turn rotateLeft, rotateRight
Page Up / Page Down Lift / lower up, down
Arrow keys Gizmo: nudge relative to the camera forward, back, left, right
Left mouse Gizmo: drag a handle drag
Right mouse (hold) Gizmo: turn the camera look
Shift Fine steps everywhere fine

The defaults are in placement.controls. One call can remap or free any action with controls = { action = id | { ids } | false }. A key you free is yours to read, e.g. the wheel for a camera FOV.

Options ​

Option Default Notes
model none Ghost model. Without one only the gizmo is drawn, e.g. for a flat surface you outline yourself
coords, rotation aim point, player heading Start pose. A vector4 or heading is accepted, a rotation number is the heading. To move something that exists, pass its pose and mode = 'translate'
mode 'aim' 'aim', 'translate' or 'rotate'
aim true false: gizmo only, G does nothing
rotationMode 'heading' 'full': x / y / z rings (pitch, roll, heading)
align 'auto' 'auto': on a wall the object faces out (wall-mounted). 'stand': like auto, but aimed at a wall the object stands on the ground at its foot, back to the wall; the pose then counts as ground. 'normal': the model's up axis follows the surface normal. 'none': heading only
headingOffset 0 Degrees added to the wall-facing heading, for models whose front is not +y (180 for a front on -y)
rotationOffset none vector3 added to the aim-mode rotation, e.g. a camera's tilt. When your own keys change it, set it with updatePlacement instead of an adjust on every frame
surfaces any { 'ground', 'wall', 'ceiling' } or one string. Otherwise wrong_surface
maxSlope none Degrees, ground only. Otherwise too_steep
maxDistance placement.maxDistance (10) From the player, and the raycast reach. Otherwise too_far
area none { center, radius } the object must stay in. Otherwise outside_area
allowWater, allowVehicle false Otherwise water / in_vehicle
grid none Metres. Aim and drags snap x / y to it
rotationStep placement.rotationStep (7.5) Degrees per wheel notch. Ring drags snap to it
surfaceOffset 0 Pushes the object off the surface along its normal
flags 17 Raycast flags (1 world + 16 objects)
frame none { entity, offset? }: the pose is local to that entity. See Relative to an entity
bounds none With frame: metres every local axis may reach. Otherwise out_of_bounds
alpha, outline, box 170, on, off Ghost look. box draws the model bounds in the valid / invalid colour
handles true false hides the gizmo handles (cursor and keys stay), e.g. while your own key mode scales the object
help on false for resources with their own HUD. Drive it from onUpdate
hint none An extra line on top of the help text
labels config Replaces help texts (keys of placement.text) and names your own refusal reasons: { confirm = 'Save', reasons = { blocked = 'Recorder full' } }
blockControls none Extra control ids disabled while placing, e.g. { 30, 31 } when WASD nudges, so the ped stands still
controls config { action = id | { ids } | false }. Actions: confirm cancel toggleGizmo toggleMode rotateLeft rotateRight fine forward back left right up down drag look
keepOnConfirm false Keep placing after a confirm, e.g. several seeds in a row
data none Passed back in every pose
validate(pose) none -> ok, reason. On a change, at most every placement.validateIntervalMs (100)
adjust(pose) none -> { coords?, rotation?, valid?, reason? } on every pose change. Snap to your own grid, cells or heights
onUpdate(pose) none On a pose or validity change, at most every placement.updateIntervalMs (100)
onConfirm(pose) none
onCancel(reason, pose) none

Every callback is a cross-resource call. adjust runs on each change, so keep it cheap.

startPlacement returns a handle or false, reason: 'resource_disabled', 'model_missing', 'unknown_field', 'invalid_<option>' (e.g. 'invalid_surfaces'), 'invalid_model', 'invalid_coords'.

Pose ​

Every callback, checkPlacement, getPlacement and the event get the same pose:

Field Description
handle The placement handle
coords, rotation, heading Where the object would go. rotation is a vector3 in degrees
mode 'aim', 'translate' or 'rotate'
valid, reason The result of the validity checks
surface 'ground', 'wall' or 'ceiling'
normal, material, hitEntity What the raycast hit
model, entity The ghost model and entity
data Your data
worldCoords With frame only: coords in world space

Validity ​

A spot is checked in this order. The first refusal wins:

  1. The built-in checks: no_surface, wrong_surface, too_steep, too_far, outside_area, water, in_vehicle, out_of_bounds.
  2. adjust(pose), which can also return valid = false, reason = ….
  3. validate(pose).
  4. What you pushed with setPlacementValid(handle, false, 'reason'), e.g. after a server check. nil clears it.

updatePlacement(handle, {}) runs validate again without a move, for when state outside the pose changed. The help text names the reason from placement.text.reasons, or from your labels.reasons.

Changing a running placement ​

lua
exports.np_prophandler:updatePlacement(handle, { model = 'prop_vend_coffe_01', hint = 'Coffee' })  -- swap the ghost
exports.np_prophandler:checkPlacement(handle)     -- valid, reason, pose: before opening your own form
exports.np_prophandler:confirmPlacement(handle)   -- from your own key or form; refused while invalid
exports.np_prophandler:cancelPlacement(handle, 'closed')
exports.np_prophandler:stopPlacement(handle)      -- silently, no onCancel
  • A patch with coords alone keeps the rotation.
  • mode = 'aim' resets the aim heading to the current one, like G.
  • checkPlacement runs validate too. Nothing ends.
  • frame and aim are fixed for the life of a placement ('fixed_field').
  • The other exports return 'unknown_handle' when the handle is not the running placement, and 'not_owner' for another resource's placement. getPlacement() without a handle returns the running pose, isPlacing() returns placing, handle, owner.

Callbacks and event ​

  • onConfirm(pose) runs on E / Enter or confirmPlacement, only while the spot is valid. Without keepOnConfirm the placement ends there. With it, the next confirm waits at least placement.confirmCooldownMs (300).
  • onCancel(reason, pose) runs on Backspace / Esc ('cancelled'), on cancelPlacement with your reason, when a new startPlacement replaces it ('replaced'), when the frame entity is gone ('frame_missing') and when np_prophandler stops ('handler_stopped').
  • Besides the callbacks, the local event np_prophandler:placement fires with (handle, 'confirm' | 'cancel', pose, reason).

One placement runs at a time per client. It ends with the resource that started it.

The confirm is client input

onConfirm is a decision of the player's client. Check the pose on your server like any other client input: distance to the player, area, limits, what the player may place.

Recipes ​

Wall-mounted ​

A dart board hangs on a wall. Its face is the model's -y side, so the wall-facing heading is turned by 180°:

lua
local handle = exports.np_prophandler:startPlacement({
  model = 'prop_dart_bd_cab_01',
  surfaces = { 'wall' }, maxDistance = 6.0,
  headingOffset = 180.0,   -- the face is the -y side
  onConfirm = function(pose) TriggerServerEvent('my_darts:place', pose.coords, pose.heading) end,
})

Standing against a wall ​

Vending machines and cabinets stand on the floor. With align = 'stand', aiming at a wall puts the object on the ground at its foot, back to the wall, and the pose counts as ground:

lua
exports.np_prophandler:startPlacement({
  model = 'prop_vend_soda_01',
  align = 'stand', surfaces = { 'ground' }, maxSlope = 10.0, maxDistance = 8.0,
  onConfirm = function(pose) TriggerServerEvent('my_vending:place', pose.coords, pose.heading) end,
})

A camera with tilt ​

rotationMode = 'full' gives the gizmo x / y / z rings. rotationOffset tilts the camera in aim mode; change it with updatePlacement when your own keys turn it:

lua
local tilt = -15.0
local handle = exports.np_prophandler:startPlacement({
  model = 'prop_cctv_cam_01a',
  surfaces = { 'wall', 'ceiling' }, rotationMode = 'full',
  rotationOffset = vector3(tilt, 0.0, 0.0),
  controls = { up = false, down = false },  -- Page Up / Down are ours: tilt
  onConfirm = function(pose) TriggerServerEvent('my_cctv:install', pose.coords, pose.rotation) end,
})

-- in your own key loop
tilt = math.max(-60.0, math.min(30.0, tilt + 2.5))
exports.np_prophandler:updatePlacement(handle, { rotationOffset = vector3(tilt, 0.0, 0.0) })

Grid and cells ​

For a fixed grid, grid = 0.5 is enough. For your own cells (planting rows, field slots) snap in adjust, and keep placing with keepOnConfirm:

lua
exports.np_prophandler:startPlacement({
  model = 'prop_plant_01a', surfaces = { 'ground' }, keepOnConfirm = true,
  adjust = function(pose)
    local cell = field:cellAt(pose.coords)
    if not cell then return { valid = false, reason = 'no_cell' } end
    if cell.used then return { coords = cell.center, valid = false, reason = 'cell_used' } end
    return { coords = cell.center, rotation = vector3(0.0, 0.0, cell.heading) }
  end,
  labels = { reasons = { no_cell = 'Outside the field', cell_used = 'Already planted' } },
  onConfirm = function(pose) TriggerServerEvent('my_farm:plant', pose.coords) end,
})

Relative to an entity ​

With frame = { entity, offset? } the pose is local to that entity, e.g. an anchor point on a vehicle or an attachment offset. There is no aim mode, the arrow keys nudge along the entity's axes, and bounds (metres) clamps every axis (out_of_bounds). The placement is cancelled with frame_missing when the entity is gone. The pose has worldCoords as well.

lua
exports.np_prophandler:startPlacement({
  frame = { entity = vehicle, offset = vector3(0.0, -1.2, 0.6) },
  mode = 'translate', bounds = 3.0,
  onConfirm = function(pose) TriggerServerEvent('my_workshop:anchor', VehToNet(vehicle), pose.coords, pose.rotation) end,
})

Configuration ​

The defaults (distance, look, steps, speeds, gizmo size, keys, help texts) are in placement. Some of them can be changed live in np_admin's Placement category.