Skip to content

Commit cfd12e7

Browse files
committed
createBuilding function
1 parent 6dec017 commit cfd12e7

File tree

4 files changed

+141
-4
lines changed

4 files changed

+141
-4
lines changed
Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,73 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/createBuilding
2-
client:
1+
shared: &shared
32
name: createBuilding
4-
description: TODO
5-
incomplete: true
3+
description: Creates a [[building]] element at a given position & interior with a certain rotation.
4+
notes:
5+
- type: info
6+
content: Unlike [[createObject]], this function does not have an **isLowLOD** argument. When using [[setLowLODElement]], the system will automatically define the building as a LOD.
7+
- type: info
8+
content: |
9+
- Some objects/buildings in interior 13 show in all interiors.
10+
- Buildings can only be created inside regular GTA:SA Map Boundaries (X between -3000 and 3000; Y between -3000 and 3000). Use [[createObject]] to spawn objects outside these normal limits. **This limitation is probably going to stop existing in the near future.**
11+
- Buildings cannot appear in certain a [[Dimension]], and not show in others. Function [[setElementDimension]] returns false on any building. A building is created in a specific [[Interior]] world (such as 0, the main world), like the default GTA:SA landscape objects. All buildings appear in **EVERY DIMENSION**.
12+
- There is a distinction in GTA: San Andreas between static and dynamic models (these use a separate streaming system). Examples of buildings include building models, roads, and terrain. Objects created as [[building]]s **can contain glass and shadows**, unlike those created as [[object]]s (which are missing these features).
13+
- Buildings can be created with dynamic object model IDs, but they won't have any physical interaction. For example, [object ID 1502 (Gen_doorINT04)](https://dev.prineside.com/en/gtasa_samp_model_id/model/1502-Gen_doorINT04/) is a door that can only be opened if created with [[createObject]].
14+
- type: tip
15+
content: |
16+
- Using buildings for mapping is more optimized than using objects. Gains in FPS can be noticed in areas where a lot of objects were replaced with buildings of this new system.
17+
- Created buildings can have <b>LOD models</b>. The procedure is as follows: spawn the LOD building using <b>createBuilding</b>, then use [[setLowLODElement]] to associate it with the non-LOD building element you created beforehand. LOD model distance changed with [[engineSetModelLODDistance]] works for buildings.
18+
oop:
19+
element: building
20+
constructorclass: Building
21+
parameters:
22+
- name: modelId
23+
type: int
24+
description: A whole integer specifying the GTA:SA object model ID. See [Object IDs](/reference/ID_Lists/Objects) for a list of model IDs.
25+
- name: x
26+
type: float
27+
description: A floating point number representing the X coordinate on the map.
28+
- name: 'y'
29+
type: float
30+
description: A floating point number representing the Y coordinate on the map.
31+
- name: z
32+
type: float
33+
description: A floating point number representing the Z coordinate on the map.
34+
- name: rx
35+
type: float
36+
description: A floating point number representing the rotation about the X axis in degrees.
37+
default: '0'
38+
- name: ry
39+
type: float
40+
description: A floating point number representing the rotation about the Y axis in degrees.
41+
default: '0'
42+
- name: rz
43+
type: float
44+
description: A floating point number representing the rotation about the Z axis in degrees.
45+
default: '0'
46+
- name: interior
47+
type: int
48+
description: The interior you want to set the building to. Valid values are 0 to 255.
49+
default: '0'
50+
returns:
51+
values:
52+
- type: building
53+
name: building element
54+
description: Returns the [[building]] element if the creation was successful, throws an error otherwise.
55+
examples:
56+
- path: examples/createBuilding-2.lua
57+
description: This example creates a building when the resource starts.
58+
- path: examples/createBuilding_OOP-2.lua
59+
description: This example creates a building when the resource starts.
60+
oop: true
61+
meta:
62+
- changelog:
63+
- version: '1.6.0 r23232'
64+
description: Added to the server side, thus the function is now shared.
65+
66+
client:
67+
<<: *shared
68+
examples:
69+
- path: examples/createBuilding-1.lua
70+
description: |
71+
This example shows you how to automatically create an object or building element according to certain parameters.
72+
73+
This script onlys works <b>CLIENTSIDE</b> due to createBuilding and [[engineGetModelPhysicalPropertiesGroup]].
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- Creates an 'object' or 'building' element based on certain parameters or one of the functions fails
2+
-- This function will throw an error when unexpected arguments are used
3+
-- TODO: Add LOD support
4+
function createObjectOrBuilding(modelID, x, y, z, rx, ry, rz, interior, dimension)
5+
-- Validate the arguments passed
6+
assert(type(modelID)=="number", "invalid modelID passed: " .. tostring(modelID))
7+
assert(type(x)=="number" and type(y)== "number" and type(z)=="number", "invalid position passed: " .. tostring(x) .. ", " .. tostring(y) .. ", " .. tostring(z))
8+
if not rx then rx = 0 end
9+
if not ry then ry = 0 end
10+
if not rz then rz = 0 end
11+
assert(type(rx)=="number" and type(ry)== "number" and type(rz)=="number", "invalid rotation passed: " .. tostring(rx) .. ", " .. tostring(ry) .. ", " .. tostring(rz))
12+
if not interior then interior = 0 end
13+
if not dimension then dimension = 0 end
14+
assert(type(interior)=="number" and interior >= 0 and interior <= 255, "invalid interior (must be 0-255) passed: " .. tostring(interior))
15+
assert(type(dimension)=="number" and dimension >= -1 and dimension <= 65535, "invalid dimension passed (must be -1 65535): " .. tostring(dimension))
16+
17+
-- Dynamic object models will always have a physical properties group different than -1.
18+
local isNonDynamic = engineGetModelPhysicalPropertiesGroup(modelID) == -1
19+
-- Buildings can't be affected by dimension
20+
local isNormalDimension = dimension == 0
21+
-- Buildings can't be placed outside regular map boundaries
22+
local isInsideMapLimits = x >= -3000 and x <= 3000 and y >= -3000 and y <= 3000
23+
24+
local obj, bld
25+
if isNonDynamic and isNormalDimension and isInsideMapLimits then
26+
bld = createBuilding(modelID, x, y, z, rx, ry, rz, interior)
27+
assert(bld, ("Failed to create building with model ID %d at %f, %f, %f in interior %d"):format(modelID, x, y, z, interior))
28+
else
29+
obj = createObject(modelID, x, y, z, rx, ry, rz, false)
30+
assert(obj, ("Failed to create object with model ID %d at %f, %f, %f"):format(modelID, x, y, z))
31+
setElementInterior(obj, interior)
32+
setElementDimension(obj, dimension)
33+
end
34+
return obj or bld
35+
end
36+
37+
-- Test this create object/building function
38+
addCommandHandler("testobject", function()
39+
-- This would be your object's model ID
40+
local modelID = 3556
41+
-- This would be your object's position coordinates
42+
local x, y, z = 0, 0, 69
43+
-- This would be your object's rotation coordinates
44+
local rx, ry, rz = 0, 0, 90
45+
-- This would be your object's interior ID
46+
local interior = 0
47+
-- This would be your object's dimension ID
48+
local dimension = 0
49+
50+
-- We use pcall to catch any errors that may be thrown
51+
local success, element = pcall(createObjectOrBuilding, modelID, x, y, z, rx, ry, rz, interior, dimension)
52+
if not success then
53+
outputDebugString(("Failed to create object or building: %s"):format(tostring(element)), 4, 255, 25, 25)
54+
return
55+
end
56+
57+
-- OPTIONAL: Then you can apply additional properties, like so:
58+
if getElementType(element) == "object" then
59+
setObjectScale(element, 1.69)
60+
setObjectBreakable(element, false)
61+
setElementAlpha(element, 230)
62+
end
63+
setElementCollisionsEnabled(element, true)
64+
setElementFrozen(element, false)
65+
end)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- create a *building* at a specified position with a specified rotation
2+
createBuilding(4550, 1985, -2544, 94, 0,0,0) -- Maze Bank Tower in LS airport
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- create a *building* at a specified position with a specified rotation
2+
Building(4550, 1985, -2544, 94, 0,0,0) -- Maze Bank Tower in LS airport

0 commit comments

Comments
 (0)