|
| 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) |
0 commit comments