Skip to content

Commit c13930e

Browse files
Fix #59 setElementResource not getting sourceResource properly
1 parent fcf8cd2 commit c13930e

5 files changed

Lines changed: 61 additions & 33 deletions

File tree

[examples]/test_vehicles/meta.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<meta>
22
<include resource="newmodels_azul" minversion="5.0.0"/>
33
<script src="s_vehicles.lua" type="server"/>
4+
<script src="s_vehicles_alt.lua" type="server"/>
5+
<script src="s_test_cmd.lua" type="server"/>
46
<aclrequest>
57
<right name="function.loadstring" access="true"/>
68
</aclrequest>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- Outputs, for example:
2+
-- This vehicle has the custom model ID -1, which is based on the default model ID 490 (FBI Rancher)
3+
addCommandHandler("myvehicle", function(player)
4+
local vehicle = getPedOccupiedVehicle(player)
5+
if not vehicle then
6+
outputChatBox("You are not in a vehicle", player, 255, 0, 0)
7+
return
8+
end
9+
local serversideModel = getElementModel(vehicle)
10+
local customModel = exports["newmodels_azul"]:getElementCustomModel(vehicle)
11+
if not customModel then
12+
outputChatBox("This vehicle has the default model ID " .. serversideModel .. " ("..(tostring(getVehicleNameFromModel(serversideModel)) or "")..")", player, 0, 255, 0)
13+
else
14+
local baseModel = exports["newmodels_azul"]:getElementBaseModel(vehicle)
15+
if not baseModel then
16+
outputChatBox("This vehicle has the custom model ID " .. customModel .. ", but the base model ID could not be determined", player, 255, 0, 0)
17+
return
18+
end
19+
outputChatBox("This vehicle has the custom model ID " .. customModel .. ", which is based on the default model ID " .. baseModel .. " ("..(tostring(getVehicleNameFromModel(baseModel)) or "")..")", player, 0, 255, 0)
20+
end
21+
end, false, false)
Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,22 @@
1-
-- Loads newmodels functions, which allow usage of custom model IDs "as if they were normal IDs"
2-
loadstring(exports.newmodels_azul:import())()
1+
-- Method with exports
2+
-- These vehicles will be destroyed if newmodels_azul stops
3+
-- because they are children of that resource.
34

45
-- Vehicle model, x,y,z, rx,ry,rz, interior,dimension
56
local VEHICLE_SPAWNS = {
6-
{525, -938.74, 1034.21, 23.59, 3.42, 2.85, 20.27, 0, 0},
77
{490, -941.95, 1043.03, 24.25, 355.90, 356.51, 199.00, 0, 0},
8-
{-1, -951.79, 1069.05, 25.96, 356.28, 356.34, 204.01, 0, 0},
9-
{-5, -944.88, 1051.90, 24.84, 355.97, 356.23, 198.86, 0, 0},
8+
{-1, -947.94, 1060.05, 25.96, 356.28, 356.34, 204.01, 0, 0},
109
}
1110

