Advertisement
Guest User

Preload

a guest
Jun 6th, 2020
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. --Library created by RickG00 and WD200019
  2.  
  3. --  WARNING: this library requires the version of CYF 0.6.3 or higher, lower versions or Unitale won't work.
  4.  
  5. --  How to use:
  6. --      1)Put this file into the "Libraries" folder of your mod;
  7. --      2)In the encounter script, write " require "Libraries/Preload" " in the "EncounterStarting" function;
  8. --      3)Profit??
  9.  
  10. --  Mods with tons of sprites, musics and sounds might take a while to start, but they will be lag-free when those files are used later.
  11.  
  12.  
  13. function Search_Folders(path, searching_for)
  14.  
  15.     local temp_table = Misc.ListDir(path, true)
  16.     for i = 1, #temp_table do
  17.         local temp_table2 = Misc.ListDir(path .. "/" .. temp_table[i], false)
  18.         local new_path = string.gsub(path, searching_for, "", 1) .. "/" .. temp_table[i] .. "/"
  19.         Load_Files(temp_table2, new_path, searching_for)
  20.         Search_Folders(path .. "/" .. temp_table[i], searching_for)
  21.     end
  22.  
  23. end
  24.  
  25. function Load_Files(t, path, searching_for)
  26.  
  27.     for j = 1, #t do
  28.         if searching_for == "Sprites" then
  29.            
  30.             if string.endsWith(t[j], ".png") then
  31.                 Sprite_File.Set(path .. t[j]:gsub(".png", ''))
  32.             end
  33.            
  34.         elseif searching_for == "Sounds" or searching_for == "Audio" then
  35.        
  36.             local file_type = nil
  37.             if string.endsWith(t[j], ".wav") then
  38.                 file_type = "wav"
  39.             elseif string.endsWith(t[j], ".ogg") then
  40.                 file_type = "ogg"
  41.             end
  42.            
  43.             if file_type == "wav" or file_type == "ogg" then
  44.                 if searching_for == "Sounds" then
  45.                     NewAudio.PlaySound("|__Preload_Channel__|", path .. t[j]:gsub("." .. file_type, ''), false, 0)
  46.                 else
  47.                     NewAudio.PlayMusic("|__Preload_Channel__|", path .. t[j]:gsub("." .. file_type, ''), false, 0)
  48.                 end
  49.             end
  50.  
  51.         end
  52.     end
  53.  
  54. end
  55.  
  56. if isCYF == true then
  57.     if CYFversion >= "0.6.3" and CYFversion != "1.0" then
  58.    
  59.         if CYFversion == "0.6.3" or Misc.DirExists("Sprites") then
  60.             Sprite_File = CreateSprite("empty")
  61.            
  62.             Load_Files(Misc.ListDir("Sprites", false), "", "Sprites")
  63.             Search_Folders("Sprites", "Sprites")
  64.            
  65.             Sprite_File.Remove()
  66.         end
  67.  
  68.         NewAudio.CreateChannel("|__Preload_Channel__|")
  69.        
  70.         if CYFversion == "0.6.3" or Misc.DirExists("Sounds") then
  71.             Load_Files(Misc.ListDir("Sounds", false), "", "Sounds")
  72.             Search_Folders("Sounds", "Sounds")
  73.         end
  74.        
  75.         if CYFversion == "0.6.3" or Misc.DirExists("Audio") then
  76.             Load_Files(Misc.ListDir("Audio", false), "", "Audio")
  77.             Search_Folders("Audio", "Audio")
  78.         end
  79.  
  80.         NewAudio.DestroyChannel("|__Preload_Channel__|")
  81.     else
  82.         DEBUG("Your CYF version is too outdated, it was not possible to preload anything.")
  83.     end
  84. else
  85.     DEBUG("You are not using CYF, it was not possible to preload anything.")
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement