diff --git a/addons/inkgd/runtime.gd b/addons/inkgd/runtime.gd index 865ff9d8..f6f2d243 100644 --- a/addons/inkgd/runtime.gd +++ b/addons/inkgd/runtime.gd @@ -15,8 +15,7 @@ static func init(root_node, should_pause_on_error = true): if root_node.has_node("__InkRuntime"): return root_node.get_node("__InkRuntime") - var InkRuntime = load("res://addons/inkgd/runtime/static/ink_runtime.gd") - var _ink_runtime = InkRuntime.new() + var _ink_runtime = load("res://addons/inkgd/runtime/static/ink_runtime.gd").new() _ink_runtime.should_pause_execution_on_exception = should_pause_on_error _ink_runtime.should_pause_execution_on_error = should_pause_on_error diff --git a/addons/inkgd/runtime/common/ink_object.gd b/addons/inkgd/runtime/common/ink_object.gd index 106a2557..633fb639 100644 --- a/addons/inkgd/runtime/common/ink_object.gd +++ b/addons/inkgd/runtime/common/ink_object.gd @@ -74,8 +74,8 @@ func debug_line_number_of_path(path: InkPath): return null # InkPath -var path: Path setget , get_path -func get_path() -> Path: +var path: InkPath setget , get_path +func get_path() -> InkPath: if _path == null: if self.parent == null: _path = InkPath().new() diff --git a/addons/inkgd/runtime/content/choices/choice.gd b/addons/inkgd/runtime/content/choices/choice.gd index 2c9a658e..f2095d7d 100644 --- a/addons/inkgd/runtime/content/choices/choice.gd +++ b/addons/inkgd/runtime/content/choices/choice.gd @@ -17,27 +17,33 @@ class_name InkChoice # Imports # ############################################################################ # -var CallStack = load("res://addons/inkgd/runtime/callstack.gd") +var CallStack := load("res://addons/inkgd/runtime/callstack.gd") as GDScript # ############################################################################ # -var text # String +var text: String -# () -> String -# (String) -> void -var path_string_on_choice setget set_path_string_on_choice, get_path_string_on_choice -func get_path_string_on_choice(): +var path_string_on_choice: String setget set_path_string_on_choice, get_path_string_on_choice +func get_path_string_on_choice() -> String: return target_path.to_string() -func set_path_string_on_choice(value): +func set_path_string_on_choice(value: String): target_path = InkPath().new_with_components_string(value) -var source_path = null # String -var index = 0 # index -var target_path = null # InkPath -var thread_at_generation = null # CallStack.InkThread -var original_thread_index = 0 # int -var is_invisible_default = false # bool +# String? +var source_path = null + +var index: int = 0 + +# InkPath? +var target_path = null + +# CallStack.InkThread? +var thread_at_generation = null + +var original_thread_index: int = 0 + +var is_invisible_default: bool = false # ############################################################################ # # GDScript extra methods diff --git a/addons/inkgd/runtime/content/choices/choice_point.gd b/addons/inkgd/runtime/content/choices/choice_point.gd index 258ee1d3..9b9e3a1a 100644 --- a/addons/inkgd/runtime/content/choices/choice_point.gd +++ b/addons/inkgd/runtime/content/choices/choice_point.gd @@ -18,52 +18,48 @@ class_name InkChoicePoint # () -> InkPath # (InkPath) -> void -var path_on_choice setget set_path_on_choice, get_path_on_choice -func get_path_on_choice(): +var path_on_choice: InkPath setget set_path_on_choice, get_path_on_choice +func get_path_on_choice() -> InkPath: if self._path_on_choice != null && self._path_on_choice.is_relative: var choice_target_obj = self.choice_target if choice_target_obj: self._path_on_choice = choice_target_obj.path return _path_on_choice -func set_path_on_choice(value): +func set_path_on_choice(value: InkPath): _path_on_choice = value -var _path_on_choice = null # InkPath +# InkPath? +var _path_on_choice = null # ############################################################################ # -# () -> InkContainer -# (InkContainer) -> void -var choice_target setget , get_choice_target -func get_choice_target(): +var choice_target: InkContainer setget , get_choice_target +func get_choice_target() -> InkContainer: var cont = resolve_path(self._path_on_choice).container return cont # ############################################################################ # -# () -> String -# (String) -> void -var path_string_on_choice setget set_path_string_on_choice, get_path_string_on_choice -func get_path_string_on_choice(): +var path_string_on_choice: String setget \ + set_path_string_on_choice, \ + get_path_string_on_choice +func get_path_string_on_choice() -> String: return compact_path_string(self.path_on_choice) -func set_path_string_on_choice(value): +func set_path_string_on_choice(value: String): self.path_on_choice = InkPath().new_with_components_string(value) # ############################################################################ # -var has_condition # bool -var has_start_content # bool -var has_choice_only_content # bool -var once_only # bool -var is_invisible_default # bool +var has_condition: bool +var has_start_content: bool +var has_choice_only_content: bool +var once_only: bool +var is_invisible_default: bool # ############################################################################ # -# () -> int -# (int) -> void -var flags setget set_flags, get_flags - +var flags: int setget set_flags, get_flags func get_flags() -> int: var flags: int = 0 @@ -79,8 +75,7 @@ func get_flags() -> int: flags |= 16 return flags - -func set_flags(value): +func set_flags(value: int): has_condition = (value & 1) > 0 has_start_content = (value & 2) > 0 has_choice_only_content = (value & 4) > 0 @@ -92,22 +87,21 @@ func set_flags(value): func _init(once_only: bool = true): self.once_only = once_only -# () -> String -func to_string(): +func to_string() -> String: var target_line_num = debug_line_number_of_path(self.path_on_choice) var target_string = self.path_on_choice.to_string() if target_line_num != null: - target_string = " line " + target_line_num + "(" + target_string + ")" + target_string = " line %d(%s)" % [target_line_num, target_string] - return "Choice: -> " + target_string + return "Choice: -> %s" % target_string # ############################################################################ # # GDScript extra methods # ############################################################################ # -func is_class(type): +func is_class(type: String) -> bool: return type == "ChoicePoint" || .is_class(type) -func get_class(): +func get_class() -> String: return "ChoicePoint" diff --git a/addons/inkgd/runtime/content/container.gd b/addons/inkgd/runtime/content/container.gd index dc174ade..a82b300a 100644 --- a/addons/inkgd/runtime/content/container.gd +++ b/addons/inkgd/runtime/content/container.gd @@ -17,23 +17,27 @@ class_name InkContainer # Imports # ############################################################################ # -var InkSearchResult = load("res://addons/inkgd/runtime/search_result.gd") +var InkSearchResult := load("res://addons/inkgd/runtime/search_result.gd") as GDScript # ############################################################################ # -var name = null # String +# String +var name = null -var content setget set_content, get_content # Array -func get_content(): +# Array +var content: Array setget set_content, get_content +func get_content() -> Array: return self._content -func set_content(value): +func set_content(value: Array): add_content(value) -var _content = null # Array +# Array +var _content: Array -var named_content = null # Dictionary +# Dictionary +var named_content: Dictionary -# Dictionary +# Dictionary? var named_only_content setget set_named_only_content, get_named_only_content func get_named_only_content(): var named_only_content_dict = {} # Dictionary @@ -64,9 +68,9 @@ func set_named_only_content(value): add_to_named_content_only(named) -var visits_should_be_counted = false # bool -var turn_index_should_be_counted = false # bool -var counting_at_start_only = false # bool +var visits_should_be_counted: bool = false +var turn_index_should_be_counted: bool = false +var counting_at_start_only: bool = false enum CountFlags { VISITS = 1, @@ -74,8 +78,9 @@ enum CountFlags { COUNT_START_ONLY = 4 } -var count_flags setget set_count_flags, get_count_flags # CountFlags -func get_count_flags(): +# CountFlags +var count_flags: int setget set_count_flags, get_count_flags +func get_count_flags() -> int: var flags = 0 if visits_should_be_counted: flags |= CountFlags.VISITS @@ -86,32 +91,30 @@ func get_count_flags(): flags = 0 return flags -func set_count_flags(value): +func set_count_flags(value: int): var flag = value if (flag & CountFlags.VISITS) > 0: visits_should_be_counted = true if (flag & CountFlags.TURNS) > 0: turn_index_should_be_counted = true if (flag & CountFlags.COUNT_START_ONLY) > 0: counting_at_start_only = true -# () -> bool -var has_valid_name setget , get_has_valid_name -func get_has_valid_name(): +var has_valid_name: bool setget , get_has_valid_name +func get_has_valid_name() -> bool: return self.name != null && self.name.length() > 0 -# () -> InkPath -var path_to_first_leaf_content setget , get_path_to_first_leaf_content -func get_path_to_first_leaf_content(): +var path_to_first_leaf_content: InkPath setget , get_path_to_first_leaf_content +func get_path_to_first_leaf_content() -> InkPath: if self._path_to_first_leaf_content == null: self._path_to_first_leaf_content = self.path.path_by_appending_path(self.internal_path_to_first_leaf_content) return self._path_to_first_leaf_content -var _path_to_first_leaf_content # Path +# InkPath? +var _path_to_first_leaf_content = null -# () -> InkPath -var internal_path_to_first_leaf_content setget , get_internal_path_to_first_leaf_content -func get_internal_path_to_first_leaf_content(): - var components = [] # Array - var container = self +var internal_path_to_first_leaf_content: InkPath setget , get_internal_path_to_first_leaf_content +func get_internal_path_to_first_leaf_content() -> InkPath: + var components: Array = [] # Array + var container: InkContainer = self while container != null: if container.content.size() > 0: components.append(InkPath().Component.new(0)) @@ -120,17 +123,16 @@ func get_internal_path_to_first_leaf_content(): return InkPath().new_with_components(components) func _init(): - self._content = [] # List + self._content = [] # Array self.named_content = {} # Dictionary -# (InkObject) -> void -func add_content(content_obj_or_content_list): +func add_content(content_obj_or_content_list) -> void: if Utils.is_ink_class(content_obj_or_content_list, "InkObject"): var content_obj = content_obj_or_content_list self.content.append(content_obj) if content_obj.parent: - Utils.throw_exception("content is already in " + content_obj.parent) + Utils.throw_exception("content is already in %s" % content_obj.parent.to_string()) return content_obj.parent = self @@ -141,42 +143,38 @@ func add_content(content_obj_or_content_list): for c in content_list: add_content(c) -# (InkObject, int) -> void -func insert_content(content_obj, index): +func insert_content(content_obj: InkObject, index: int) -> void: self.content.insert(index, content_obj) if content_obj.parent: - Utils.throw_exception("content is already in " + content_obj.parent) + Utils.throw_exception("content is already in %s" % content_obj.parent.to_string()) return content_obj.parent = self try_add_named_content(content_obj) -# (InkObject) -> void -func try_add_named_content(content_obj): +func try_add_named_content(content_obj: InkObject) -> void: var named_content_obj = Utils.as_INamedContent_or_null(content_obj) if (named_content_obj != null && named_content_obj.has_valid_name): add_to_named_content_only(named_content_obj) # (INamedContent) -> void -func add_to_named_content_only(named_content_obj): +func add_to_named_content_only(named_content_obj: InkObject) -> void: Utils.__assert__(named_content_obj.is_class("InkObject"), "Can only add Runtime.Objects to a Runtime.Container") var runtime_obj = named_content_obj runtime_obj.parent = self named_content[named_content_obj.name] = named_content_obj -# (InkContainer) -> void -func add_contents_of_container(other_container): +func add_contents_of_container(other_container: InkContainer) -> void: self.content = self.content + other_container.content for obj in other_container.content: obj.parent = self try_add_named_content(obj) -# (InkPath().Component) -> InkObject -func content_with_path_component(component): +func content_with_path_component(component: InkPath.Component) -> InkObject: if component.is_index: if component.index >= 0 && component.index < self.content.size(): return self.content[component.index] @@ -192,25 +190,29 @@ func content_with_path_component(component): return null # (InkPath, int, int) -> InkSearchResult -func content_at_path(path, partial_path_start = 0, partial_path_length = -1): +func content_at_path( + path: InkPath, + partial_path_start: int = 0, + partial_path_length: int = -1 +) -> InkSearchResult: if partial_path_length == -1: partial_path_length = path.length - var result = InkSearchResult.new() + var result: InkSearchResult = InkSearchResult.new() result.approximate = false - var current_container = self # Container - var current_obj = self # InkObject + var current_container: InkContainer = self + var current_obj: InkObject = self - var i = partial_path_start - while (i < partial_path_length): + var i: int = partial_path_start + while i < partial_path_length: var comp = path.get_component(i) if current_container == null: result.approximate = true break - var found_obj = current_container.content_with_path_component(comp) + var found_obj: InkObject = current_container.content_with_path_component(comp) if found_obj == null: result.approximate = true @@ -226,12 +228,16 @@ func content_at_path(path, partial_path_start = 0, partial_path_length = -1): return result # (String, int, InkObject) -> string -func build_string_of_hierarchy(existing_hierarchy, indentation, pointed_obj): +func build_string_of_hierarchy( + existing_hierarchy: String, + indentation: int, + pointed_obj: InkObject +) -> String: existing_hierarchy = _append_indentation(existing_hierarchy, indentation) existing_hierarchy += "[" if self.has_valid_name: - existing_hierarchy += str(" (", self.name, ") ") + existing_hierarchy += str(" (%s) " % self.name) if self == pointed_obj: existing_hierarchy += " <---" @@ -264,7 +270,7 @@ func build_string_of_hierarchy(existing_hierarchy, indentation, pointed_obj): existing_hierarchy += "\n" i += 1 - var only_named = {} # Dictionary + var only_named: Dictionary = {} # Dictionary for obj_key in self.named_content: var value = self.named_content[obj_key] @@ -291,10 +297,10 @@ func build_string_of_hierarchy(existing_hierarchy, indentation, pointed_obj): return existing_hierarchy -func build_full_string_of_hierarchy(): +func build_full_string_of_hierarchy() -> String: return build_string_of_hierarchy("", 0, null) -func _append_indentation(string, indentation): +func _append_indentation(string: String, indentation: int) -> String: var spaces_per_indent = 4 var i = 0 while(i < spaces_per_indent * indentation): @@ -308,8 +314,8 @@ func _append_indentation(string, indentation): # GDScript extra methods # ############################################################################ # -func is_class(type): +func is_class(type: String) -> bool: return type == "InkContainer" || .is_class(type) -func get_class(): +func get_class() -> String: return "InkContainer" diff --git a/addons/inkgd/runtime/content/control_command.gd b/addons/inkgd/runtime/content/control_command.gd index b2112b55..2ca150b2 100644 --- a/addons/inkgd/runtime/content/control_command.gd +++ b/addons/inkgd/runtime/content/control_command.gd @@ -17,8 +17,8 @@ class_name InkControlCommand # Imports # ############################################################################ # -static func ControlCommand(): - return load("res://addons/inkgd/runtime/control_command.gd") +static func ControlCommand() -> GDScript: + return load("res://addons/inkgd/runtime/control_command.gd") as GDScript # ############################################################################ # @@ -54,115 +54,91 @@ enum CommandType { # ############################################################################ # -var command_type # CommandType +# CommandType +var command_type: int # ############################################################################ # -# (CommandType) -> InkControlCommand -func _init(command_type = CommandType.NOT_SET): +func _init(command_type: int = CommandType.NOT_SET): self.command_type = command_type # ############################################################################ # -# () -> ControlCommand -func copy(): +func copy() -> InkControlCommand: return ControlCommand().new(self.command_type) -# () -> ControlCommand -static func eval_start(): +static func eval_start() -> InkControlCommand: return ControlCommand().new(CommandType.EVAL_START) -# () -> ControlCommand -static func eval_output(): +static func eval_output() -> InkControlCommand: return ControlCommand().new(CommandType.EVAL_OUTPUT) -# () -> ControlCommand -static func eval_end(): +static func eval_end() -> InkControlCommand: return ControlCommand().new(CommandType.EVAL_END) -# () -> ControlCommand -static func duplicate(): +static func duplicate() -> InkControlCommand: return ControlCommand().new(CommandType.DUPLICATE) -# () -> ControlCommand -static func pop_evaluated_value(): +static func pop_evaluated_value() -> InkControlCommand: return ControlCommand().new(CommandType.POP_EVALUATED_VALUE) -# () -> ControlCommand -static func pop_function(): +static func pop_function() -> InkControlCommand: return ControlCommand().new(CommandType.POP_FUNCTION) -# () -> ControlCommand -static func pop_tunnel(): +static func pop_tunnel() -> InkControlCommand: return ControlCommand().new(CommandType.POP_TUNNEL) -# () -> ControlCommand -static func begin_string(): +static func begin_string() -> InkControlCommand: return ControlCommand().new(CommandType.BEGIN_STRING) -# () -> ControlCommand -static func end_string(): +static func end_string() -> InkControlCommand: return ControlCommand().new(CommandType.END_STRING) -# () -> ControlCommand -static func no_op(): +static func no_op() -> InkControlCommand: return ControlCommand().new(CommandType.NO_OP) -# () -> ControlCommand -static func choice_count(): +static func choice_count() -> InkControlCommand: return ControlCommand().new(CommandType.CHOICE_COUNT) -# () -> ControlCommand -static func turns(): +static func turns() -> InkControlCommand: return ControlCommand().new(CommandType.TURNS) -# () -> ControlCommand -static func turns_since(): +static func turns_since() -> InkControlCommand: return ControlCommand().new(CommandType.TURNS_SINCE) -# () -> ControlCommand -static func read_count(): +static func read_count() -> InkControlCommand: return ControlCommand().new(CommandType.READ_COUNT) -# () -> ControlCommand -static func random(): +static func random() -> InkControlCommand: return ControlCommand().new(CommandType.RANDOM) -# () -> ControlCommand -static func seed_random(): +static func seed_random() -> InkControlCommand: return ControlCommand().new(CommandType.SEED_RANDOM) -# () -> ControlCommand -static func visit_index(): +static func visit_index() -> InkControlCommand: return ControlCommand().new(CommandType.VISIT_INDEX) -# () -> ControlCommand -static func sequence_shuffle_index(): +static func sequence_shuffle_index() -> InkControlCommand: return ControlCommand().new(CommandType.SEQUENCE_SHUFFLE_INDEX) -# () -> ControlCommand -static func done(): +static func done() -> InkControlCommand: return ControlCommand().new(CommandType.DONE) -# () -> ControlCommand -static func end(): +static func end() -> InkControlCommand: return ControlCommand().new(CommandType.END) -# () -> ControlCommand -static func list_from_int(): +static func list_from_int() -> InkControlCommand: return ControlCommand().new(CommandType.LIST_FROM_INT) -# () -> ControlCommand -static func list_range(): +static func list_range() -> InkControlCommand: return ControlCommand().new(CommandType.LIST_RANGE) -# () -> ControlCommand -static func list_random(): +static func list_random() -> InkControlCommand: return ControlCommand().new(CommandType.LIST_RANDOM) # () -> String func to_string(): - var command_name = "" + var command_name: String = "" match self.command_type: CommandType.NOT_SET: command_name = "NOT_SET" CommandType.EVAL_START: command_name = "EVAL_START" @@ -191,14 +167,14 @@ func to_string(): CommandType.LIST_RANDOM: command_name = "LIST_RANDOM" CommandType.TOTAL_VALUES: command_name = "TOTAL_VALUES" - return str("Command(", command_name, ")") + return "Command(%s)" % command_name # ############################################################################ # # GDScript extra methods # ############################################################################ # -func is_class(type): +func is_class(type: String) -> bool: return type == "ControlCommand" || .is_class(type) -func get_class(): +func get_class() -> String: return "ControlCommand" diff --git a/addons/inkgd/runtime/content/divert.gd b/addons/inkgd/runtime/content/divert.gd index af498bbf..829dfbb5 100644 --- a/addons/inkgd/runtime/content/divert.gd +++ b/addons/inkgd/runtime/content/divert.gd @@ -18,28 +18,29 @@ class_name InkDivert # Imports # ############################################################################ # -var PushPopType = preload("res://addons/inkgd/runtime/enums/push_pop.gd").PushPopType -var Pointer = load("res://addons/inkgd/runtime/structs/pointer.gd") +const PushPopType = preload("res://addons/inkgd/runtime/enums/push_pop.gd").PushPopType +var InkPointer := load("res://addons/inkgd/runtime/structs/pointer.gd") as GDScript # ############################################################################ # -var target_path setget set_target_path, get_target_path # InkPath -func get_target_path(): +var target_path: InkPath setget set_target_path, get_target_path +func get_target_path() -> InkPath: if self._target_path != null && self._target_path.is_relative: - var target_obj = self.target_pointer.resolve() + var target_obj: InkObject = self.target_pointer.resolve() if target_obj: self._target_path = target_obj.path return self._target_path -func set_target_path(value): +func set_target_path(value: InkPath): self._target_path = value - self._target_pointer = Pointer.null() + self._target_pointer = InkPointer.null() -var _target_path = null # InkPath +# InkPath +var _target_path = null -var target_pointer setget , get_target_pointer # InkPointer -func get_target_pointer(): +var target_pointer: InkPointer setget , get_target_pointer # InkPointer +func get_target_pointer() -> InkPointer: if self._target_pointer.is_null: var target_obj = resolve_path(self._target_path).obj @@ -47,13 +48,14 @@ func get_target_pointer(): self._target_pointer.container = Utils.as_or_null(target_obj.parent, "InkContainer") self._target_pointer.index = self._target_path.last_component.index else: - self._target_pointer = Pointer.start_of(Utils.as_or_null(target_obj, "InkContainer")) + self._target_pointer = InkPointer.start_of(Utils.as_or_null(target_obj, "InkContainer")) return self._target_pointer.duplicate() -var _target_pointer = Pointer.null() # InkPointer +var _target_pointer: InkPointer = InkPointer.null() -var target_path_string setget set_target_path_string, get_target_path_string # String +# String? +var target_path_string setget set_target_path_string, get_target_path_string func get_target_path_string(): if self.target_path == null: return null @@ -66,20 +68,24 @@ func set_target_path_string(value): else: self.target_path = InkPath().new_with_components_string(value) -var variable_divert_name = null # String -var has_variable_target setget , get_has_variable_target # bool -func get_has_variable_target(): return self.variable_divert_name != null +# String +var variable_divert_name = null +var has_variable_target: bool setget , get_has_variable_target +func get_has_variable_target() -> bool: + return self.variable_divert_name != null -var pushes_to_stack = false # bool -var stack_push_type = 0 # PushPopType +var pushes_to_stack: bool = false -var is_external = false # bool -var external_args = 0 # int +# PushPopType +var stack_push_type: int = 0 -var is_conditional = false # bool +var is_external: bool = false +var external_args: int = 0 +var is_conditional: bool = false -# (int) -> InkDivert + +# (int?) -> InkDivert func _init_with(stack_push_type = null): self.pushes_to_stack = false @@ -88,8 +94,8 @@ func _init_with(stack_push_type = null): self.stack_push_type = stack_push_type # (InkBase) -> bool -func equals(obj): - var other_divert = Utils.as_or_null(obj, "Divert") +func equals(obj) -> bool: + var other_divert: InkDivert = Utils.as_or_null(obj, "Divert") if other_divert: if self.has_variable_target == other_divert.has_variable_target: if self.has_variable_target: @@ -99,15 +105,15 @@ func equals(obj): return false -func to_string(): +func to_string() -> String: if self.has_variable_target: - return "Divert(variable: " + self.variable_divert_name + ")" + return "Divert(variable: %s)" % self.variable_divert_name elif self.target_path == null: return "Divert(null)" else: var _string = "" - var target_str = self.target_path.to_string() + var target_str: String = self.target_path.to_string() var target_line_num = debug_line_number_of_path(self.target_path) if target_line_num != null: target_str = "line " + target_line_num @@ -126,9 +132,7 @@ func to_string(): _string += " -> " _string += self.target_path_string - _string += " (" - _string += target_str - _string += ")" + _string += " (%s)" % target_str return _string @@ -136,8 +140,8 @@ func to_string(): # GDScript extra methods # ############################################################################ # -func is_class(type): +func is_class(type: String) -> bool: return type == "Divert" || .is_class(type) -func get_class(): +func get_class() -> String: return "Divert" diff --git a/addons/inkgd/runtime/content/native_function_call.gd b/addons/inkgd/runtime/content/native_function_call.gd index 0bcecf9f..227d6e1b 100644 --- a/addons/inkgd/runtime/content/native_function_call.gd +++ b/addons/inkgd/runtime/content/native_function_call.gd @@ -19,47 +19,48 @@ class_name InkNativeFunctionCall # ############################################################################ # const ValueType = preload("res://addons/inkgd/runtime/values/value_type.gd").ValueType -var InkList = load("res://addons/inkgd/runtime/lists/ink_list.gd") +var InkList := load("res://addons/inkgd/runtime/lists/ink_list.gd") as GDScript -var Value = load("res://addons/inkgd/runtime/values/value.gd") -var BoolValue = load("res://addons/inkgd/runtime/values/bool_value.gd") -var ListValue = load("res://addons/inkgd/runtime/values/list_value.gd") +var InkValue := load("res://addons/inkgd/runtime/values/value.gd") as GDScript +var InkBoolValue := load("res://addons/inkgd/runtime/values/bool_value.gd") as GDScript +var InkListValue := load("res://addons/inkgd/runtime/values/list_value.gd") as GDScript -static func NativeFunctionCall(): - return load("res://addons/inkgd/runtime/content/native_function_call.gd") +static func InkNativeFunctionCall() -> GDScript: + return load("res://addons/inkgd/runtime/content/native_function_call.gd") as GDScript # ############################################################################ # # (String) -> NativeFunctionCall -static func call_with_name(function_name): - return NativeFunctionCall().new_with_name(function_name) +static func call_with_name(function_name) -> InkNativeFunctionCall: + return InkNativeFunctionCall().new_with_name(function_name) -var name setget set_name, get_name # String -func get_name(): +var name: String setget set_name, get_name +func get_name() -> String: return _name -func set_name(value): +func set_name(value: String): _name = value if !_is_prototype: - _prototype = self.StaticNativeFunctionCall.native_functions[_name] + _prototype = self._static_native_function_call.native_functions[_name] var _name -var number_of_parameters setget set_number_of_parameters, get_number_of_parameters # int -func get_number_of_parameters(): +var number_of_parameters: int setget set_number_of_parameters, get_number_of_parameters +func get_number_of_parameters() -> int: if _prototype: return _prototype.number_of_parameters else: return _number_of_parameters -func set_number_of_parameters(value): +func set_number_of_parameters(value: int): _number_of_parameters = value -var _number_of_parameters # int +var _number_of_parameters = 0 # (Array) -> InkObject -func call(parameters): +# The name is different to avoid shadowing 'Object.call'. +func call_with_parameters(parameters: Array) -> InkObject: if _prototype: - return _prototype.call(parameters) + return _prototype.call_with_parameters(parameters) if self.number_of_parameters != parameters.size(): Utils.throw_exception("Unexpected number of parameters") @@ -68,10 +69,10 @@ func call(parameters): var has_list = false for p in parameters: if Utils.is_ink_class(p, "Void"): - Utils.throw_exception(str( - "Attempting to perform operation on a void value. Did you forget to ", - "'return' a value from a function you called here?" - )) + Utils.throw_exception( + "Attempting to perform operation on a void value. Did you forget to " + + "'return' a value from a function you called here?" + ) return null if Utils.is_ink_class(p, "ListValue"): has_list = true @@ -79,43 +80,51 @@ func call(parameters): if parameters.size() == 2 && has_list: return call_binary_list_operation(parameters) - var coerced_params = coerce_values_to_single_type(parameters) - var coerced_type = coerced_params[0].value_type # ValueType + var coerced_params: Array = coerce_values_to_single_type(parameters) - if (coerced_type == ValueType.INT || - coerced_type == ValueType.FLOAT || - coerced_type == ValueType.STRING || - coerced_type == ValueType.DIVERT_TARGET || - coerced_type == ValueType.LIST): + # ValueType + var coerced_type: int = coerced_params[0].value_type + + if ( + coerced_type == ValueType.INT || + coerced_type == ValueType.FLOAT || + coerced_type == ValueType.STRING || + coerced_type == ValueType.DIVERT_TARGET || + coerced_type == ValueType.LIST + ): return call_coerced(coerced_params) return null # (Array) -> Value # Call in the original code -func call_coerced(parameters_of_single_type): - var param1 = parameters_of_single_type[0] - var val_type = param1.value_type +func call_coerced(parameters_of_single_type: Array) -> InkValue: + var param1: InkValue = parameters_of_single_type[0] + var val_type: int = param1.value_type - var param_count = parameters_of_single_type.size() + var param_count: int = parameters_of_single_type.size() if param_count == 2 || param_count == 1: var op_for_type = null if _operation_funcs.has(val_type): op_for_type = _operation_funcs[val_type] else: - Utils.throw_story_exception(str("Cannot perform operation '", self.name, "' on ", val_type)) + var type_name = Utils.value_type_name(val_type) + Utils.throw_story_exception( + "Cannot perform operation '%s' on value of type (%d)" \ + % [self.name, type_name] + ) return null if param_count == 2: var param2 = parameters_of_single_type[1] - var result_val = self.StaticNativeFunctionCall.call(op_for_type, param1.value, param2.value) + var result_val = self._static_native_function_call.call(op_for_type, param1.value, param2.value) - return Value.create(result_val) + return InkValue.create(result_val) else: - var result_val = self.StaticNativeFunctionCall.call(op_for_type, param1.value) + var result_val = self._static_native_function_call.call(op_for_type, param1.value) - return Value.create(result_val) + return InkValue.create(result_val) else: Utils.throw_exception(str( "Unexpected number of parameters to NativeFunctionCall: ", @@ -124,7 +133,7 @@ func call_coerced(parameters_of_single_type): return null # (Array) -> Value -func call_binary_list_operation(parameters): +func call_binary_list_operation(parameters: Array) -> InkValue: if ((self.name == "+" || self.name == "-") && Utils.is_ink_class(parameters[0], "ListValue") && Utils.is_ink_class(parameters [1], "IntValue") @@ -137,14 +146,14 @@ func call_binary_list_operation(parameters): if ((self.name == "&&" || self.name == "||") && (v1.value_type != ValueType.LIST || v2.value_type != ValueType.LIST) ): - var op = _operation_funcs[ValueType.INT] - var result = bool(self.StaticNativeFunctionCall.call( + var op: String = _operation_funcs[ValueType.INT] + var result = bool(self._static_native_function_call.call( "op_for_type", 1 if v1.is_truthy else 0, 1 if v2.is_truthy else 0 )) - return BoolValue.new_with(result) + return InkBoolValue.new_with(result) if v1.value_type == ValueType.LIST && v2.value_type == ValueType.LIST: return call_coerced([v1, v2]) @@ -157,20 +166,26 @@ func call_binary_list_operation(parameters): return null # (Array) -> Value -func call_list_increment_operation(list_int_params): - var list_val = Utils.cast(list_int_params[0], "ListValue") - var int_val = Utils.cast(list_int_params [1], "IntValue") +func call_list_increment_operation(list_int_params: Array) -> InkValue: + var list_val: InkListValue = Utils.cast(list_int_params[0], "ListValue") + var int_val: InkIntValue = Utils.cast(list_int_params [1], "IntValue") var result_raw_list = InkList.new() for list_item in list_val.value.keys(): # TODO: Optimize? var list_item_value = list_val.value.get_item(list_item) - var int_op = _operation_funcs[ValueType.INT] + var int_op: String = _operation_funcs[ValueType.INT] - var target_int = int(self.StaticNativeFunctionCall.call(int_op, list_item_value, int_val.value)) + var target_int = int( + self._static_native_function_call.call( + int_op, + list_item_value, + int_val.value + ) + ) - var item_origin = null # ListDefinition + var item_origin: InkListDefinition = null for origin in list_val.value.origins: if origin.name == list_item.origin_name: item_origin = origin @@ -181,23 +196,23 @@ func call_list_increment_operation(list_int_params): if incremented_item.exists: result_raw_list.set_item(incremented_item.result, target_int) - return ListValue.new_with(result_raw_list) + return InkListValue.new_with(result_raw_list) -# (Array) -> Array -func coerce_values_to_single_type(parameters_in): +# (Array) -> Array? +func coerce_values_to_single_type(parameters_in: Array): var val_type = ValueType.INT - var special_case_list = null # ListValue + var special_case_list: InkListValue = null # ListValue for obj in parameters_in: - var val = obj # Value + var val: InkValue = obj if val.value_type > val_type: val_type = val.value_type if val.value_type == ValueType.LIST: special_case_list = Utils.as_or_null(val, "ListValue") - var parameters_out = [] # Array + var parameters_out: Array = [] # Array if val_type == ValueType.LIST: for val in parameters_in: @@ -209,18 +224,20 @@ func coerce_values_to_single_type(parameters_in): var item = list.try_get_item_with_value(int_val) if item.exists: - var casted_value = ListValue.new_with_single_item(item.result, int_val) + var casted_value = InkListValue.new_with_single_item(item.result, int_val) parameters_out.append(casted_value) else: - Utils.throw_exception(str( - "Could not find List item with the value ", int_val, " in ", list.name - )) + Utils.throw_exception( + "Could not find List item with the value %d in %s" \ + % [int_val, list.name] + ) return null else: - Utils.throw_exception(str( - "Cannot mix Lists and ", val.value_type, " values in this operation" - )) + var type_name = Utils.value_type_name(val.value_type) + Utils.throw_exception( + "Cannot mix Lists and %s values in this operation" % type_name + ) return null @@ -234,33 +251,36 @@ func coerce_values_to_single_type(parameters_in): func _init(): generate_native_functions_if_necessary() -func _init_with_name(name): +func _init_with_name(name: String): generate_native_functions_if_necessary() self.name = name -func _init_with_name_and_number_of_parameters(name, number_of_parameters): +func _init_with_name_and_number_of_parameters(name: String, number_of_parameters: int): _is_prototype = true self.name = name self.number_of_parameters = number_of_parameters -func generate_native_functions_if_necessary(): - get_reference_to_functions_singleton_if_necessary() - self.StaticNativeFunctionCall.generate_native_functions_if_necessary() +func generate_native_functions_if_necessary() -> void: + find_static_objects() + self._static_native_function_call.generate_native_functions_if_necessary() # (ValueType, String) -> void -func add_op_func_for_type(val_type, op): +func add_op_func_for_type(val_type: int, op: String) -> void: if _operation_funcs == null: _operation_funcs = {} _operation_funcs[val_type] = op -func to_string(): - return str("Native '", self.name, "'") +func to_string() -> String: + return "Native '%s'" % self.name + +# NativeFunctionCall +var _prototype = null -var _prototype = null # NativeFunctionCall -var _is_prototype = false # bool +var _is_prototype: bool = false -var _operation_funcs = null # Dictionary +# Dictionary +var _operation_funcs: Dictionary = {} # ############################################################################ # # GDScript extra methods @@ -272,23 +292,25 @@ func is_class(type): func get_class(): return "NativeFunctionCall" -var StaticNativeFunctionCall setget, get_StaticNativeFunctionCall -func get_StaticNativeFunctionCall(): - return _StaticNativeFunctionCall.get_ref() -var _StaticNativeFunctionCall = WeakRef.new() +var _static_native_function_call: InkStaticNativeFunctionCall setget \ + , get_static_native_function_call +func get_static_native_function_call(): + return _weak_static_native_function_call.get_ref() +var _weak_static_native_function_call = WeakRef.new() -func get_reference_to_functions_singleton_if_necessary(): - # TODO: refactor - if _StaticNativeFunctionCall.get_ref() == null: - var scene_tree = Engine.get_main_loop() - _StaticNativeFunctionCall = weakref(scene_tree.root.get_node("__InkRuntime").native_function_call) +func find_static_objects(): + if _static_native_function_call == null: + var ink_runtime = Engine.get_main_loop().root.get_node("__InkRuntime") + _weak_static_native_function_call = weakref(ink_runtime.native_function_call) + +# ############################################################################ # -static func new_with_name(name): - var native_function_call = NativeFunctionCall().new() +static func new_with_name(name: String): + var native_function_call = InkNativeFunctionCall().new() native_function_call._init_with_name(name) return native_function_call -static func new_with_name_and_number_of_parameters(name, number_of_parameters): - var native_function_call = NativeFunctionCall().new() +static func new_with_name_and_number_of_parameters(name: String, number_of_parameters: int): + var native_function_call = InkNativeFunctionCall().new() native_function_call._init_with_name_and_number_of_parameters(name, number_of_parameters) return native_function_call diff --git a/addons/inkgd/runtime/content/tag.gd b/addons/inkgd/runtime/content/tag.gd index 7c29b4b5..3c1f4e82 100644 --- a/addons/inkgd/runtime/content/tag.gd +++ b/addons/inkgd/runtime/content/tag.gd @@ -8,26 +8,24 @@ # ############################################################################ # tool -extends "res://addons/inkgd/runtime/common/ink_object.gd" +extends InkObject class_name InkTag -var text # String +var text: String -# (String) -> Tag -func _init(tag_text): +func _init(tag_text: String): text = tag_text -# () -> String -func to_string(): +func to_string() -> String: return '# ' + text # ############################################################################ # # GDScript extra methods # ############################################################################ # -func is_class(type): +func is_class(type: String) -> bool: return type == "Tag" || .is_class(type) -func get_class(): +func get_class() -> String: return "Tag" diff --git a/addons/inkgd/runtime/content/variable_assignment.gd b/addons/inkgd/runtime/content/variable_assignment.gd index b86c624d..20991191 100644 --- a/addons/inkgd/runtime/content/variable_assignment.gd +++ b/addons/inkgd/runtime/content/variable_assignment.gd @@ -18,36 +18,42 @@ class_name InkVariableAssignment # Self-reference # ############################################################################ # -static func VariableAssignment(): - return load("res://addons/inkgd/runtime/content/variable_assignment.gd") +static func InkVariableAssignment() -> GDScript: + return load("res://addons/inkgd/runtime/content/variable_assignment.gd") as GDScript # ############################################################################ # -var variable_name = null # String -var is_new_declaration = false # bool -var is_global = false # bool +# String +var variable_name = null +var is_new_declaration: bool = false +var is_global: bool = false func _init(): _init_with(null, false) -func _init_with(variable_name, is_new_declaration): +# (String?, bool) -> InkVariableAssignment +func _init_with(variable_name, is_new_declaration: bool): self.variable_name = variable_name self.is_new_declaration = is_new_declaration -func to_string(): +func to_string() -> String: return "VarAssign to %s" % variable_name # ############################################################################ # # GDScript extra methods # ############################################################################ # -func is_class(type): +func is_class(type: String) -> bool: return type == "VariableAssignment" || .is_class(type) -func get_class(): +func get_class() -> String: return "VariableAssignment" -static func new_with(variable_name, is_new_declaration): - var variable_assignment = VariableAssignment().new() +# (String?, bool) -> InkVariableAssignment +static func new_with( + variable_name: String, + is_new_declaration: bool +) -> InkVariableAssignment: + var variable_assignment = InkVariableAssignment().new() variable_assignment._init_with(variable_name, is_new_declaration) return variable_assignment diff --git a/addons/inkgd/runtime/extra/utils.gd b/addons/inkgd/runtime/extra/utils.gd index 3749dd7b..550deab0 100644 --- a/addons/inkgd/runtime/extra/utils.gd +++ b/addons/inkgd/runtime/extra/utils.gd @@ -12,6 +12,12 @@ extends Reference class_name InkUtils +# ############################################################################ # +# Imports +# ############################################################################ # + +const ValueType = preload("res://addons/inkgd/runtime/values/value_type.gd").ValueType + # ############################################################################ # # Exceptions # ############################################################################ # @@ -93,6 +99,20 @@ static func are_of_same_type(object1: Object, object2: Object) -> bool: return typeof(object1) == typeof(object2) +static func value_type_name(value_type: int) -> String: + match value_type: + ValueType.BOOL: return "Boolean" + + ValueType.INT: return "Int" + ValueType.FLOAT: return "Float" + ValueType.LIST: return "List" + ValueType.STRING: return "String" + + ValueType.DIVERT_TARGET: return "Divert Target" + ValueType.VARIABLE_POINTER: return "Variable Pointer" + + _: return "unknown" + static func typename_of(variant) -> String: match typeof(variant): TYPE_NIL: return "null" @@ -248,5 +268,5 @@ static func array_equal(a1: Array, a2: Array, use_equals = false) -> bool: # ############################################################################ # -static func InkRuntime() -> InkRuntimeNode: - return Engine.get_main_loop().root.get_node("__InkRuntime") as InkRuntimeNode +static func InkRuntime(): + return Engine.get_main_loop().root.get_node("__InkRuntime") diff --git a/addons/inkgd/runtime/lists/list_definition.gd b/addons/inkgd/runtime/lists/list_definition.gd index 9af15406..e75cab5e 100644 --- a/addons/inkgd/runtime/lists/list_definition.gd +++ b/addons/inkgd/runtime/lists/list_definition.gd @@ -10,7 +10,7 @@ # ############################################################################ # tool -extends "res://addons/inkgd/runtime/common/ink_object.gd" +extends InkObject class_name InkListDefinition diff --git a/addons/inkgd/runtime/lists/list_definitions_origin.gd b/addons/inkgd/runtime/lists/list_definitions_origin.gd index 7bcf9644..56e8ae57 100644 --- a/addons/inkgd/runtime/lists/list_definitions_origin.gd +++ b/addons/inkgd/runtime/lists/list_definitions_origin.gd @@ -10,7 +10,7 @@ # ############################################################################ # tool -extends "res://addons/inkgd/runtime/common/ink_object.gd" +extends InkObject class_name InkListDefinitionsOrigin diff --git a/addons/inkgd/runtime/static/ink_runtime.gd b/addons/inkgd/runtime/static/ink_runtime.gd index 7b074dbe..02c6d28c 100644 --- a/addons/inkgd/runtime/static/ink_runtime.gd +++ b/addons/inkgd/runtime/static/ink_runtime.gd @@ -20,8 +20,8 @@ class_name InkRuntimeNode # Imports # ############################################################################ # -var StaticJson = load("res://addons/inkgd/runtime/static/json.gd") -var StaticNativeFunctionCall = load("res://addons/inkgd/runtime/static/native_function_call.gd") +var InkStaticJSON := load("res://addons/inkgd/runtime/static/json.gd") as GDScript +var InkStaticNativeFunctionCall := load("res://addons/inkgd/runtime/static/native_function_call.gd") as GDScript # ############################################################################ # # Signals @@ -46,20 +46,32 @@ var should_pause_execution_on_error: bool = true # ############################################################################ # -var should_pause_execution_on_runtime_error setget set_speore, get_speore -func get_speore(): - printerr("'should_pause_execution_on_runtime_error' is deprecated, use 'should_pause_execution_on_exception' instead.") +var should_pause_execution_on_runtime_error: bool setget set_speore, get_speore +func get_speore() -> bool: + printerr( + "'should_pause_execution_on_runtime_error' is deprecated, " + + "use 'should_pause_execution_on_exception' instead." + ) return should_pause_execution_on_exception -func set_speore(value): - printerr("'should_pause_execution_on_runtime_error' is deprecated, use 'should_pause_execution_on_exception' instead.") +func set_speore(value: bool): + printerr( + "'should_pause_execution_on_runtime_error' is deprecated, " + + "use 'should_pause_execution_on_exception' instead." + ) should_pause_execution_on_exception = value -var should_pause_execution_on_story_error setget set_speose, get_speose -func get_speose(): - printerr("'should_pause_execution_on_story_error' is deprecated, use 'should_pause_execution_on_error' instead.") +var should_pause_execution_on_story_error: bool setget set_speose, get_speose +func get_speose() -> bool: + printerr( + "'should_pause_execution_on_story_error' is deprecated, " + + "use 'should_pause_execution_on_error' instead." + ) return should_pause_execution_on_error -func set_speose(value): - printerr("'should_pause_execution_on_story_error' is deprecated, use 'should_pause_execution_on_error' instead.") +func set_speose(value: bool): + printerr( + "'should_pause_execution_on_story_error' is deprecated, " + + "use 'should_pause_execution_on_error' instead." + ) should_pause_execution_on_error = value # ############################################################################ # @@ -70,8 +82,8 @@ func set_speose(value): # declared in Ink. var dont_save_default_values: bool = true -var native_function_call = StaticNativeFunctionCall.new() -var json = StaticJson.new(native_function_call) +var native_function_call: InkStaticNativeFunctionCall = InkStaticNativeFunctionCall.new() +var json: InkStaticJSON = InkStaticJSON.new(native_function_call) # ############################################################################ # # Internal Properties diff --git a/addons/inkgd/runtime/static/json.gd b/addons/inkgd/runtime/static/json.gd index 085be945..2340c340 100644 --- a/addons/inkgd/runtime/static/json.gd +++ b/addons/inkgd/runtime/static/json.gd @@ -8,7 +8,7 @@ # ############################################################################ # tool -extends Reference +extends InkBase class_name InkStaticJSON @@ -20,35 +20,36 @@ class_name InkStaticJSON # ############################################################################ # var PushPopType = preload("res://addons/inkgd/runtime/enums/push_pop.gd").PushPopType -var Utils = preload("res://addons/inkgd/runtime/extra/utils.gd") - var InkListItem = preload("res://addons/inkgd/runtime/lists/structs/ink_list_item.gd") # ############################################################################ # var InkNativeFunctionCall = load("res://addons/inkgd/runtime/content/native_function_call.gd") -var Value = load("res://addons/inkgd/runtime/values/value.gd") -var StringValue = load("res://addons/inkgd/runtime/values/string_value.gd") -var DivertTargetValue = load("res://addons/inkgd/runtime/values/divert_target_value.gd") -var VariablePointerValue = load("res://addons/inkgd/runtime/values/variable_pointer_value.gd") -var ListValue = load("res://addons/inkgd/runtime/values/list_value.gd") - -var Glue = load("res://addons/inkgd/runtime/content/glue.gd") -var ControlCommand = load("res://addons/inkgd/runtime/content/control_command.gd") -var Divert = load("res://addons/inkgd/runtime/content/divert.gd") -var ChoicePoint = load("res://addons/inkgd/runtime/content/choices/choice_point.gd") -var VariableReference = load("res://addons/inkgd/runtime/content/variable_reference.gd") -var VariableAssignment = load("res://addons/inkgd/runtime/content/variable_assignment.gd") -var Tag = load("res://addons/inkgd/runtime/content/tag.gd") -var ListDefinition = load("res://addons/inkgd/runtime/lists/list_definition.gd") -var ListDefinitionsOrigin = load("res://addons/inkgd/runtime/lists/list_definitions_origin.gd") +var InkValue = load("res://addons/inkgd/runtime/values/value.gd") +var InkStringValue = load("res://addons/inkgd/runtime/values/string_value.gd") +var InkDivertTargetValue = load("res://addons/inkgd/runtime/values/divert_target_value.gd") +var InkVariablePointerValue = load("res://addons/inkgd/runtime/values/variable_pointer_value.gd") +var InkListValue = load("res://addons/inkgd/runtime/values/list_value.gd") + +var InkControlCommand = load("res://addons/inkgd/runtime/content/control_command.gd") +var InkGlue = load("res://addons/inkgd/runtime/content/glue.gd") +var InkVoid = load("res://addons/inkgd/runtime/content/void.gd") -var InkList = load("res://addons/inkgd/runtime/lists/ink_list.gd") -var Void = load("res://addons/inkgd/runtime/content/void.gd") -var InkContainer = load("res://addons/inkgd/runtime/content/container.gd") -var Choice = load("res://addons/inkgd/runtime/content/choices/choice.gd") var InkPath = load("res://addons/inkgd/runtime/ink_path.gd") +var InkDivert = load("res://addons/inkgd/runtime/content/divert.gd") +var InkTag = load("res://addons/inkgd/runtime/content/tag.gd") + +var InkContainer = load("res://addons/inkgd/runtime/content/container.gd") +var InkChoice = load("res://addons/inkgd/runtime/content/choices/choice.gd") +var InkChoicePoint = load("res://addons/inkgd/runtime/content/choices/choice_point.gd") + +var InkList = load("res://addons/inkgd/runtime/lists/ink_list.gd") +var InkListDefinition = load("res://addons/inkgd/runtime/lists/list_definition.gd") +var InkListDefinitionsOrigin = load("res://addons/inkgd/runtime/lists/list_definitions_origin.gd") + +var InkVariableReference = load("res://addons/inkgd/runtime/content/variable_reference.gd") +var InkVariableAssignment = load("res://addons/inkgd/runtime/content/variable_assignment.gd") # ############################################################################ # @@ -273,37 +274,37 @@ func jobject_to_int_dictionary(jobject: Dictionary) -> Dictionary: func jtoken_to_runtime_object(token) -> InkObject: if token is int || token is float || token is bool: - return Value.create(token) + return InkValue.create(token) if token is String: var _str = token var first_char = _str[0] if first_char == "^": - return StringValue.new_with(_str.substr(1, _str.length() - 1)) + return InkStringValue.new_with(_str.substr(1, _str.length() - 1)) elif first_char == "\n" && _str.length() == 1: - return StringValue.new_with("\n") + return InkStringValue.new_with("\n") - if _str == "<>": return Glue.new() + if _str == "<>": return InkGlue.new() var i = 0 while (i < _control_command_names.size()): var cmd_name = _control_command_names[i] if _str == cmd_name: - return ControlCommand.new(i) + return InkControlCommand.new(i) i += 1 if _str == "L^": _str = "^" - if StaticNativeFunctionCall.call_exists_with_name(_str): + if _static_native_function_call.call_exists_with_name(_str): return InkNativeFunctionCall.call_with_name(_str) if _str == "->->": - return ControlCommand.pop_tunnel() + return InkControlCommand.pop_tunnel() elif _str == "~ret": - return ControlCommand.pop_function() + return InkControlCommand.pop_function() if _str == "void": - return Void.new() + return InkVoid.new() if token is Dictionary: var obj = token @@ -311,11 +312,13 @@ func jtoken_to_runtime_object(token) -> InkObject: if obj.has("^->"): prop_value = obj["^->"] - return DivertTargetValue.new_with(InkPath.new_with_components_string(str(prop_value))) + return InkDivertTargetValue.new_with( + InkPath.new_with_components_string(str(prop_value)) + ) if obj.has("^var"): prop_value = obj["^var"] - var var_ptr = VariablePointerValue.new_with_context(str(prop_value)) + var var_ptr = InkVariablePointerValue.new_with_context(str(prop_value)) if (obj.has("ci")): prop_value = obj["ci"] var_ptr.context_index = int(prop_value) @@ -347,7 +350,7 @@ func jtoken_to_runtime_object(token) -> InkObject: div_push_type = PushPopType.FUNCTION if is_divert: - var divert = Divert.new() + var divert = InkDivert.new() divert.pushes_to_stack = pushes_to_stack divert.stack_push_type = div_push_type divert.is_external = external @@ -372,7 +375,7 @@ func jtoken_to_runtime_object(token) -> InkObject: if obj.has("*"): prop_value = obj["*"] - var choice = ChoicePoint.new() + var choice = InkChoicePoint.new() choice.path_string_on_choice = str(prop_value) if obj.has("flg"): @@ -383,10 +386,10 @@ func jtoken_to_runtime_object(token) -> InkObject: if obj.has("VAR?"): prop_value = obj["VAR?"] - return VariableReference.new(str(prop_value)) + return InkVariableReference.new(str(prop_value)) elif obj.has("CNT?"): prop_value = obj["CNT?"] - var read_count_var_ref = VariableReference.new() + var read_count_var_ref = InkVariableReference.new() read_count_var_ref.path_string_for_count = str(prop_value) return read_count_var_ref @@ -404,13 +407,13 @@ func jtoken_to_runtime_object(token) -> InkObject: if is_var_ass: var var_name = str(prop_value) var is_new_decl = !obj.has("re") - var var_ass = VariableAssignment.new_with(var_name, is_new_decl) + var var_ass = InkVariableAssignment.new_with(var_name, is_new_decl) var_ass.is_global = is_global_var return var_ass if obj.has("#"): prop_value = obj["#"] - return Tag.new(str(prop_value)) + return InkTag.new(str(prop_value)) if obj.has("list"): prop_value = obj["list"] @@ -426,7 +429,7 @@ func jtoken_to_runtime_object(token) -> InkObject: var val = list_content[name_to_val_key] raw_list.set_item(item, val) - return ListValue.new_with(raw_list) + return InkListValue.new_with(raw_list) if obj.has("originalChoicePath"): return jobject_to_choice(obj) @@ -505,7 +508,7 @@ func jarray_to_container(jarray: Array) -> InkContainer: # (Dictionary) -> Choice func jobject_to_choice(jobj: Dictionary) -> InkChoice: - var choice = Choice.new() + var choice = InkChoice.new() choice.text = str(jobj["text"]) choice.index = int(jobj["index"]) choice.source_path = str(jobj["originalChoicePath"]) @@ -590,13 +593,13 @@ func jtoken_to_list_definitions(obj): for name_value_key in list_def_json: items[name_value_key] = int(list_def_json[name_value_key]) - var def = ListDefinition.new(name, items) + var def = InkListDefinition.new(name, items) all_defs.append(def) - return ListDefinitionsOrigin.new(all_defs) + return InkListDefinitionsOrigin.new(all_defs) func _init(native_function_call): - StaticNativeFunctionCall = native_function_call + _static_native_function_call = native_function_call _control_command_names = [] @@ -626,13 +629,15 @@ func _init(native_function_call): _control_command_names.append("lrnd") # LIST_RANDOM var i = 0 - while i < ControlCommand.CommandType.TOTAL_VALUES: + while i < InkControlCommand.CommandType.TOTAL_VALUES: if _control_command_names[i] == null: Utils.throw_exception("Control command not accounted for in serialisation") i += 1 -var _control_command_names = null # Array +# Array +var _control_command_names = null # ############################################################################ # -var StaticNativeFunctionCall = null # Eventually a pointer to InkRuntime.StaticJson +# Eventually a pointer to InkRuntime.StaticJson +var _static_native_function_call = null diff --git a/addons/inkgd/runtime/static/native_function_call.gd b/addons/inkgd/runtime/static/native_function_call.gd index 901d3870..45a1a855 100644 --- a/addons/inkgd/runtime/static/native_function_call.gd +++ b/addons/inkgd/runtime/static/native_function_call.gd @@ -19,7 +19,7 @@ class_name InkStaticNativeFunctionCall const ValueType = preload("res://addons/inkgd/runtime/values/value_type.gd").ValueType -static func NativeFunctionCall(): +static func InkNativeFunctionCall(): return load("res://addons/inkgd/runtime/content/native_function_call.gd") # ############################################################################ # @@ -168,7 +168,7 @@ func add_op_to_native_func(name, args, val_type, op): if native_functions.has(name): native_func = native_functions[name] else: - native_func = NativeFunctionCall().new_with_name_and_number_of_parameters(name, args) + native_func = InkNativeFunctionCall().new_with_name_and_number_of_parameters(name, args) native_functions[name] = native_func native_func.add_op_func_for_type(val_type, op) diff --git a/addons/inkgd/runtime/story.gd b/addons/inkgd/runtime/story.gd index 7b7e1667..a83392de 100644 --- a/addons/inkgd/runtime/story.gd +++ b/addons/inkgd/runtime/story.gd @@ -24,37 +24,38 @@ const INK_VERSION_MINIMUM_COMPATIBLE = 18 var PushPopType = preload("res://addons/inkgd/runtime/enums/push_pop.gd").PushPopType var ErrorType = preload("res://addons/inkgd/runtime/enums/error.gd").ErrorType -var InkListItem = preload("res://addons/inkgd/runtime/lists/structs/ink_list_item.gd") -var ListDefinitionsOrigin = load("res://addons/inkgd/runtime/lists/list_definitions_origin.gd") -var StoryState = load("res://addons/inkgd/runtime/story_state.gd") +var InkStopWatch := preload("res://addons/inkgd/runtime/extra/stopwatch.gd") as GDScript +var InkProfiler := preload("res://addons/inkgd/runtime/profiler.gd") as GDScript -var Pointer = load("res://addons/inkgd/runtime/structs/pointer.gd") -var ControlCommand = load("res://addons/inkgd/runtime/content/control_command.gd") +var InkSimpleJSON := preload("res://addons/inkgd/runtime/simple_json.gd") as GDScript +var InkStringSet := preload("res://addons/inkgd/runtime/extra/string_set.gd") as GDScript +var InkListItem := preload("res://addons/inkgd/runtime/lists/structs/ink_list_item.gd") as GDScript +var InkListDefinitionsOrigin := preload("res://addons/inkgd/runtime/lists/list_definitions_origin.gd") as GDScript -var Value = load("res://addons/inkgd/runtime/values/value.gd") -var IntValue = load("res://addons/inkgd/runtime/values/int_value.gd") -var StringValue = load("res://addons/inkgd/runtime/values/string_value.gd") -var VariablePointerValue = load("res://addons/inkgd/runtime/values/variable_pointer_value.gd") -var ListValue = load("res://addons/inkgd/runtime/values/list_value.gd") +var InkPointer := preload("res://addons/inkgd/runtime/structs/pointer.gd") as GDScript +var InkControlCommand := preload("res://addons/inkgd/runtime/content/control_command.gd") as GDScript -var Stopwatch = load("res://addons/inkgd/runtime/extra/stopwatch.gd") -var InkList = load("res://addons/inkgd/runtime/lists/ink_list.gd") +var InkVoid := preload("res://addons/inkgd/runtime/content/void.gd") as GDScript -var StringSet = load("res://addons/inkgd/runtime/extra/string_set.gd") +# ############################################################################ # -var Choice = load("res://addons/inkgd/runtime/content/choices/choice.gd") -var Void = load("res://addons/inkgd/runtime/content/void.gd") +var InkValue := load("res://addons/inkgd/runtime/values/value.gd") as GDScript +var InkIntValue := load("res://addons/inkgd/runtime/values/int_value.gd") as GDScript +var InkStringValue := load("res://addons/inkgd/runtime/values/string_value.gd") as GDScript +var InkVariablePointerValue := load("res://addons/inkgd/runtime/values/variable_pointer_value.gd") as GDScript +var InkListValue := load("res://addons/inkgd/runtime/values/list_value.gd") as GDScript -var Profiler = load("res://addons/inkgd/runtime/profiler.gd") +var InkList := load("res://addons/inkgd/runtime/lists/ink_list.gd") as GDScript +var InkChoice := load("res://addons/inkgd/runtime/content/choices/choice.gd") as GDScript -var SimpleJson = load("res://addons/inkgd/runtime/simple_json.gd") +var InkStoryState := load("res://addons/inkgd/runtime/story_state.gd") as GDScript # ############################################################################ # -var current_choices setget , get_current_choices # Array -func get_current_choices(): - var choices = [] # Array +var current_choices: Array setget , get_current_choices # Array +func get_current_choices() -> Array: + var choices: Array = [] # Array for c in self._state.current_choices: if !c.is_invisible_default: @@ -63,13 +64,15 @@ func get_current_choices(): return choices -var current_text setget , get_current_text # String +# String? +var current_text setget , get_current_text func get_current_text(): if async_we_cant("call currentText since it's a work in progress"): return null return self.state.current_text +# Array? var current_tags setget , get_current_tags # Array func get_current_tags(): if async_we_cant("call currentTags since it's a work in progress"): @@ -99,7 +102,7 @@ var list_definitions setget , get_list_definitions # ListDefinitionsOrigin func get_list_definitions(): return self._list_definitions -var state setget , get_state # StoryState +var state: InkStoryState setget , get_state # StoryState func get_state(): return self._state @@ -133,7 +136,7 @@ func _init_with(content_container, lists = null): self._main_content_container = content_container if lists != null: - self._list_definitions = ListDefinitionsOrigin.new(lists) + self._list_definitions = InkListDefinitionsOrigin.new(lists) self._externals = {} # Dictionary @@ -141,7 +144,7 @@ func _init_with(content_container, lists = null): func _init(json_string): _init_with(null) - var root_object = SimpleJson.text_to_dictionary(json_string) + var root_object = InkSimpleJSON.text_to_dictionary(json_string) var version_obj = root_object["inkVersion"] if version_obj == null: @@ -178,7 +181,7 @@ func _init(json_string): # () -> String func to_json(): - var writer = SimpleJson.Writer.new() + var writer: InkSimpleJSON.Writer = InkSimpleJSON.Writer.new() to_json_with_writer(writer) return writer.to_string() @@ -219,7 +222,7 @@ func reset_state(): if async_we_cant ("ResetState"): return - self._state = StoryState.new(self) + self._state = InkStoryState.new(self) self._state.variables_state.connect("variable_changed", self, "variable_state_did_change_event") self.reset_globals() @@ -305,7 +308,7 @@ func continue_internal(millisecs_limit_async = 0): if self._recursive_continue_count == 1: self._state.variables_state.batch_observing_variable_changes = true - var duration_stopwatch = Stopwatch.new() + var duration_stopwatch = InkStopWatch.new() duration_stopwatch.start() var output_stream_ends_in_newline = false @@ -499,9 +502,9 @@ func knot_container_with_name(name): # (InkPath) -> Result func pointer_at_path(path): if (path.length == 0): - return Pointer.null() + return InkPointer.null() - var p = Pointer.new() + var p = InkPointer.new() var path_length_to_use = path.length @@ -584,7 +587,7 @@ func step(): if container_to_enter.content.size() == 0: break - pointer = Pointer.start_of(container_to_enter) + pointer = InkPointer.start_of(container_to_enter) container_to_enter = Utils.as_or_null(pointer.resolve(), "InkContainer") self.state.current_pointer = pointer.duplicate() @@ -617,7 +620,7 @@ func step(): var var_pointer = Utils.as_or_null(current_content_obj, "VariablePointerValue") if var_pointer && var_pointer.context_index == -1: var context_idx = self.state.callstack.context_for_variable_named(var_pointer.variable_name) - current_content_obj = VariablePointerValue.new_with_context(var_pointer.variable_name, context_idx) + current_content_obj = InkVariablePointerValue.new_with_context(var_pointer.variable_name, context_idx) if self.state.in_expression_evaluation: self.state.push_evaluation_stack(current_content_obj) @@ -627,7 +630,7 @@ func step(): self.next_content() var control_cmd = Utils.as_or_null(current_content_obj, "ControlCommand") - if control_cmd && control_cmd.command_type == ControlCommand.CommandType.START_THREAD: + if control_cmd && control_cmd.command_type == InkControlCommand.CommandType.START_THREAD: self.state.callstack.push_thread() # (InkContainer, bool) -> void @@ -704,7 +707,7 @@ func process_choice(choice_point): if !show_choice: return null - var choice = Choice.new() + var choice = InkChoice.new() choice.target_path = choice_point.path_on_choice choice.source_path = choice_point.path.to_string() choice.is_invisible_default = choice_point.is_invisible_default @@ -797,33 +800,35 @@ func perform_logic_and_flow_control(content_obj): match eval_command.command_type: - ControlCommand.CommandType.EVAL_START: + InkControlCommand.CommandType.EVAL_START: self.__assert__(self.state.in_expression_evaluation == false, "Already in expression evaluation?") self.state.in_expression_evaluation = true - ControlCommand.CommandType.EVAL_END: + InkControlCommand.CommandType.EVAL_END: self.__assert__(self.state.in_expression_evaluation == true, "Not in expression evaluation mode") self.state.in_expression_evaluation = false - ControlCommand.CommandType.EVAL_OUTPUT: + InkControlCommand.CommandType.EVAL_OUTPUT: if self.state.evaluation_stack.size() > 0: var output = self.state.pop_evaluation_stack() if !Utils.as_or_null(output, "Void"): - var text = StringValue.new_with(output.to_string()) + var text = InkStringValue.new_with(output.to_string()) self.state.push_to_output_stream(text) - ControlCommand.CommandType.NO_OP: + InkControlCommand.CommandType.NO_OP: pass - ControlCommand.CommandType.DUPLICATE: + InkControlCommand.CommandType.DUPLICATE: self.state.push_evaluation_stack(self.state.peek_evaluation_stack()) - ControlCommand.CommandType.POP_EVALUATED_VALUE: + InkControlCommand.CommandType.POP_EVALUATED_VALUE: self.state.pop_evaluation_stack() - ControlCommand.CommandType.POP_FUNCTION, ControlCommand.CommandType.POP_TUNNEL: - var is_pop_function = (eval_command.command_type == ControlCommand.CommandType.POP_FUNCTION) + InkControlCommand.CommandType.POP_FUNCTION, InkControlCommand.CommandType.POP_TUNNEL: + var is_pop_function = ( + eval_command.command_type == InkControlCommand.CommandType.POP_FUNCTION + ) var pop_type = PushPopType.FUNCTION if is_pop_function else PushPopType.TUNNEL var override_tunnel_return_target = null # DivertTargetValue @@ -854,14 +859,14 @@ func perform_logic_and_flow_control(content_obj): if override_tunnel_return_target: self.state.diverted_pointer = self.pointer_at_path(override_tunnel_return_target.target_path) - ControlCommand.CommandType.BEGIN_STRING: + InkControlCommand.CommandType.BEGIN_STRING: self.state.push_to_output_stream(eval_command) self.__assert__(self.state.in_expression_evaluation == true, "Expected to be in an expression when evaluating a string") self.state.in_expression_evaluation = false - ControlCommand.CommandType.END_STRING: + InkControlCommand.CommandType.END_STRING: var content_stack_for_string = [] # Stack var output_count_consumed = 0 @@ -873,7 +878,7 @@ func perform_logic_and_flow_control(content_obj): var command = Utils.as_or_null(obj, "ControlCommand") if (command != null && - command.command_type == ControlCommand.CommandType.BEGIN_STRING): + command.command_type == InkControlCommand.CommandType.BEGIN_STRING): break if Utils.is_ink_class(obj, "StringValue"): @@ -888,16 +893,16 @@ func perform_logic_and_flow_control(content_obj): _str += c.to_string() self.state.in_expression_evaluation = true - self.state.push_evaluation_stack(StringValue.new_with(_str)) + self.state.push_evaluation_stack(InkStringValue.new_with(_str)) - ControlCommand.CommandType.CHOICE_COUNT: + InkControlCommand.CommandType.CHOICE_COUNT: var choice_count = self.state.generated_choices.size() - self.state.push_evaluation_stack(IntValue.new_with(choice_count)) + self.state.push_evaluation_stack(InkIntValue.new_with(choice_count)) - ControlCommand.CommandType.TURNS: - self.state.push_evaluation_stack(IntValue.new_with(self.state.current_turn_index + 1)) + InkControlCommand.CommandType.TURNS: + self.state.push_evaluation_stack(InkIntValue.new_with(self.state.current_turn_index + 1)) - ControlCommand.CommandType.TURNS_SINCE, ControlCommand.CommandType.READ_COUNT: + InkControlCommand.CommandType.TURNS_SINCE, InkControlCommand.CommandType.READ_COUNT: var target = self.state.pop_evaluation_stack() if !Utils.is_ink_class(target, "DivertTargetValue"): var extra_note = "" @@ -912,12 +917,12 @@ func perform_logic_and_flow_control(content_obj): var either_count = 0 if container != null: - if eval_command.command_type == ControlCommand.CommandType.TURNS_SINCE: + if eval_command.command_type == InkControlCommand.CommandType.TURNS_SINCE: either_count = self.state.turns_since_for_container(container) else: either_count = self.state.visit_count_for_container(container) else: - if eval_command.command_type == ControlCommand.CommandType.TURNS_SINCE: + if eval_command.command_type == InkControlCommand.CommandType.TURNS_SINCE: either_count = -1 else: either_count = 0 @@ -925,9 +930,9 @@ func perform_logic_and_flow_control(content_obj): warning(str("Failed to find container for ", eval_command.to_string(), " lookup at ", divert_target.target_path.to_string())) - self.state.push_evaluation_stack(IntValue.new_with(either_count)) + self.state.push_evaluation_stack(InkIntValue.new_with(either_count)) - ControlCommand.CommandType.RANDOM: + InkControlCommand.CommandType.RANDOM: var max_int = Utils.as_or_null(self.state.pop_evaluation_stack(), "IntValue") var min_int = Utils.as_or_null(self.state.pop_evaluation_stack(), "IntValue") @@ -956,11 +961,11 @@ func perform_logic_and_flow_control(content_obj): var next_random = randi() var chosen_value = (next_random % random_range) + min_int.value - self.state.push_evaluation_stack(IntValue.new_with(chosen_value)) + self.state.push_evaluation_stack(InkIntValue.new_with(chosen_value)) self.state.previous_random = next_random - ControlCommand.CommandType.SEED_RANDOM: + InkControlCommand.CommandType.SEED_RANDOM: var _seed = Utils.as_or_null(self.state.pop_evaluation_stack(), "IntValue") if _seed == null: error("Invalid value passed to SEED_RANDOM") @@ -969,30 +974,30 @@ func perform_logic_and_flow_control(content_obj): self.state.story_seed = _seed.value self.state.previous_random = 0 - self.state.push_evaluation_stack(Void.new()) + self.state.push_evaluation_stack(InkVoid.new()) - ControlCommand.CommandType.VISIT_INDEX: + InkControlCommand.CommandType.VISIT_INDEX: var count = self.state.visit_count_for_container(self.state.current_pointer.container) - 1 - self.state.push_evaluation_stack(IntValue.new_with(count)) + self.state.push_evaluation_stack(InkIntValue.new_with(count)) - ControlCommand.CommandType.SEQUENCE_SHUFFLE_INDEX: + InkControlCommand.CommandType.SEQUENCE_SHUFFLE_INDEX: var shuffle_index = self.next_sequence_shuffle_index() - self.state.push_evaluation_stack(IntValue.new_with(shuffle_index)) + self.state.push_evaluation_stack(InkIntValue.new_with(shuffle_index)) - ControlCommand.CommandType.START_THREAD: + InkControlCommand.CommandType.START_THREAD: pass - ControlCommand.CommandType.DONE: + InkControlCommand.CommandType.DONE: if self.state.callstack.can_pop_thread: self.state.callstack.pop_thread() else: self.state.did_safe_exit = true - self.state.current_pointer = Pointer.null() + self.state.current_pointer = InkPointer.null() - ControlCommand.CommandType.END: + InkControlCommand.CommandType.END: self.state.force_end() - ControlCommand.CommandType.LIST_FROM_INT: + InkControlCommand.CommandType.LIST_FROM_INT: var int_val = Utils.as_or_null(self.state.pop_evaluation_stack(), "IntValue") var list_name_val = Utils.as_or_null(self.state.pop_evaluation_stack(), "StringValue") @@ -1006,17 +1011,20 @@ func perform_logic_and_flow_control(content_obj): if found_list_def.exists: var found_item = found_list_def.result.try_get_item_with_value(int_val.value) if found_item.exists: - generated_list_value = ListValue.new_with_single_item(found_item.result, int_val.value) + generated_list_value = InkListValue.new_with_single_item( + found_item.result, + int_val.value + ) else: Utils.throw_story_exception("Failed to find LIST called " + list_name_val.value) return null if generated_list_value == null: - generated_list_value = ListValue.new() + generated_list_value = InkListValue.new() self.state.push_evaluation_stack(generated_list_value) - ControlCommand.CommandType.LIST_RANGE: + InkControlCommand.CommandType.LIST_RANGE: var max_value = Utils.as_or_null(self.state.pop_evaluation_stack(), "Value") var min_value = Utils.as_or_null(self.state.pop_evaluation_stack(), "Value") @@ -1028,9 +1036,9 @@ func perform_logic_and_flow_control(content_obj): var result = target_list.value.list_with_sub_range(min_value.value_object, max_value.value_object) - self.state.push_evaluation_stack(ListValue.new_with(result)) + self.state.push_evaluation_stack(InkListValue.new_with(result)) - ControlCommand.CommandType.LIST_RANDOM: + InkControlCommand.CommandType.LIST_RANDOM: var list_val = Utils.as_or_null(self.state.pop_evaluation_stack(), "ListValue") if list_val == null: @@ -1063,7 +1071,7 @@ func perform_logic_and_flow_control(content_obj): self.state.previous_random = next_random - self.state.push_evaluation_stack(ListValue.new_with(new_list)) + self.state.push_evaluation_stack(InkListValue.new_with(new_list)) _: error("unhandled ControlCommand: " + eval_command.to_string()) @@ -1086,7 +1094,7 @@ func perform_logic_and_flow_control(content_obj): if var_ref.path_for_count != null: var container = var_ref.container_for_count var count = self.state.visit_count_for_container(container) - found_value = IntValue.new_with(count) + found_value = InkIntValue.new_with(count) else: found_value = self.state.variables_state.get_variable_with_name(var_ref.name) @@ -1096,7 +1104,7 @@ func perform_logic_and_flow_control(content_obj): "happen with temporary variables if the declaration", "hasn't yet been hit. Globals are always given a default", "value on load if a value doesn't exist in the save state.")) - found_value = IntValue.new_with(0) + found_value = InkIntValue.new_with(0) self.state.push_evaluation_stack(found_value) return true @@ -1104,7 +1112,7 @@ func perform_logic_and_flow_control(content_obj): elif Utils.as_or_null(content_obj, "NativeFunctionCall"): var function = content_obj var func_params = self.state.pop_evaluation_stack(function.number_of_parameters) - var result = function.call(func_params) + var result = function.call_with_parameters(func_params) self.state.push_evaluation_stack(result) return true @@ -1265,12 +1273,14 @@ func call_external_function(func_name, number_of_arguments): self.state.output_stream.size() ) - self.state.diverted_pointer = Pointer.start_of(fallback_function_container) + self.state.diverted_pointer = InkPointer.start_of(fallback_function_container) return else: - self.__assert__(false, - str("Trying to call EXTERNAL function '", func_name, - "' which has not been bound (and ink fallbacks disabled).")) + self.__assert__( + false, + "Trying to call EXTERNAL function '%s'" % func_name, + "which has not been bound (and ink fallbacks disabled)." + ) var arguments = [] # Array var i = 0 @@ -1287,12 +1297,12 @@ func call_external_function(func_name, number_of_arguments): var return_obj = null if func_result != null: - return_obj = Value.create(func_result) + return_obj = InkValue.create(func_result) self.__assert__(return_obj != null, str("Could not create ink value from returned object of type ", typeof(func_result))) else: - return_obj = Void.new() + return_obj = InkVoid.new() self.state.push_evaluation_stack(return_obj) @@ -1322,7 +1332,7 @@ func unbind_external_function(func_name): _externals.erase(func_name) func validate_external_bindings(): - var missing_externals = StringSet.new() + var missing_externals = InkStringSet.new() validate_external_bindings_with(_main_content_container, missing_externals) _has_validated_externals = true @@ -1505,7 +1515,7 @@ func next_content(): if !self.state.diverted_pointer.is_null: self.state.current_pointer = self.state.diverted_pointer.duplicate() - self.state.diverted_pointer = Pointer.null() + self.state.diverted_pointer = InkPointer.null() self.visit_changed_containers_due_to_divert() @@ -1522,7 +1532,7 @@ func next_content(): self.state.pop_callstack(PushPopType.FUNCTION) if self.state.in_expression_evaluation: - self.state.push_evaluation_stack(Void.new()) + self.state.push_evaluation_stack(InkVoid.new()) did_pop = true elif self.state.callstack.can_pop_thread: @@ -1554,13 +1564,13 @@ func increment_content_pointer(): if index_in_ancestor == -1: break - pointer = Pointer.new(next_ancestor, index_in_ancestor) + pointer = InkPointer.new(next_ancestor, index_in_ancestor) pointer.index += 1 successful_increment = true - if !successful_increment: pointer = Pointer.null() + if !successful_increment: pointer = InkPointer.null() var current_element = self.state.callstack.current_element current_element.current_pointer = pointer.duplicate() diff --git a/addons/inkgd/runtime/story_state.gd b/addons/inkgd/runtime/story_state.gd index 4514ebdf..77314e65 100644 --- a/addons/inkgd/runtime/story_state.gd +++ b/addons/inkgd/runtime/story_state.gd @@ -15,59 +15,65 @@ extends InkBase class_name InkStoryState # ############################################################################ # -# Self-reference +# Imports # ############################################################################ # -static func StoryState(): - return load("res://addons/inkgd/runtime/story_state.gd") +var PushPopType = preload("res://addons/inkgd/runtime/enums/push_pop.gd").PushPopType +var ValueType = preload("res://addons/inkgd/runtime/values/value_type.gd").ValueType + +var InkPointer = preload("res://addons/inkgd/runtime/structs/pointer.gd") +var InkPath = preload("res://addons/inkgd/runtime/ink_path.gd") # ############################################################################ # -# Imports -# ############################################################################ # -var PushPopType = preload("res://addons/inkgd/runtime/enums/push_pop.gd").PushPopType -var Pointer = load("res://addons/inkgd/runtime/structs/pointer.gd") -var CallStack = load("res://addons/inkgd/runtime/callstack.gd") -var VariablesState = load("res://addons/inkgd/runtime/variables_state.gd") -var InkPath = load("res://addons/inkgd/runtime/ink_path.gd") -var ControlCommand = load("res://addons/inkgd/runtime/content/control_command.gd") -var SimpleJson = load("res://addons/inkgd/runtime/simple_json.gd") -var StatePatch = load("res://addons/inkgd/runtime/state_patch.gd") -var Flow = load("res://addons/inkgd/runtime/flow.gd") +var InkValue = load("res://addons/inkgd/runtime/values/value.gd") +var InkStringValue = load("res://addons/inkgd/runtime/values/string_value.gd") -var ValueType = preload("res://addons/inkgd/runtime/values/value_type.gd").ValueType +var InkControlCommand = preload("res://addons/inkgd/runtime/content/control_command.gd") +var InkSimpleJSON = preload("res://addons/inkgd/runtime/simple_json.gd") +var InkStatePatch = preload("res://addons/inkgd/runtime/state_patch.gd") + +var InkCallStack = load("res://addons/inkgd/runtime/callstack.gd") +var InkVariablesState = load("res://addons/inkgd/runtime/variables_state.gd") +var InkFlow = load("res://addons/inkgd/runtime/flow.gd") + +# ############################################################################ # +# Self-reference +# ############################################################################ # -var Value = load("res://addons/inkgd/runtime/values/value.gd") -var StringValue = load("res://addons/inkgd/runtime/values/string_value.gd") +static func InkStoryState() -> GDScript: + return load("res://addons/inkgd/runtime/story_state.gd") as GDScript # ############################################################################ # -const INK_SAVE_STATE_VERSION = 9 -const MIN_COMPATIBLE_LOAD_VERSION = 8 +const INK_SAVE_STATE_VERSION: int = 9 +const MIN_COMPATIBLE_LOAD_VERSION: int = 8 + +# ############################################################################ # signal on_did_load_state() -# () -> String -func to_json(): - var writer = SimpleJson.Writer.new() +# ############################################################################ # + +func to_json() -> String: + var writer: InkSimpleJSON.Writer = InkSimpleJSON.Writer.new() write_json(writer) return writer.to_string() -# (String) -> void -func load_json(json): - var jobject = SimpleJson.text_to_dictionary(json) +func load_json(json: String) -> void: + var jobject: Dictionary = InkSimpleJSON.text_to_dictionary(json) load_json_obj(jobject) emit_signal("on_did_load_state") -# (String) -> int -func visit_count_at_path_string(path_string): +func visit_count_at_path_string(path_string: String) -> int: if self._patch != null: - var container = self.story.content_at_path(InkPath.new_with_components_string(path_string)).container + var path = InkPath.new_with_components_string(path_string) + var container: InkContainer = self.story.content_at_path(path).container if container == null: - Utils.throw_exception(str("Content at path not found: ", path_string)) + Utils.throw_exception("Content at path not found: %s" % path_string) return 0 - var visit_count = self._patch.try_get_visit_count(container) + var visit_count: InkTryGetResult = self._patch.try_get_visit_count(container) if visit_count.exists: return visit_count.result @@ -76,117 +82,122 @@ func visit_count_at_path_string(path_string): return 0 -# (InkContainer) -> int -func visit_count_for_container(container): +func visit_count_for_container(container: InkContainer) -> int: if !container.visits_should_be_counted: self.story.error( - str("Read count for target (", container.name, " - on ", - container.debugMetadata, ") unknown. The story may need to", - "be compiled with countAllVisits flag (-c).") + "Read count for target (%s - on %s) " % [container.name, container.debugMetadata] + + "unknown. The story may need to be compiled with countAllVisits flag (-c)." ) return 0 - var count = 0 + var count: int = 0 if self._patch != null: - var visit_count = self._patch.try_get_visit_count(container) + var visit_count: InkTryGetResult = self._patch.try_get_visit_count(container) if visit_count.exists: return visit_count.result - var container_path_str = container.path.to_string() + var container_path_str: String = container.path.to_string() if self._visit_counts.has(container_path_str): count = self._visit_counts[container_path_str] return count - -# (InkContainer) -> void -func increment_visit_count_for_container(container): +func increment_visit_count_for_container(container: InkContainer) -> void: if self._patch != null: - var curr_count = visit_count_for_container(container) + var curr_count: int = visit_count_for_container(container) curr_count += 1 self._patch.set_visit_count(container, curr_count) return - var count = 0 - var container_path_str = container.path.to_string() + var count: int = 0 + var container_path_str: String = container.path.to_string() if self._visit_counts.has(container_path_str): count = self._visit_counts[container_path_str] count += 1 self._visit_counts[container_path_str] = count -# (InkContainer) -> void -func record_turn_index_visit_to_container(container): +func record_turn_index_visit_to_container(container: InkContainer) -> void: if self._patch != null: self._patch.set_turn_index(container, self.current_turn_index) return - var container_path_str = container.path.to_string() + var container_path_str: String = container.path.to_string() self._turn_indices[container_path_str] = self.current_turn_index -# (InkContainer) -> ink -func turns_since_for_container(container): +# (InkContainer) -> int +func turns_since_for_container(container: InkContainer) -> int: if !container.turn_index_should_be_counted: self.story.error( - str("TURNS_SINCE() for target (", container.name, " - on ", - container.debugMetadata, ") unknown. The story may need", - "to be compiled with countAllVisits flag (-c).") - ) - return null + "TURNS_SINCE() for target (%s - on %s) " \ + % [container.name, container.debugMetadata] + + "unknown. The story may need to be compiled with countAllVisits flag (-c)." + ) + return 0 if self._patch != null: - var turn_index = self._patch.try_get_turn_index(container) + var turn_index: InkTryGetResult = self._patch.try_get_turn_index(container) if turn_index.exists: return self.current_turn_index - turn_index.result - var container_path_str = container.path.to_string() + var container_path_str: String = container.path.to_string() if self._turn_indices.has(container_path_str): return self.current_turn_index - self._turn_indices[container_path_str] else: return -1 -var callstack_depth setget , get_callstack_depth # int -func get_callstack_depth(): +var callstack_depth: int setget , get_callstack_depth # int +func get_callstack_depth() -> int: return self.callstack.depth -var output_stream setget , get_output_stream # Array -func get_output_stream(): +var output_stream: Array setget , get_output_stream # Array +func get_output_stream() -> Array: return self._current_flow.output_stream -var current_choices setget , get_current_choices # Array -func get_current_choices(): - if self.can_continue: return [] +var current_choices: Array setget , get_current_choices # Array +func get_current_choices() -> Array: + if self.can_continue: + return [] return self._current_flow.current_choices -var generated_choices setget , get_generated_choices # Array -func get_generated_choices(): +var generated_choices: Array setget , get_generated_choices # Array +func get_generated_choices() -> Array: return self._current_flow.current_choices -var current_errors = null # Array -var current_warnings = null # Array -var variables_state = null # VariableState +# Array +var current_errors = null + +# Array +var current_warnings = null + +# InkVariablesState +var variables_state -var callstack setget , get_callstack # CallStack -func get_callstack(): +var callstack: InkCallStack setget , get_callstack +func get_callstack() -> InkCallStack: return self._current_flow.callstack -var evaluation_stack = null # Array -var diverted_pointer = Pointer.null() # Pointer +# Array +var evaluation_stack: Array -var current_turn_index = 0 # int -var story_seed = 0 # int -var previous_random = 0 # int -var did_safe_exit = false # bool +# Pointer +var diverted_pointer: InkPointer = InkPointer.null() + +var current_turn_index: int = 0 +var story_seed: int = 0 +var previous_random: int = 0 +var did_safe_exit: bool = false var story setget , get_story func get_story(): return _story.get_ref() var _story = WeakRef.new() -var current_path_string setget , get_current_path_string # String +# String? +var current_path_string setget , get_current_path_string func get_current_path_string(): var pointer = self.current_pointer if pointer.is_null: @@ -194,42 +205,42 @@ func get_current_path_string(): else: return pointer.path.to_string() -var current_pointer setget set_current_pointer, get_current_pointer # Pointer -func get_current_pointer(): +var current_pointer: InkPointer setget set_current_pointer, get_current_pointer +func get_current_pointer() -> InkPointer: var pointer = self.callstack.current_element.current_pointer return self.callstack.current_element.current_pointer.duplicate() -func set_current_pointer(value): +func set_current_pointer(value: InkPointer): var current_element = self.callstack.current_element current_element.current_pointer = value.duplicate() -var previous_pointer setget set_previous_pointer, get_previous_pointer # Pointer -func get_previous_pointer(): +var previous_pointer: InkPointer setget set_previous_pointer, get_previous_pointer +func get_previous_pointer() -> InkPointer: return self.callstack.current_thread.previous_pointer.duplicate() -func set_previous_pointer(value): +func set_previous_pointer(value: InkPointer): var current_thread = self.callstack.current_thread current_thread.previous_pointer = value.duplicate() -var can_continue setget , get_can_continue # bool -func get_can_continue(): +var can_continue: bool setget , get_can_continue +func get_can_continue() -> bool: return !self.current_pointer.is_null && !self.has_error -var has_error setget , get_has_error # bool -func get_has_error(): +var has_error: bool setget , get_has_error +func get_has_error() -> bool: return self.current_errors != null && self.current_errors.size() > 0 -var has_warning setget , get_has_warning # bool -func get_has_warning(): +var has_warning: bool setget , get_has_warning +func get_has_warning() -> bool: return self.current_warnings != null && self.current_warnings.size() > 0 -var current_text setget , get_current_text # String +var current_text: String setget , get_current_text func get_current_text(): if self._output_stream_text_dirty: var _str = "" for output_obj in self.output_stream: - var text_content = Utils.as_or_null(output_obj, "StringValue") + var text_content: InkStringValue = Utils.as_or_null(output_obj, "StringValue") if text_content != null: _str += text_content.value @@ -239,20 +250,20 @@ func get_current_text(): return self._current_text -var _current_text = null # String +var _current_text: String = "" # (String) -> String -func clean_output_whitespace(str_to_clean): - var _str = "" +func clean_output_whitespace(str_to_clean: String) -> String: + var _str: String = "" - var current_whitespace_start = -1 - var start_of_line = 0 + var current_whitespace_start: int = -1 + var start_of_line: int = 0 - var i = 0 + var i: int = 0 while(i < str_to_clean.length()): - var c = str_to_clean[i] + var c: String = str_to_clean[i] - var is_inline_whitespace = (c == " " || c == "\t") + var is_inline_whitespace: bool = (c == " " || c == "\t") if is_inline_whitespace && current_whitespace_start == -1: current_whitespace_start = i @@ -273,10 +284,11 @@ func clean_output_whitespace(str_to_clean): return _str -var current_tags setget , get_current_tags # Array +# Array +var current_tags: Array setget , get_current_tags func get_current_tags(): if self._output_stream_tags_dirty: - self._current_tags = [] # Array + self._current_tags = [] for output_obj in self.output_stream: var tag = Utils.as_or_null(output_obj, "Tag") @@ -287,34 +299,36 @@ func get_current_tags(): return self._current_tags -var _current_tags # Array +# Array +var _current_tags: Array = [] -var current_flow_name setget , get_current_flow_name # String -func get_current_flow_name(): +var current_flow_name: String setget , get_current_flow_name +func get_current_flow_name() -> String: return self._current_flow.name -var in_expression_evaluation setget set_in_expression_evaluation, get_in_expression_evaluation # bool -func get_in_expression_evaluation(): +var in_expression_evaluation: bool setget \ + set_in_expression_evaluation, \ + get_in_expression_evaluation +func get_in_expression_evaluation() -> bool: return self.callstack.current_element.in_expression_evaluation -func set_in_expression_evaluation(value): +func set_in_expression_evaluation(value: bool): var current_element = self.callstack.current_element current_element.in_expression_evaluation = value -# (Story) -> StoryState func _init(story): get_json() self._story = weakref(story) - self._current_flow = Flow.new_with_name(DEFAULT_FLOW_NAME, story) + self._current_flow = InkFlow.new_with_name(DEFAULT_FLOW_NAME, story) self.output_stream_dirty() - self.evaluation_stack = [] # Array + self.evaluation_stack = [] - self.variables_state = VariablesState.new(self.callstack, self.story.list_definitions) + self.variables_state = InkVariablesState.new(self.callstack, self.story.list_definitions) - self._visit_counts = {} # Dictionary - self._turn_indices = {} # Dictionary + self._visit_counts = {} + self._turn_indices = {} self.current_turn_index = -1 randomize() @@ -326,7 +340,7 @@ func _init(story): # () -> void func go_to_start(): var current_element = self.callstack.current_element - current_element.current_pointer = Pointer.start_of(self.story.main_content_container) + current_element.current_pointer = InkPointer.start_of(self.story.main_content_container) # (String) -> void func switch_flow_internal(flow_name): @@ -344,7 +358,7 @@ func switch_flow_internal(flow_name): if self._named_flows.has(flow_name): flow = self._named_flows[flow_name] else: - flow = Flow.new_with_name(flow_name, self.story) + flow = InkFlow.new_with_name(flow_name, self.story) self._named_flows[flow_name] = flow self._current_flow = flow @@ -376,12 +390,12 @@ func remove_flow_internal(flow_name): # () -> StoryState func copy_and_start_patching(): - var copy = StoryState().new(self.story) + var copy = InkStoryState().new(self.story) - copy._patch = StatePatch.new(self._patch) + copy._patch = InkStatePatch.new(self._patch) copy._current_flow.name = self._current_flow.name - copy._current_flow.callstack = CallStack.new(self._current_flow.callstack) + copy._current_flow.callstack = InkCallStack.new(self._current_flow.callstack) copy._current_flow.current_choices += self._current_flow.current_choices copy._current_flow.output_stream += self._current_flow.output_stream copy.output_stream_dirty() @@ -511,10 +525,10 @@ func load_json_obj(jobject): var name = named_flow_obj_key var flow_obj = flows_obj_dict[named_flow_obj_key] - var flow = Flow.new_with_name_and_jobject(name, self.story, flow_obj) + var flow = InkFlow.new_with_name_and_jobject(name, self.story, flow_obj) if flows_obj_dict.size() == 1: - self._current_flow = Flow.new_with_name_and_jobject(name, self.story, flow_obj) + self._current_flow = InkFlow.new_with_name_and_jobject(name, self.story, flow_obj) else: self._named_flows[name] = flow @@ -633,10 +647,10 @@ func try_splitting_head_tail_whitespace(single): if head_first_newline_idx != -1: if head_first_newline_idx > 0: - var leading_spaces = StringValue.new_with(_str.substr(0, head_first_newline_idx)) + var leading_spaces = InkStringValue.new_with(_str.substr(0, head_first_newline_idx)) list_texts.append(leading_spaces) - list_texts.append(StringValue.new_with("\n")) + list_texts.append(InkStringValue.new_with("\n")) inner_str_start = head_last_newline_idx + 1 if tail_last_newline_idx != -1: @@ -644,13 +658,13 @@ func try_splitting_head_tail_whitespace(single): if inner_str_end > inner_str_start: var inner_str_text = _str.substr(inner_str_start, inner_str_end - inner_str_start) - list_texts.append(StringValue.new(inner_str_text)) + list_texts.append(InkStringValue.new(inner_str_text)) if tail_last_newline_idx != -1 && tail_first_newline_idx > head_last_newline_idx: - list_texts.append(StringValue.new("\n")) + list_texts.append(InkStringValue.new("\n")) if tail_last_newline_idx < _str.length() - 1: var num_spaces = (_str.length() - tail_last_newline_idx) - 1 - var trailing_spaces = StringValue.new(_str.substr(tail_last_newline_idx + 1, num_spaces)) + var trailing_spaces = InkStringValue.new(_str.substr(tail_last_newline_idx + 1, num_spaces)) list_texts.append(trailing_spaces) return list_texts @@ -681,7 +695,7 @@ func push_to_output_stream_individual(obj): if g: glue_trim_index = i break - elif c && c.command_type == ControlCommand.CommandType.BEGIN_STRING: + elif c && c.command_type == InkControlCommand.CommandType.BEGIN_STRING: if i >= function_trim_index: function_trim_index = -1 @@ -799,7 +813,7 @@ func get_in_string_evaluation(): while (i >= 0): var cmd = Utils.as_or_null(self.output_stream[i], "ControlCommand") - if cmd && cmd.command_type == ControlCommand.CommandType.BEGIN_STRING: + if cmd && cmd.command_type == InkControlCommand.CommandType.BEGIN_STRING: return true i -= 1 @@ -852,8 +866,8 @@ func force_end(): self._current_flow.current_choices.clear() - self.current_pointer = Pointer.null() - self.previous_pointer = Pointer.null() + self.current_pointer = InkPointer.null() + self.previous_pointer = InkPointer.null() self.did_safe_exit = true @@ -909,7 +923,7 @@ func set_chosen_path(path, incrementing_turn_index): func start_function_evaluation_from_game(func_container, arguments): self.callstack.push(PushPopType.FUNCTION_EVALUATION_FROM_GAME, self.evaluation_stack.size()) var current_element = self.callstack.current_element - current_element.current_pointer = Pointer.start_of(func_container) + current_element.current_pointer = InkPointer.start_of(func_container) self.pass_arguments_to_evaluation_stack(arguments) @@ -927,14 +941,14 @@ func pass_arguments_to_evaluation_stack(arguments): )) return - push_evaluation_stack(Value.create(arguments[i])) + push_evaluation_stack(InkValue.create(arguments[i])) i += 1 # () -> bool func try_exit_function_evaluation_from_game(): if self.callstack.current_element.type == PushPopType.FUNCTION_EVALUATION_FROM_GAME: - self.current_pointer = Pointer.null() + self.current_pointer = InkPointer.null() self.did_safe_exit = true return true @@ -988,11 +1002,14 @@ func output_stream_dirty(): self._output_stream_text_dirty = true self._output_stream_tags_dirty = true -var _visit_counts = null # Dictionary -var _turn_indices = null # Dictionary +# Dictionary +var _visit_counts: Dictionary + +# Dictionary +var _turn_indices: Dictionary -var _output_stream_text_dirty = true # bool -var _output_stream_tags_dirty = true # bool +var _output_stream_text_dirty: bool = true # bool +var _output_stream_tags_dirty: bool = true # bool var _patch # StatePatch diff --git a/addons/inkgd/runtime/structs/pointer.gd b/addons/inkgd/runtime/structs/pointer.gd index 3c520575..919e6838 100644 --- a/addons/inkgd/runtime/structs/pointer.gd +++ b/addons/inkgd/runtime/structs/pointer.gd @@ -24,6 +24,9 @@ class_name InkPointer var InkPath := preload("res://addons/inkgd/runtime/ink_path.gd") as GDScript +static func InkPointer() -> GDScript: + return load("res://addons/inkgd/runtime/structs/pointer.gd") as GDScript + # ############################################################################ # # InkContainer @@ -58,16 +61,17 @@ func resolve(): # ############################################################################ # # () -> bool -var is_null setget , get_is_null -func get_is_null(): +var is_null: bool setget , get_is_null +func get_is_null() -> bool: return self.container == null # ############################################################################ # # () -> InkPath -var path setget , get_path -func get_path(): - if self.is_null: return null +var path: InkPath setget , get_path +func get_path() -> InkPath: + if self.is_null: + return null if index >= 0: return self.container.path.path_by_appending_component(InkPath.Component.new(index)) @@ -76,36 +80,31 @@ func get_path(): ############################################################################ # -# () -> String -func to_string(): +func to_string() -> String: if self.container == null: return "Ink Pointer (null)" return "Ink Pointer -> %s -- index %d" % [self.container.path.to_string(), index] # (InkContainer) -> InkPointer -static func start_of(container): - return Pointer().new(container, 0) +static func start_of(container: InkContainer) -> InkPointer: + return InkPointer().new(container, 0) # ############################################################################ # # () -> InkPointer static func null(): - return Pointer().new(null, -1) + return InkPointer().new(null, -1) # ############################################################################ # # GDScript extra methods # ############################################################################ # -func is_class(type): +func is_class(type: String) -> bool: return type == "Pointer" || .is_class(type) -func get_class(): +func get_class() -> String: return "Pointer" -# () -> Pointer -func duplicate(): - return Pointer().new(self.container, index) - -static func Pointer(): - return load("res://addons/inkgd/runtime/structs/pointer.gd") +func duplicate() -> InkPointer: + return InkPointer().new(self.container, index) diff --git a/project.godot b/project.godot index 1dfbfa02..0ac8dbc4 100644 --- a/project.godot +++ b/project.godot @@ -254,7 +254,7 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://addons/inkgd/runtime/state_patch.gd" }, { -"base": "Reference", +"base": "InkBase", "class": "InkStaticJSON", "language": "GDScript", "path": "res://addons/inkgd/runtime/static/json.gd"