Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. local objectData = {
  2.     [1] = {txd="1.txd", dff="1.dff"},
  3.     [2] = {txd="2.txd", dff="2.dff"},
  4.     [5] = {txd="5.txd", dff="5.dff"}
  5. }
  6.  
  7. local downloadStatus = {}
  8.  
  9. function init()
  10.     --Loop our object data, and intiliaze a download "state"
  11.     for id, data in pairs(objectData) do
  12.         downloadStatus[id] = {}
  13.         downloadStatus[id].txd, downloadStatus[id].dff = false, false
  14.        
  15.         --Start downloading
  16.         downloadFile(data.txd)
  17.         downloadFile(data.dff)
  18.     end
  19. end
  20. addEventHandler("onClientResourceStart", resourceRoot, init) --As an example, start with the resource
  21.  
  22. function handleObjectDownloads(file, success)
  23.     --If the file wasn't downloaded, return with an error message
  24.     if not success then
  25.         return outputDebugString("Download failed for "..file)
  26.     end
  27.    
  28.     --We could make this more efficient, but for simplicity I'll keep it like this
  29.     --Loop through all the object data and check if the object "package" has been downloaded, else, store the download completion state for this file.
  30.     local id, filetype
  31.     for i, data in pairs(objectData) do
  32.         if data.txd == file then
  33.             filetype = "txd"
  34.         elseif data.dff == file then
  35.             filetype = "dff"
  36.         end
  37.        
  38.         if filetype then
  39.             id = i
  40.         end
  41.     end
  42.    
  43.     --The file downloaded is not relevant
  44.     if not id then
  45.         return false
  46.     end
  47.    
  48.     --Set download status for this file to true
  49.     downloadStatus[id][filetype] = true
  50.    
  51.     --If all the files aren't downloaded yet, then return and wait
  52.     for i, status in ipairs(downloadStatus[id]) do
  53.         if not status then
  54.             return false
  55.         end
  56.     end
  57.    
  58.     --All files in this "package" are downloaded, so import the txd/dffs
  59.     local txd = engineLoadTXD(objectData[id].txd)
  60.     engineImportTXD(txd, id)
  61.    
  62.     local dff = engineLoadDFF(objectData[id].dff, id)
  63.     engineReplaceModel(dff, id)
  64.    
  65.     return true
  66. end
  67. addEventHandler("onClientFileDownloadComplete", root, handleObjectDownloads)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement