./system_lib.lua

  1. ---------------------- 
  2. -- System Functions -- 
  3. ---------------------- 
  4.  
  5. _UGGlobals = { } 
  6. _DynamicGlobals = { } 
  7. _ActiveTable = "" 
  8.  
  9. -- Assign a value to global variable. Any nil-assignment raises an error. 
  10. -- 
  11. function _CatchNilAssignment(t, k, v) 
  12. 	if (v == nil) then 
  13. 		error("attempted to initialize global variable '"..k.."' with nil value") 
  14. 	elseif (_ActiveTable ~= "") then 
  15. 		rawset( _DynamicGlobals[ _ActiveTable ], k, v ) 
  16. 	else 
  17. 		rawset(t, k, v) 
  18. 	end 
  19. end 
  20.  
  21. -- Catch an attempt to write to an undefined global variable 
  22. -- 
  23. function _CatchUndefinedGlobalWrite(t, k, v) 
  24. 	if (v == nil) then 
  25. 		error("attempted to initialize global variable '"..k.."' with nil value") 
  26. 		return 
  27. 	end 
  28.  
  29. 	for i, table in pairs( _DynamicGlobals ) do 
  30.  
  31. 		local val = rawget( table, k ) 
  32. 		if (val ~= nil) then 
  33. 			rawset( table, k, v ) 
  34. 			return 
  35. 		end 
  36.  
  37. 	end 
  38.  
  39. 	error("attempted to write to undefined global variable '"..k.."'") 
  40. end 
  41.  
  42. -- Find a value in the Dynamic table since it's not in UGGlobals 
  43. -- 
  44. function _GetDynamicGlobal( t, k ) 
  45. 	for i, table in pairs( _DynamicGlobals ) do 
  46.  
  47. 		local v = rawget( table, k ) 
  48. 		if (v ~= nil) then 
  49. 			return v 
  50. 		end 
  51.  
  52. 	end 
  53.  
  54. 	error("attempted to read undefined global variable '"..k.."'") 
  55. end 
  56.  
  57. -- Find a value in any global space, return nil if not found 
  58. -- 
  59. function _GetAnyGlobalSilent( k ) 
  60. 	local v 
  61. 	 
  62. 	v = rawget( _UGGlobals, k ) 
  63. 	 
  64. 	if v ~= nil then 
  65. 		return v 
  66. 	end 
  67. 	 
  68. 	for i, table in pairs( _DynamicGlobals ) do 
  69. 		v = rawget( table, k ) 
  70. 		if v ~= nil then 
  71. 			return v 
  72. 		end 
  73.  
  74. 	end 
  75.  
  76. 	return nil 
  77. end 
  78.  
  79. -- Prepare to load a lua script into the dynamic table 
  80. -- 
  81. function _PrepareForDynamicGlobals( filename ) 
  82. 	_DynamicGlobals[ filename ] = { } 
  83. 	_ActiveTable = filename 
  84.  
  85. 	setmetatable( _UGGlobals, {__index = _GetDynamicGlobal, __newindex = _CatchNilAssignment} ) 
  86. end 
  87.  
  88. -- Callback for loading of dynamic table complete 
  89. -- 
  90. function _DynamicGlobalsLoadComplete( ) 
  91. 	_ActiveTable = "" 
  92.  
  93. 	setmetatable( _UGGlobals, {__index = _GetDynamicGlobal, __newindex = _CatchUndefinedGlobalWrite} ) 
  94. end 
  95.  
  96. -- Unload a table of Dynamic globals 
  97. -- 
  98. function _UnloadDynamicGlobals( filename ) 
  99. 	rawset( _DynamicGlobals, filename, nil ) -- erase the entry from the table 
  100. end 
  101.  
  102. -- Delay execution for a certain amount of time 
  103. -- 
  104. -- duration:	duration of delay, in seconds (if time_seconds <= 0, execution will delay for exactly one frame) 
  105. -- 
  106. function delay(duration) 
  107. 	if duration == nil then 
  108. 		duration = 0 
  109. 	end 
  110.  
  111. 	local elapsed_time = 0.0 
  112.  
  113. 	repeat 
  114. 		thread_yield() 
  115. 		elapsed_time = elapsed_time + get_frame_time() 
  116. 	until (elapsed_time >= duration) 
  117. end 
  118.