From 87cf21e0e5ccb9b7fa50f4fc14d39a6d230bcdac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mikrut?= <41945903+qarmin@users.noreply.github.com> Date: Fri, 18 Jun 2021 18:38:38 +0200 Subject: [PATCH] instance (#56) --- Autoload/Autoload.gd | 27 ++++++++++------------ AutomaticBugs/FunctionExecutor.gd | 4 ++-- AutomaticBugs/ValueCreator.gd | 12 +++++----- Nodes/Nodes.gd | 8 +++---- ReparentingDeleting/ReparentingDeleting.gd | 4 ++-- Start.gd | 4 ++-- 6 files changed, 28 insertions(+), 31 deletions(-) diff --git a/Autoload/Autoload.gd b/Autoload/Autoload.gd index 2e98afa..2ffbbb4 100644 --- a/Autoload/Autoload.gd +++ b/Autoload/Autoload.gd @@ -12,8 +12,7 @@ var time_to_show: int = 25 * 1000 # How long test works in miliseconds var time_for_each_step : int = -1 -var os -var time +var os # Each scene runs alone const alone_steps : Array = [ @@ -26,18 +25,11 @@ const alone_steps : Array = [ func _init(): if ClassDB.class_exists("_OS"): - os = ClassDB.instance("_OS") + os = get_instance_from_name("_OS") else: - os = ClassDB.instance("_Platform") + os = get_instance_from_name("_Platform") - if ClassDB.class_exists("Time"): - time = ClassDB.instance("Time") - elif ClassDB.class_exists("_OS"): - time = ClassDB.instance("_OS") - else: - time = ClassDB.instance("_Platform") - - start_time = time.get_ticks_msec() + start_time = Time.get_ticks_msec() # In case when user doesn't provide time time_for_each_step = time_to_show / (alone_steps.size()) @@ -51,7 +43,7 @@ func _init(): func _process(delta: float) -> void: - var current_run_time : int = time.get_ticks_msec() - start_time + var current_run_time : int = Time.get_ticks_msec() - start_time # While loop instead if, will allow to properly flush results under heavy operations(e.g. Thread sanitizer) while current_run_time > time_to_print_next_time: @@ -62,7 +54,12 @@ func _process(delta: float) -> void: print("######################## Ending test ########################") get_tree().quit() +func get_instance_from_name(method: String): + if ClassDB.class_has_method("_ClassDB","instance"): + return ClassDB.call("instance",method) + else: + return ClassDB.call("instantiate",method) + + func _exit_tree(): os.free() - time.free() - diff --git a/AutomaticBugs/FunctionExecutor.gd b/AutomaticBugs/FunctionExecutor.gd index d06a884..7e4acf0 100644 --- a/AutomaticBugs/FunctionExecutor.gd +++ b/AutomaticBugs/FunctionExecutor.gd @@ -40,7 +40,7 @@ func tests_all_functions() -> void: if debug_print: print("\n#################### " + name_of_class + " ####################") - var object: Object = ClassDB.instance(name_of_class) + var object: Object = Autoload.get_instance_from_name(name_of_class) assert(object != null, "Object must be instantable") if add_to_tree: if object is Node: @@ -90,7 +90,7 @@ func tests_all_functions() -> void: elif object is Object && !(obj_is_reference(object.get_class())): object.free() - object = ClassDB.instance(name_of_class) + object = Autoload.get_instance_from_name(name_of_class) if add_to_tree: if object is Node: add_child(object) diff --git a/AutomaticBugs/ValueCreator.gd b/AutomaticBugs/ValueCreator.gd index f56fcbb..1e4f1d8 100644 --- a/AutomaticBugs/ValueCreator.gd +++ b/AutomaticBugs/ValueCreator.gd @@ -283,7 +283,7 @@ func get_object(object_name: String) -> Object: && (ClassDB.is_parent_class(choosen_class, "Node") || obj_is_reference(choosen_class)) && !(choosen_class in BasicData.disabled_classes) ): - return ClassDB.instance(choosen_class) + return Autoload.get_instance_from_name(choosen_class) if ClassDB.is_parent_class(object_name, "Node") || obj_is_reference(object_name): if should_be_always_valid: @@ -300,7 +300,7 @@ func get_object(object_name: String) -> Object: assert(false, "Cannot find proper instantable child for ") var choosen_class: String = to_use_classes[randi() % to_use_classes.size()] if ClassDB.can_instance(choosen_class) && !(choosen_class in BasicData.disabled_classes): - return ClassDB.instance(choosen_class) + return Autoload.get_instance_from_name(choosen_class) else: while true: a += 1 @@ -308,7 +308,7 @@ func get_object(object_name: String) -> Object: assert(false, "Cannot find proper instantable child for ") var choosen_class: String = classes[randi() % classes.size()] if ClassDB.can_instance(choosen_class) && !ClassDB.is_parent_class(choosen_class, object_name) && !(choosen_class in BasicData.disabled_classes): - return ClassDB.instance(choosen_class) + return Autoload.get_instance_from_name(choosen_class) # Non Node/Resource object var to_use_classes = ClassDB.get_inheriters_from_class(object_name) @@ -324,17 +324,17 @@ func get_object(object_name: String) -> Object: assert(false, "Cannot find proper instantable child for ") var choosen_class: String = to_use_classes[randi() % to_use_classes.size()] if ClassDB.can_instance(choosen_class) && !(choosen_class in BasicData.disabled_classes): - return ClassDB.instance(choosen_class) + return Autoload.get_instance_from_name(choosen_class) else: if ClassDB.can_instance(object_name): # E.g. Texture is not instantable or shouldn't be, but LargeTexture is - return ClassDB.instance(object_name) + return Autoload.get_instance_from_name(object_name) else: # Found child of non instantable object var list_of_class = ClassDB.get_inheriters_from_class(object_name) assert(list_of_class.size() > 0, "Cannot find proper instantable child for ") # Number of inherited class of non instantable class must be greater than 0, otherwise this function would be useless for i in list_of_class: if ClassDB.can_instance(i) && (ClassDB.is_parent_class(i, "Node") || obj_is_reference(i)): - return ClassDB.instance(i) + return Autoload.get_instance_from_name(i) assert(false, "Cannot find proper instantable child for ") assert(false, "Cannot find proper instantable child for ") diff --git a/Nodes/Nodes.gd b/Nodes/Nodes.gd index 55e3ec9..8774c50 100644 --- a/Nodes/Nodes.gd +++ b/Nodes/Nodes.gd @@ -22,16 +22,16 @@ func _populate() -> void: if ClassDB.is_parent_class(name_of_class,"Control"): - add_child(ClassDB.instance(name_of_class)) + add_child(Autoload.get_instance_from_name(name_of_class)) continue if ClassDB.is_parent_class(name_of_class,"Node3D"): - add_child(ClassDB.instance(name_of_class)) + add_child(Autoload.get_instance_from_name(name_of_class)) continue if ClassDB.is_parent_class(name_of_class,"Node2D"): - add_child(ClassDB.instance(name_of_class)) + add_child(Autoload.get_instance_from_name(name_of_class)) continue if ClassDB.is_parent_class(name_of_class,"Node"): - add_child(ClassDB.instance(name_of_class)) + add_child(Autoload.get_instance_from_name(name_of_class)) continue diff --git a/ReparentingDeleting/ReparentingDeleting.gd b/ReparentingDeleting/ReparentingDeleting.gd index 1197502..8a40f96 100644 --- a/ReparentingDeleting/ReparentingDeleting.gd +++ b/ReparentingDeleting/ReparentingDeleting.gd @@ -36,7 +36,7 @@ func _ready() -> void: if i >= collected_nodes.size(): # Wrap values index = i % collected_nodes.size() - var child : Node = ClassDB.instance(collected_nodes[index]) + var child : Node = Autoload.get_instance_from_name(collected_nodes[index]) child.set_name("Special Node " + str(i)) add_child(child) @@ -70,7 +70,7 @@ func _process(delta: float) -> void: # if randi() % 6 == 0: # 16% chance to remove node with children # var names_to_remove : Array = find_all_special_children_names(choosen_node) # for name_to_remove in names_to_remove: -# var node : Node = ClassDB.instance(collected_nodes[randi() % collected_nodes.size()]) +# var node : Node = Autoload.get_instance_from_name(collected_nodes[randi() % collected_nodes.size()]) # node.set_name(name_to_remove) # add_child(node) # choosen_node.queue_free() diff --git a/Start.gd b/Start.gd index cff5f04..90a4d58 100644 --- a/Start.gd +++ b/Start.gd @@ -9,11 +9,11 @@ var array_with_time_to_change: Array = [] func _ready(): for i in Autoload.alone_steps.size() + 1: - array_with_time_to_change.append(Autoload.time.get_ticks_msec() + i * Autoload.time_for_each_step) + array_with_time_to_change.append(Time.get_ticks_msec() + i * Autoload.time_for_each_step) func _process(_delta): - if current_scene < Autoload.alone_steps.size() - 1 && Autoload.time.get_ticks_msec() > array_with_time_to_change[current_scene + 1]: + if current_scene < Autoload.alone_steps.size() - 1 && Time.get_ticks_msec() > array_with_time_to_change[current_scene + 1]: current_scene += 1 for child in get_children():