Browse Source

Update resource_queue.gd for Godot 3.2 (#3228)

Fix warnings, fix using a deprecated method, style fixes.
Aaron Franke 5 years ago
parent
commit
b104610ae1
1 changed files with 24 additions and 24 deletions
  1. 24 24
      tutorials/io/files/resource_queue.gd

+ 24 - 24
tutorials/io/files/resource_queue.gd

@@ -2,21 +2,24 @@ var thread
 var mutex
 var sem
 
-var time_max = 100 # msec
+var time_max = 100 # Milliseconds.
 
 var queue = []
 var pending = {}
 
-func _lock(caller):
+func _lock(_caller):
 	mutex.lock()
 
-func _unlock(caller):
+
+func _unlock(_caller):
 	mutex.unlock()
 
-func _post(caller):
+
+func _post(_caller):
 	sem.post()
 
-func _wait(caller):
+
+func _wait(_caller):
 	sem.wait()
 
 
@@ -25,8 +28,7 @@ func queue_resource(path, p_in_front = false):
 	if path in pending:
 		_unlock("queue_resource")
 		return
-
-	elif ResourceLoader.has(path):
+	elif ResourceLoader.has_cached(path):
 		var res = ResourceLoader.load(path)
 		pending[path] = res
 		_unlock("queue_resource")
@@ -43,6 +45,7 @@ func queue_resource(path, p_in_front = false):
 		_unlock("queue_resource")
 		return
 
+
 func cancel_resource(path):
 	_lock("cancel_resource")
 	if path in pending:
@@ -51,6 +54,7 @@ func cancel_resource(path):
 		pending.erase(path)
 	_unlock("cancel_resource")
 
+
 func get_progress(path):
 	_lock("get_progress")
 	var ret = -1
@@ -60,9 +64,9 @@ func get_progress(path):
 		else:
 			ret = 1.0
 	_unlock("get_progress")
-
 	return ret
 
+
 func is_ready(path):
 	var ret
 	_lock("is_ready")
@@ -70,16 +74,15 @@ func is_ready(path):
 		ret = !(pending[path] is ResourceInteractiveLoader)
 	else:
 		ret = false
-
 	_unlock("is_ready")
-
 	return ret
 
+
 func _wait_for_resource(res, path):
 	_unlock("wait_for_resource")
 	while true:
 		VisualServer.sync()
-		OS.delay_usec(16000) # wait 1 frame
+		OS.delay_usec(16000) # Wait approximately 1 frame.
 		_lock("wait_for_resource")
 		if queue.size() == 0 || queue[0] != res:
 			return pending[path]
@@ -95,13 +98,11 @@ func get_resource(path):
 				var pos = queue.find(res)
 				queue.remove(pos)
 				queue.insert(0, res)
-
+			
 			res = _wait_for_resource(res, path)
-
 			pending.erase(path)
 			_unlock("return")
 			return res
-
 		else:
 			var res = pending[path]
 			pending.erase(path)
@@ -111,33 +112,32 @@ func get_resource(path):
 		_unlock("return")
 		return ResourceLoader.load(path)
 
+
 func thread_process():
 	_wait("thread_process")
-
 	_lock("process")
-
+	
 	while queue.size() > 0:
-
 		var res = queue[0]
-
 		_unlock("process_poll")
 		var ret = res.poll()
 		_lock("process_check_queue")
-
+		
 		if ret == ERR_FILE_EOF || ret != OK:
 			var path = res.get_meta("path")
-			if path in pending: # else it was already retrieved
+			if path in pending: # Else, it was already retrieved.
 				pending[res.get_meta("path")] = res.get_resource()
-
-			queue.erase(res) # something might have been put at the front of the queue while we polled, so use erase instead of remove
-
+			# Something might have been put at the front of the queue while
+			# we polled, so use erase instead of remove.
+			queue.erase(res)
 	_unlock("process")
 
 
-func thread_func(u):
+func thread_func(_u):
 	while true:
 		thread_process()
 
+
 func start():
 	mutex = Mutex.new()
 	sem = Semaphore.new()