1 extends DialogicSubsystem
3 ## Subsystem that allows executing strings (with the Expression class).
4 ## This is used by conditions and to allow expresions as variables.
8 ####################################################################################################
10 func execute_string(string:String, default: Variant = null, no_warning := false) -> Variant:
11 # Some methods are not supported by the expression class, but very useful.
12 # Thus they are recreated below and secretly added.
13 string = string.replace('range(', 'd_range(')
14 string = string.replace('len(', 'd_len(')
15 string = string.replace('regex(', 'd_regex(')
18 var regex: RegEx = RegEx.create_from_string('{([^{}]*)}')
20 for res in regex.search_all(string):
21 var value: Variant = dialogic.VAR.get_variable(res.get_string())
22 string = string.replace(res.get_string(), var_to_str(value))
24 if string.begins_with("{") and string.ends_with('}') and string.count("{") == 1:
25 string = string.trim_prefix("{").trim_suffix("}")
27 var expr := Expression.new()
30 var autoload_names := []
31 for c in get_tree().root.get_children():
33 autoload_names.append(c.name)
35 if expr.parse(string, autoload_names) != OK:
37 printerr('[Dialogic] Expression "', string, '" failed to parse.')
38 printerr(' ', expr.get_error_text())
39 dialogic.print_debug_moment()
42 var result: Variant = expr.execute(autoloads, self)
43 if expr.has_execute_failed():
45 printerr('[Dialogic] Expression "', string, '" failed to parse.')
46 printerr(' ', expr.get_error_text())
47 dialogic.print_debug_moment()
52 func execute_condition(condition:String) -> bool:
53 if execute_string(condition, false):
58 var condition_modifier_regex := RegEx.create_from_string(r"(?(DEFINE)(?<nobraces>([^{}]|\{(?P>nobraces)\})*))\[if *(?<condition>\{(?P>nobraces)\})(?<truetext>(\\\]|\\\/|[^\]\/])*)(\/(?<falsetext>(\\\]|[^\]])*))?\]")
59 func modifier_condition(text:String) -> String:
60 for find in condition_modifier_regex.search_all(text):
61 if execute_condition(find.get_string("condition")):
62 text = text.replace(find.get_string(), find.get_string("truetext").strip_edges())
64 text = text.replace(find.get_string(), find.get_string("falsetext").strip_edges())
70 ####################################################################################################
71 func d_range(a1, a2=null,a3=null,a4=null) -> Array:
77 return range(a1, a2, a3)
79 return range(a1, a2, a3, a4)
81 func d_len(arg:Variant) -> int:
85 # Checks if there is a match in a string based on a regex pattern string.
86 func d_regex(input: String, pattern: String, offset: int = 0, end: int = -1) -> bool:
87 var regex: RegEx = RegEx.create_from_string(pattern)
88 regex.compile(pattern)
89 var match := regex.search(input, offset, end)