Skip to content

Commit 88bbf6a

Browse files
basic utility scripts
1 parent dc56f82 commit 88bbf6a

File tree

7 files changed

+142
-11
lines changed

7 files changed

+142
-11
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@
7171
- **Persistence Manager**
7272
- TODO
7373

74-
## 💎 Globals
74+
## 🤖 Code
75+
76+
### 💎 Globals
7577

7678
- **Configuration**
7779
- Configure Plugins & Project depending on environment.
@@ -86,7 +88,7 @@
8688
- **Wrapper**
8789
- **SceneManagerWrapper** - call SceneManager with options presets Resource.
8890

89-
## 🎬 Scenes
91+
### 🎬 Scenes
9092

9193
- **BootSplash**
9294
- Main Scene, smoothly transitions from boot to true main scene.
@@ -97,6 +99,14 @@
9799
- **OptionsMenu**
98100
- TODO
99101

102+
### 📄 Scripts
103+
104+
- **FileSystemUtils**
105+
- **MathUtils**
106+
- **NodeUtils**
107+
- **RandomUtils**
108+
109+
100110
## 🚀 Deployment
101111

102112
- **TODO**

godot/autoload/reference/reference.gd

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ var _resource_references: Dictionary = {}
66

77

88
func get_scene_manager_options(resource_name: String) -> SceneManagerOptions:
9-
var key: String = _get_key(resource_name, SceneManagerOptions)
9+
return _get_resource(resource_name, SceneManagerOptions)
10+
11+
12+
func _get_resource(resource_name: String, type: Variant) -> Resource:
13+
var key: String = _get_key(resource_name, type)
1014
return _resource_references[key]
1115

1216

1317
func _ready() -> void:
1418
Log.debug("AUTOLOAD READY: ", name)
1519

16-
_init()
17-
18-
19-
func _init() -> void:
20-
var root_path: String = PathConsts.RESOURCES
21-
var paths: Array[String] = FileSystemUtils.get_paths(root_path, RESOURCE_EXTENSION)
20+
var paths: Array[String] = FileSystemUtils.get_paths(PathConsts.RESOURCES, RESOURCE_EXTENSION)
2221
_resource_references = _load_resources(paths)
2322

2423

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class_name CharsetConsts
2+
3+
const DIGITS: String = "0123456789"
4+
const LOWERCASE: String = "abcdefghijklmnopqrstuvwxyz"
5+
const UPPERCASE: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
6+
const LETTERS: String = LOWERCASE + UPPERCASE
7+
const USERNAME: String = LETTERS + DIGITS
8+
const SPECIAL: String = " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
9+
const ASCII: String = USERNAME + SPECIAL

godot/scripts/utils/file_system_utils.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const EXTENSION_SEPARATOR = "."
77
## Converts full file path to file name, removing the path prefix and the extension suffix.
88
static func get_file_name(path: String) -> String:
99
var parts: PackedStringArray = path.split(PATH_SEPARATOR)
10-
var output: String = parts[parts.size() - 1]
11-
return output.split(EXTENSION_SEPARATOR)[0]
10+
var file_name: String = parts[parts.size() - 1]
11+
return file_name.split(EXTENSION_SEPARATOR)[0]
1212

1313

1414
## Returns full file paths of all files under path, with given extension.

godot/scripts/utils/math_utils.gd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class_name MathUtils
2+
3+
4+
## integer power (edge cases in order: 0^x = 0, 1^x = 0, x^0 = 1)
5+
static func pow_int(base: int, exponent: int) -> int:
6+
if base == 0 or exponent < 0:
7+
return 0
8+
if base == 1 or exponent == 0:
9+
return 1
10+
11+
var result: int = 1
12+
for i: int in range(exponent):
13+
result *= base
14+
return result

godot/scripts/utils/node_utils.gd

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class_name NodeUtils
2+
3+
4+
## remove child from node
5+
static func remove_child(node: Node, child: Node) -> void:
6+
node.remove_child(child)
7+
child.queue_free()
8+
9+
10+
## remove all children of given node
11+
static func remove_children(node: Node) -> void:
12+
for child: Node in node.get_children():
13+
remove_child(node, child)
14+
15+
16+
## remove all children of given node with given type
17+
static func remove_children_of(node: Node, type: Variant) -> void:
18+
for child: Node in node.get_children():
19+
if is_instance_of(child, type):
20+
remove_child(node, child)
21+
22+
23+
## remove child of given node at given index (allows large and negative index, looping around)
24+
static func remove_child_at(parent: Node, index: int) -> void:
25+
index = index % parent.get_child_count()
26+
var child: Node = parent.get_child(index)
27+
parent.remove_child(child)
28+
child.queue_free()
29+
30+
31+
## remove last child of given node
32+
static func remove_child_back(parent: Node) -> void:
33+
remove_child_at(parent, -1)
34+
35+
36+
## remove first child of given node
37+
static func remove_child_front(parent: Node) -> void:
38+
remove_child_at(parent, 0)
39+
40+
41+
## add child to given node to last spot
42+
static func add_child_back(child: Node, parent: Node) -> void:
43+
parent.add_child(child)
44+
45+
46+
## add child to given node to first spot
47+
static func add_child_front(child: Node, parent: Node) -> void:
48+
parent.add_child(child)
49+
parent.move_child(child, 0)
50+
51+
52+
## add child to given node such that children remain sorted (assumes children are sorted before add)
53+
static func add_child_sorted(child: Node, parent: Node, compare_func: Callable) -> void:
54+
parent.add_child(child)
55+
var children: Array[Node] = parent.get_children()
56+
if children.size() == 0:
57+
return
58+
var position: int = children.bsearch_custom(child, compare_func)
59+
parent.move_child(child, position)
60+
61+
62+
## returns active theme of control node (control nodes inheirt parent theme if theirs is null)
63+
static func get_inherited_theme(control: Node) -> Resource:
64+
var theme: Resource = null
65+
while (control != null) and ("theme" in control):
66+
theme = control.theme
67+
if theme != null:
68+
break
69+
control = control.get_parent()
70+
return theme
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class_name RandomUtils
2+
3+
4+
## Given a dictionary, return random string key weighted by corresponding int values.
5+
## Example: {"gold": 10, "silver": 20, "copper": 70} has 20% chance to return "silver"
6+
## If weight_sum is not provided, it will be calculated.
7+
static func random_weighted_item(item_weight_dict: Dictionary, weight_sum: int = 0) -> String:
8+
if weight_sum <= 0:
9+
weight_sum = get_weights_sum(item_weight_dict)
10+
var roll: int = randi() % weight_sum
11+
for item: String in item_weight_dict:
12+
var weight: int = item_weight_dict[item]
13+
if roll < weight:
14+
return item
15+
roll -= weight
16+
return item_weight_dict[item_weight_dict.size() - 1]
17+
18+
19+
## Use this to (re)calculate weight_sum only after dictionary is modified to optimize performance.
20+
static func get_weights_sum(item_weight_dict: Dictionary) -> int:
21+
return item_weight_dict.values().reduce(func(x: int, n: int) -> int: return x + n, 0)
22+
23+
24+
## Generate random string of given length and charset. (Default charset is ASCII.)
25+
func random_string(length: int, charset: String = CharsetConsts.ASCII) -> String:
26+
var result: String = ""
27+
for i: int in range(length):
28+
result += charset[randi() % charset.length()]
29+
return result

0 commit comments

Comments
 (0)