From ef84d58273375c599dc89e707035cf75fe9d8cdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Wed, 17 Mar 2021 00:56:26 +0100 Subject: [PATCH] Add resource_tool and random_tool --- random_tool.gd | 12 ++++++++++++ resource_tool.gd | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 random_tool.gd create mode 100644 resource_tool.gd diff --git a/random_tool.gd b/random_tool.gd new file mode 100644 index 0000000..7f4894e --- /dev/null +++ b/random_tool.gd @@ -0,0 +1,12 @@ +tool + +# Tools for randomization + +static func pick(array): + """ Pick a random item in an array + """ + if array.size(): + # TODO more uniform method + return array[randi() % array.size()] + else: + return null diff --git a/resource_tool.gd b/resource_tool.gd new file mode 100644 index 0000000..96acb39 --- /dev/null +++ b/resource_tool.gd @@ -0,0 +1,22 @@ +tool + +# Resources tools + +static func list_from_dir(path: String, pattern="*") -> PoolStringArray: + """ List all resources in a directory (non-recursive) + """ + var result := PoolStringArray() + + path = path.rstrip("/") + var dir := Directory.new() + if dir.open(path) == OK: + dir.list_dir_begin() + var file_name := dir.get_next() + while file_name != "": + if file_name.match(pattern) and not dir.current_is_dir(): + result.append(path + "/" + file_name) + file_name = dir.get_next() + else: + push_error("Can't read directory: %s" % path) + + return result