aboutsummaryrefslogtreecommitdiff
path: root/game/addons/zylann.hterrain/tools/util/result.gd
diff options
context:
space:
mode:
Diffstat (limited to 'game/addons/zylann.hterrain/tools/util/result.gd')
-rw-r--r--game/addons/zylann.hterrain/tools/util/result.gd36
1 files changed, 36 insertions, 0 deletions
diff --git a/game/addons/zylann.hterrain/tools/util/result.gd b/game/addons/zylann.hterrain/tools/util/result.gd
new file mode 100644
index 0000000..5e897f1
--- /dev/null
+++ b/game/addons/zylann.hterrain/tools/util/result.gd
@@ -0,0 +1,36 @@
+# Data structure to hold the result of a function that can be expected to fail.
+# The use case is to report errors back to the GUI and act accordingly,
+# instead of forgetting them to the console or having the script break on an assertion.
+# This is a C-like way of things, where the result can bubble, and does not require globals.
+
+@tool
+
+# Replace `success` with `error : int`?
+var success := false
+var value = null
+var message := ""
+var inner_result = null
+
+
+func _init(p_success: bool, p_message := "", p_inner = null):
+ success = p_success
+ message = p_message
+ inner_result = p_inner
+
+
+# TODO Can't type-hint self return
+func with_value(v):
+ value = v
+ return self
+
+
+func get_message() -> String:
+ var msg := message
+ if inner_result != null:
+ msg += "\n"
+ msg += inner_result.get_message()
+ return msg
+
+
+func is_ok() -> bool:
+ return success