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