1
0
Fork 0

Add resource_tool and random_tool

This commit is contained in:
Michaël Lemaire 2021-03-17 00:56:26 +01:00
parent b5e40298c5
commit ef84d58273
2 changed files with 34 additions and 0 deletions

12
random_tool.gd Normal file
View file

@ -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

22
resource_tool.gd Normal file
View file

@ -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