BaseLoader.gd
· 1.7 KiB · GDScript3
Raw
Playground
extends Node
class_name BaseLoader
var items = {}
var TYPES := []
var RESOURCE_DIR := ""
var RECURSIVE := true
var _resources_to_load := []
func _notification(what: int) -> void:
if what == NOTIFICATION_READY:
if get_tree().current_scene.scene_file_path != "res://Boot.tscn":
get_resource_list() # warning-ignore:return_value_discarded
start_loading()
func start_loading(progress_bar: ProgressBar = null) -> void:
for i in len(_resources_to_load):
var resource_path: String = _resources_to_load[i]
var file_name: String = resource_path.split(".")[0]
var resource = load(RESOURCE_DIR + resource_path)
items[file_name] = resource
if progress_bar:
progress_bar.value += 1
if int(progress_bar.value) % 4 == 0:
await get_tree().process_frame
func get_resource_list() -> Array:
if _resources_to_load.is_empty():
_resources_to_load = _get_resource_list()
return _resources_to_load
func _get_resource_list(subdir: String = "") -> Array:
var dir := DirAccess.open(RESOURCE_DIR + subdir)
var res_list := []
if subdir != "":
subdir += "/"
if dir:
dir.list_dir_begin() # warning-ignore:return_value_discarded# TODOGODOT4 fill missing arguments https://github.com/godotengine/godot/pull/40547
var file_name = dir.get_next()
while file_name != "":
if dir.current_is_dir():
if RECURSIVE:
res_list.append_array(
_get_resource_list(subdir + file_name)
)
else:
if OS.has_feature("template"):
file_name = file_name.replace(".import", "").replace(".remap", "")
var results = file_name.split(".")
if results[-1] in TYPES:
res_list.append(subdir + file_name)
file_name = dir.get_next()
return res_list
| 1 | extends Node |
| 2 | class_name BaseLoader |
| 3 | |
| 4 | var items = {} |
| 5 | |
| 6 | var TYPES := [] |
| 7 | var RESOURCE_DIR := "" |
| 8 | var RECURSIVE := true |
| 9 | var _resources_to_load := [] |
| 10 | |
| 11 | |
| 12 | func _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 | |
| 18 | func 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 | |
| 31 | func 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 | |
| 37 | func _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 |