1211
local function createVehicles()
1312
for i, data in ipairs(VEHICLE_SPAWNS) do
1413
local model, x, y, z, rx, ry, rz, interior, dimension = unpack(data)
15-
local vehicle = createVehicle(model, x, y, z, rx, ry, rz)
14+
local vehicle = exports["newmodels_azul"]:createVehicle(model, x, y, z, rx, ry, rz)
1615
if vehicle then
1716
setElementInterior(vehicle, interior)
1817
setElementDimension(vehicle, dimension)
19-
print("#" .. i .. " - Created vehicle with ID " .. model .. " at " .. x .. ", " .. y .. ", " .. z)
18+
print("test_vehicles #" .. i .. " - Created vehicle with ID " .. model .. " at " .. x .. ", " .. y .. ", " .. z)
2019
end
2120
end
2221
end
2322
addEventHandler("onResourceStart", resourceRoot, createVehicles, false)
24-
25-
-- Outputs, for example:
26-
-- This vehicle has the custom model ID -1, which is based on the default model ID 490 (FBI Rancher)
27-
addCommandHandler("myvehicle", function(player)
28-
local vehicle = getPedOccupiedVehicle(player)
29-
if not vehicle then
30-
outputChatBox("You are not in a vehicle", player, 255, 0, 0)
31-
return
32-
end
33-
local model = getElementModel(vehicle)
34-
local baseModel = getElementBaseModel(vehicle)
35-
if model == baseModel then
36-
outputChatBox("This vehicle has the default model ID " .. model .. " ("..(tostring(getVehicleNameFromModel(model)) or "")..")", player, 0, 255, 0)
37-
else
38-
if not baseModel then
39-
outputChatBox("This vehicle has the custom model ID " .. model .. ", but the base model ID could not be determined", player, 255, 0, 0)
40-
return
41-
end
42-
outputChatBox("This vehicle has the custom model ID " .. model .. ", which is based on the default model ID " .. baseModel .. " ("..(tostring(getVehicleNameFromModel(baseModel)) or "")..")", player, 0, 255, 0)
43-
end
44-
end, false, false)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- Alternative method with loadstring
2+
-- These vehicles will not be destroyed if newmodels_azul stops
3+
-- because the elements are children of this resource on creation.
4+
5+
-- Loads newmodels functions, which allow usage of custom model IDs "as if they were normal IDs"
6+
loadstring(exports.newmodels_azul:import())()
7+
8+
-- Vehicle model, x,y,z, rx,ry,rz, interior,dimension
9+
local VEHICLE_SPAWNS = {
10+
{525, -938.74, 1034.21, 23.59, 3.42, 2.85, 20.27, 0, 0},
11+
{-5, -944.88, 1051.90, 24.84, 355.97, 356.23, 198.86, 0, 0},
12+
}
13+
14+
local function createVehicles()
15+
for i, data in ipairs(VEHICLE_SPAWNS) do
16+
local model, x, y, z, rx, ry, rz, interior, dimension = unpack(data)
17+
local vehicle = createVehicle(model, x, y, z, rx, ry, rz)
18+
if vehicle then
19+
setElementInterior(vehicle, interior)
20+
setElementDimension(vehicle, dimension)
21+
print("test_vehicles [alt] #" .. i .. " - Created vehicle with ID " .. model .. " at " .. x .. ", " .. y .. ", " .. z)
22+
end
23+
end
24+
end
25+
addEventHandler("onResourceStart", resourceRoot, createVehicles, false)

newmodels_azul/scripts/core/shared_exported.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ function isDefaultID(elementType, id)
139139
end
140140

141141
function isValidElement(element)
142+
if not element or not isElement(element) then
143+
return false
144+
end
142145
local elementType = getElementType(element)
143146
for _, elementType2 in pairs(VALID_ELEMENT_TYPES) do
144147
if elementType == elementType2 then
@@ -151,17 +154,16 @@ function getValidElementTypes()
151154
return VALID_ELEMENT_TYPES
152155
end
153156

157+
-- In MTA Elements are always destroyed when the resource that created them is stopped: this cannot be changed.
158+
-- So we use an internal table to keep track of elements created by resources.
154159
newmodelsUtils.setElementResource = function(element, theResource)
155160
if isElement(element) then
156-
if not isElement(theResource) then theResource = resource end
161+
-- if not isElement(theResource) then theResource = resource end
162+
if (not theResource) or (not isElement(getResourceRootElement(theResource))) then theResource = resource end
157163
if type(newmodelsUtils.resources[theResource]) ~= "table" then
158164
newmodelsUtils.resources[theResource] = {}
159165
end
160166
table.insert(newmodelsUtils.resources[theResource], element)
161-
local dynRoot = getResourceDynamicElementRoot(theResource)
162-
if dynRoot then
163-
setElementParent(element, dynRoot)
164-
end
165167
end
166168
end
167169

0 commit comments

Comments
 (0)