Последняя активность 1748794558

BaseLoader.gd Исходник Playground
1extends Node
2class_name BaseLoader
3
4var items = {}
5
6var TYPES := []
7var RESOURCE_DIR := ""
8var RECURSIVE := true
9var _resources_to_load := []
10
11
12func _notification(what: int) -> void:
13 if what == NOTIFICATION_READY:
14 if get_tree().current_scene.scene_file_path != "res://Boot.tscn":
15 get_resource_list() # warning-ignore:return_value_discarded
16 start_loading()
17
18func start_loading(progress_bar: ProgressBar = null) -> void:
19 for i in len(_resources_to_load):
20 var resource_path: String = _resources_to_load[i]
21 var file_name: String = resource_path.split(".")[0]
22 var resource = load(RESOURCE_DIR + resource_path)
23
24 items[file_name] = resource
25 if progress_bar:
26 progress_bar.value += 1
27 if int(progress_bar.value) % 4 == 0:
28 await get_tree().process_frame
29
30
31func get_resource_list() -> Array:
32 if _resources_to_load.is_empty():
33 _resources_to_load = _get_resource_list()
34 return _resources_to_load
35
36
37func _get_resource_list(subdir: String = "") -> Array:
38 var dir := DirAccess.open(RESOURCE_DIR + subdir)
39 var res_list := []
40
41 if subdir != "":
42 subdir += "/"
43
44 if dir:
45 dir.list_dir_begin() # warning-ignore:return_value_discarded# TODOGODOT4 fill missing arguments https://github.com/godotengine/godot/pull/40547
46 var file_name = dir.get_next()
47
48 while file_name != "":
49 if dir.current_is_dir():
50 if RECURSIVE:
51 res_list.append_array(
52 _get_resource_list(subdir + file_name)
53 )
54
55 else:
56 if OS.has_feature("template"):
57 file_name = file_name.replace(".import", "").replace(".remap", "")
58 var results = file_name.split(".")
59 if results[-1] in TYPES:
60 res_list.append(subdir + file_name)
61
62 file_name = dir.get_next()
63
64 return res_list
65