-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_c.lua
61 lines (52 loc) · 1.66 KB
/
sync_c.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local syncedModels = {}
local ids = {}
-- Request id for models
local function RequestTableModel(modelTbl, modelType)
local tbl = {}
for modelName, modelData in pairs(modelTbl or {}) do
local id = ML.Funcs.RequestModel(modelType, modelName, modelData)
table.insert(tbl, id)
end
return tbl
end
-- Request ids for synced models
-- and send ids to the server
local function RequestIDs()
ids.vehicles = RequestTableModel(syncedModels.vehicles, 'vehicle')
ids.peds = RequestTableModel(syncedModels.peds, 'ped')
triggerServerEvent(resourceName..':SendSyncedIDs', resourceRoot, ids)
end
-- Sync incoming models
addEvent(resourceName..':SendSyncedModels', true)
addEventHandler(resourceName..':SendSyncedModels', localPlayer, function(models)
assert(type(models) == 'table', 'Expected table in "modelloader:getModels", got '..type(models))
syncedModels = models
RequestIDs()
end)
-- When resource starts, request synced models
-- and request ids for them
addEventHandler('onClientResourceStart', resourceRoot, function()
-- if syncedModels table is empty
if not next(syncedModels) then
-- request synced models from the server
triggerServerEvent(resourceName..':RequestSyncedModels', resourceRoot)
end
-- request ids for models
RequestIDs()
end)
-- When resource stops, free all allocated models
addEventHandler('onClientResourceStop', resourceRoot, function()
for _,id in ipairs(ids.vehicles) do
FreeModel(id)
end
for _,id in ipairs(ids.peds) do
FreeModel(id)
end
end)
-- Export functions --
function GetIDs()
return ids
end
function GetModels()
return syncedModels
end