Docs/API
Object placement
Place and remove game prefabs and assets in Lua, with examples for houses, fences, rotation and ground snapping.
Use area:place_objects(placements) to place 1–128 objects on a loaded public map. Each entry takes exactly one of prefab or asset.
- Use
prefabfor a named arrangement of one or more pieces.server.fetch_prefabs()lists names such ashouse_village_eandprefab_village_campsite. - Use
assetfor one exported object.server.fetch_assets()lists values you can pass directly, such asfence_woodorres://Prefabs/Village/Campsite.tscn. Short names are case-insensitive. If a short name matches several objects, use one of the paths from the error or the asset list. Theres://prefix is optional for paths.
Define reusable arrangements and placements that must exist before the map opens in objects.json.
Placement fields#
| Field | Description |
|---|---|
prefab or asset | A named prefab, or one exported object by short name or exact path. |
position | Required {x, y, z} or {x = ..., y = ..., z = ...}. Y is height. |
yaw | Rotation around the vertical axis in degrees. Defaults to 0. Rotates the whole prefab around its origin. |
snap | Defaults to true. A ray checks from 2 m above the supplied Y to 50 m below it and snaps the origin to the first static surface. Set false to use the exact Y. |
id | Optional stable ID, 1–48 characters from A–Z, a–z, 0–9, _ or -. The server generates one if omitted. The same ID and placement inputs return the existing object once it is ready. |
Place a house and a fence#
Paste this into init.lua. The same function handles newly loaded maps and maps that were already open during a scenario reload.
local function place_outskirts(area)
if area.name ~= "Village" or area.hideout then
return
end
local objects, err = area:place_objects({
{
id = "outskirts_house",
prefab = "house_village_e",
position = {130, 10, 68},
yaw = 90,
},
{
id = "outskirts_fence",
asset = "fence_wood",
position = {122, 10, 84},
},
})
if not objects then
print("Could not place outskirts: " .. tostring(err))
return
end
return objects
end
on("area_loaded", place_outskirts)
spawn(function()
for _, area in ipairs(server.areas()) do
place_outskirts(area)
end
end)
For a level fence run, give all segments the same Y and set snap = false. At yaw = 0, the wood fence extends 4 m along +X from its origin. For example, the following loop builds a list of five adjoining segments. Set fence_y to the height you want at the site.
local placements = {}
local fence_y = 0.3
for i = 0, 4 do
placements[#placements + 1] = {
id = "level_fence_" .. i,
asset = "fence_wood",
position = {122 + i * 4, fence_y, 84},
yaw = 0,
snap = false,
}
end
local fences, err = area:place_objects(placements)
if not fences then
print("Could not place fence: " .. tostring(err))
end
Snapping uses each origin separately, so uneven ground can make segments step up or down. A level run can leave gaps underneath on a slope.
Wait for placement and remove objects#
Call the batch methods from a task that can yield, such as an area hook, an event, or a function started with spawn. Placement returns handles after navigation is ready, so event AI can spawn after a successful return. It returns nil, err for invalid targets, exceeded limits, or blocked placements. If a batch fails, the server rolls back objects created by that batch.
Keep the returned handles for removal. For example, an event cleanup task can use:
local ok, err = area:remove_objects(objects)
if not ok then
print("Could not remove objects: " .. tostring(err))
end
Removal waits for the navigation update. A failure can leave some objects already removed. Once removal starts, cancelling the Lua task does not interrupt the remaining removals.
Object handles#
Object handles resolve by area and ID. The existing area:building(id) and area:buildings() lookups also return these handles.
| Field | Type | Description |
|---|---|---|
id | string | The building's id, unique within its area. |
area | area or nil | The area it belongs to, or nil while it is unloaded. |
exists | boolean | The building still exists. |
prefab | string | The prefab name or the resolved asset path used for placement. |
source | string | "scenario" (from objects.json), "script" or "admin" (/building place). |
position | position | Where it was placed (after snapping). |
yaw | number | Degrees. |
ready | boolean | Navigation is patched: AI paths account for it. Colliders and doors work immediately; this turns true a moment after placement. |
Every field except id and area is nil once the object no longer exists, and methods return nil, "object no longer exists".
object:remove()#
Returns: true, or nil, err
Removes one object and its colliders and doors. Use area:remove_objects() when you need to wait for navigation to update.
Persistence. Static placements in objects.json are applied on area load and resynced on /scenario reload. Script placements are cleared on reload, so use the area_loaded and spawn pattern above to recreate them. If the reload fails, the server restores the previous script placements.
Found a mistake or missing detail? Tell us in the Discord.
rtv-mp