Add resource_tool and random_tool
This commit is contained in:
parent
b5e40298c5
commit
ef84d58273
12
random_tool.gd
Normal file
12
random_tool.gd
Normal 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
22
resource_tool.gd
Normal 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
|
Reference in a new issue