Python1320

gmod loadingorder unfuck

Aug 23rd, 2014
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.39 KB | None | 0 0
  1. local functions = {  }
  2. local added = false
  3. local function Stop()
  4.     hook.Remove("Think", "NextThinkHelper")
  5. end
  6.  
  7. local function error_func(line)
  8.     ErrorNoHalt("NextThink Error: "..debug.traceback(line,2))
  9. end
  10.  
  11. local function Think()
  12.     local n = #functions
  13.     if n == 0 then
  14.         Stop()
  15.         return
  16.     end
  17.  
  18.     for i = 1, n do
  19.         local func = table.remove(functions, 1)
  20.         xpcall(func,error_func)
  21.     end
  22.  
  23. end
  24.  
  25. local function Start()
  26.     if added then
  27.         return
  28.     end
  29.  
  30.     hook.Add("Think", "NextThinkHelper", Think)
  31. end
  32.  
  33. function RunNextThink(func)
  34.     if not isfunction(func) then
  35.         error("Expected function", 2)
  36.     end
  37.  
  38.     functions[#functions + 1] = func
  39.     Start()
  40. end
  41.  
  42.  
  43.  
  44.  
  45. local functions = { }
  46. local initialized = false
  47.  
  48. local function error_func(line)
  49.     ErrorNoHalt("OnInitialize: " .. debug.traceback(line, 2))
  50. end
  51.  
  52. local function Initialize()
  53.    
  54.     initialized = true
  55.    
  56.     for i = 1, #functions do
  57.         local func = functions[i]
  58.         xpcall(func, error_func)
  59.     end
  60.  
  61.     functions = nil
  62.     RunOnInitialize = function()
  63.         error("RunOnInitialize called too late",2)
  64.     end
  65.     hook.Remove("Initialize", "RunOnInitializeHelper")
  66. end
  67.  
  68.  
  69. function Init(func)
  70.     if not isfunction(func) then
  71.         error("Expected function", 2)
  72.     end
  73.    
  74.     if initialized then
  75.         error("RunOnInitialize called too late",2)
  76.     end
  77.    
  78.     functions[#functions + 1] = func
  79. end
  80.  
  81. hook.Add("Initialize", "RunOnInitializeHelper", Initialize)
Advertisement
Add Comment
Please, Sign In to add comment